• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/types/optional.h"
21 #include "api/audio_codecs/audio_encoder.h"
22 #include "api/audio_options.h"
23 #include "api/crypto/frame_decryptor_interface.h"
24 #include "api/crypto/frame_encryptor_interface.h"
25 #include "api/frame_transformer_interface.h"
26 #include "api/media_stream_interface.h"
27 #include "api/rtc_error.h"
28 #include "api/rtp_parameters.h"
29 #include "api/rtp_sender_interface.h"
30 #include "api/task_queue/pending_task_safety_flag.h"
31 #include "api/transport/data_channel_transport_interface.h"
32 #include "api/transport/rtp/rtp_source.h"
33 #include "api/units/time_delta.h"
34 #include "api/video/video_content_type.h"
35 #include "api/video/video_sink_interface.h"
36 #include "api/video/video_source_interface.h"
37 #include "api/video/video_timing.h"
38 #include "api/video_codecs/video_encoder_factory.h"
39 #include "call/video_receive_stream.h"
40 #include "common_video/include/quality_limitation_reason.h"
41 #include "media/base/codec.h"
42 #include "media/base/delayable.h"
43 #include "media/base/media_constants.h"
44 #include "media/base/stream_params.h"
45 #include "modules/audio_processing/include/audio_processing_statistics.h"
46 #include "modules/rtp_rtcp/include/report_block_data.h"
47 #include "rtc_base/async_packet_socket.h"
48 #include "rtc_base/buffer.h"
49 #include "rtc_base/copy_on_write_buffer.h"
50 #include "rtc_base/dscp.h"
51 #include "rtc_base/logging.h"
52 #include "rtc_base/network_route.h"
53 #include "rtc_base/socket.h"
54 #include "rtc_base/string_encode.h"
55 #include "rtc_base/strings/string_builder.h"
56 #include "video/config/video_encoder_config.h"
57 
58 namespace rtc {
59 class Timing;
60 }
61 
62 namespace webrtc {
63 class AudioSinkInterface;
64 class VideoFrame;
65 
66 webrtc::RTCError InvokeSetParametersCallback(SetParametersCallback& callback,
67                                              RTCError error);
68 
69 }  // namespace webrtc
70 
71 namespace cricket {
72 
73 class AudioSource;
74 class VideoCapturer;
75 struct RtpHeader;
76 struct VideoFormat;
77 
78 const int kScreencastDefaultFps = 5;
79 
80 template <class T>
ToStringIfSet(const char * key,const absl::optional<T> & val)81 static std::string ToStringIfSet(const char* key,
82                                  const absl::optional<T>& val) {
83   std::string str;
84   if (val) {
85     str = key;
86     str += ": ";
87     str += val ? rtc::ToString(*val) : "";
88     str += ", ";
89   }
90   return str;
91 }
92 
93 template <class T>
VectorToString(const std::vector<T> & vals)94 static std::string VectorToString(const std::vector<T>& vals) {
95   rtc::StringBuilder ost;  // no-presubmit-check TODO(webrtc:8982)
96   ost << "[";
97   for (size_t i = 0; i < vals.size(); ++i) {
98     if (i > 0) {
99       ost << ", ";
100     }
101     ost << vals[i].ToString();
102   }
103   ost << "]";
104   return ost.Release();
105 }
106 
107 // Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
108 // Used to be flags, but that makes it hard to selectively apply options.
109 // We are moving all of the setting of options to structs like this,
110 // but some things currently still use flags.
111 struct VideoOptions {
112   VideoOptions();
113   ~VideoOptions();
114 
SetAllVideoOptions115   void SetAll(const VideoOptions& change) {
116     SetFrom(&video_noise_reduction, change.video_noise_reduction);
117     SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
118     SetFrom(&is_screencast, change.is_screencast);
119   }
120 
121   bool operator==(const VideoOptions& o) const {
122     return video_noise_reduction == o.video_noise_reduction &&
123            screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
124            is_screencast == o.is_screencast;
125   }
126   bool operator!=(const VideoOptions& o) const { return !(*this == o); }
127 
ToStringVideoOptions128   std::string ToString() const {
129     rtc::StringBuilder ost;
130     ost << "VideoOptions {";
131     ost << ToStringIfSet("noise reduction", video_noise_reduction);
132     ost << ToStringIfSet("screencast min bitrate kbps",
133                          screencast_min_bitrate_kbps);
134     ost << ToStringIfSet("is_screencast ", is_screencast);
135     ost << "}";
136     return ost.Release();
137   }
138 
139   // Enable denoising? This flag comes from the getUserMedia
140   // constraint 'googNoiseReduction', and WebRtcVideoEngine passes it
141   // on to the codec options. Disabled by default.
142   absl::optional<bool> video_noise_reduction;
143   // Force screencast to use a minimum bitrate. This flag comes from
144   // the PeerConnection constraint 'googScreencastMinBitrate'. It is
145   // copied to the encoder config by WebRtcVideoChannel.
146   // TODO(https://crbug.com/1315155): Remove the ability to set it in Chromium
147   // and delete this flag (it should default to 100 kbps).
148   absl::optional<int> screencast_min_bitrate_kbps;
149   // Set by screencast sources. Implies selection of encoding settings
150   // suitable for screencast. Most likely not the right way to do
151   // things, e.g., screencast of a text document and screencast of a
152   // youtube video have different needs.
153   absl::optional<bool> is_screencast;
154   webrtc::VideoTrackInterface::ContentHint content_hint;
155 
156  private:
157   template <typename T>
SetFromVideoOptions158   static void SetFrom(absl::optional<T>* s, const absl::optional<T>& o) {
159     if (o) {
160       *s = o;
161     }
162   }
163 };
164 
165 class MediaChannel {
166  public:
167   class NetworkInterface {
168    public:
169     enum SocketType { ST_RTP, ST_RTCP };
170     virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
171                             const rtc::PacketOptions& options) = 0;
172     virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
173                           const rtc::PacketOptions& options) = 0;
174     virtual int SetOption(SocketType type,
175                           rtc::Socket::Option opt,
176                           int option) = 0;
~NetworkInterface()177     virtual ~NetworkInterface() {}
178   };
179 
180   explicit MediaChannel(webrtc::TaskQueueBase* network_thread,
181                         bool enable_dscp = false);
182   virtual ~MediaChannel();
183 
184   virtual cricket::MediaType media_type() const = 0;
185 
186   // Sets the abstract interface class for sending RTP/RTCP data.
187   virtual void SetInterface(NetworkInterface* iface);
188   // Called on the network when an RTP packet is received.
189   virtual void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
190                                 int64_t packet_time_us) = 0;
191   // Called on the network thread after a transport has finished sending a
192   // packet.
193   virtual void OnPacketSent(const rtc::SentPacket& sent_packet) = 0;
194   // Called when the socket's ability to send has changed.
195   virtual void OnReadyToSend(bool ready) = 0;
196   // Called when the network route used for sending packets changed.
197   virtual void OnNetworkRouteChanged(
198       absl::string_view transport_name,
199       const rtc::NetworkRoute& network_route) = 0;
200   // Creates a new outgoing media stream with SSRCs and CNAME as described
201   // by sp.
202   virtual bool AddSendStream(const StreamParams& sp) = 0;
203   // Removes an outgoing media stream.
204   // SSRC must be the first SSRC of the media stream if the stream uses
205   // multiple SSRCs. In the case of an ssrc of 0, the possibly cached
206   // StreamParams is removed.
207   virtual bool RemoveSendStream(uint32_t ssrc) = 0;
208   // Creates a new incoming media stream with SSRCs, CNAME as described
209   // by sp. In the case of a sp without SSRCs, the unsignaled sp is cached
210   // to be used later for unsignaled streams received.
211   virtual bool AddRecvStream(const StreamParams& sp) = 0;
212   // Removes an incoming media stream.
213   // ssrc must be the first SSRC of the media stream if the stream uses
214   // multiple SSRCs.
215   virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
216   // Resets any cached StreamParams for an unsignaled RecvStream, and removes
217   // any existing unsignaled streams.
218   virtual void ResetUnsignaledRecvStream() = 0;
219   // This is currently a workaround because of the demuxer state being managed
220   // across two separate threads. Once the state is consistently managed on
221   // the same thread (network), this workaround can be removed.
222   // These two notifications inform the media channel when the transport's
223   // demuxer criteria is being updated.
224   // * OnDemuxerCriteriaUpdatePending() happens on the same thread that the
225   //   channel's streams are added and removed (worker thread).
226   // * OnDemuxerCriteriaUpdateComplete() happens on the same thread.
227   // Because the demuxer is updated asynchronously, there is a window of time
228   // where packets are arriving to the channel for streams that have already
229   // been removed on the worker thread. It is important NOT to treat these as
230   // new unsignalled ssrcs.
231   virtual void OnDemuxerCriteriaUpdatePending() = 0;
232   virtual void OnDemuxerCriteriaUpdateComplete() = 0;
233   // Returns the absoulte sendtime extension id value from media channel.
234   virtual int GetRtpSendTimeExtnId() const;
235   // Set the frame encryptor to use on all outgoing frames. This is optional.
236   // This pointers lifetime is managed by the set of RtpSender it is attached
237   // to.
238   // TODO(benwright) make pure virtual once internal supports it.
239   virtual void SetFrameEncryptor(
240       uint32_t ssrc,
241       rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
242   // Set the frame decryptor to use on all incoming frames. This is optional.
243   // This pointers lifetimes is managed by the set of RtpReceivers it is
244   // attached to.
245   // TODO(benwright) make pure virtual once internal supports it.
246   virtual void SetFrameDecryptor(
247       uint32_t ssrc,
248       rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
249 
250   // Enable network condition based codec switching.
251   virtual void SetVideoCodecSwitchingEnabled(bool enabled);
252 
253   // note: The encoder_selector object must remain valid for the lifetime of the
254   // MediaChannel, unless replaced.
SetEncoderSelector(uint32_t ssrc,webrtc::VideoEncoderFactory::EncoderSelectorInterface * encoder_selector)255   virtual void SetEncoderSelector(
256       uint32_t ssrc,
257       webrtc::VideoEncoderFactory::EncoderSelectorInterface* encoder_selector) {
258   }
259 
260   // Base method to send packet using NetworkInterface.
261   bool SendPacket(rtc::CopyOnWriteBuffer* packet,
262                   const rtc::PacketOptions& options);
263 
264   bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
265                 const rtc::PacketOptions& options);
266 
267   int SetOption(NetworkInterface::SocketType type,
268                 rtc::Socket::Option opt,
269                 int option);
270 
271   // Corresponds to the SDP attribute extmap-allow-mixed, see RFC8285.
272   // Set to true if it's allowed to mix one- and two-byte RTP header extensions
273   // in the same stream. The setter and getter must only be called from
274   // worker_thread.
275   void SetExtmapAllowMixed(bool extmap_allow_mixed);
276   bool ExtmapAllowMixed() const;
277 
278   // Returns `true` if a non-null NetworkInterface pointer is held.
279   // Must be called on the network thread.
280   bool HasNetworkInterface() const;
281 
282   virtual webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const = 0;
283   virtual webrtc::RTCError SetRtpSendParameters(
284       uint32_t ssrc,
285       const webrtc::RtpParameters& parameters,
286       webrtc::SetParametersCallback callback = nullptr) = 0;
287 
288   virtual void SetEncoderToPacketizerFrameTransformer(
289       uint32_t ssrc,
290       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer);
291   virtual void SetDepacketizerToDecoderFrameTransformer(
292       uint32_t ssrc,
293       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer);
294 
295  protected:
296   int SetOptionLocked(NetworkInterface::SocketType type,
297                       rtc::Socket::Option opt,
298                       int option) RTC_RUN_ON(network_thread_);
299 
300   bool DscpEnabled() const;
301 
302   // This is the DSCP value used for both RTP and RTCP channels if DSCP is
303   // enabled. It can be changed at any time via `SetPreferredDscp`.
304   rtc::DiffServCodePoint PreferredDscp() const;
305   void SetPreferredDscp(rtc::DiffServCodePoint new_dscp);
306 
307   rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> network_safety();
308 
309   // Utility implementation for derived classes (video/voice) that applies
310   // the packet options and passes the data onwards to `SendPacket`.
311   void SendRtp(const uint8_t* data,
312                size_t len,
313                const webrtc::PacketOptions& options);
314 
315   void SendRtcp(const uint8_t* data, size_t len);
316 
317  private:
318   // Apply the preferred DSCP setting to the underlying network interface RTP
319   // and RTCP channels. If DSCP is disabled, then apply the default DSCP value.
320   void UpdateDscp() RTC_RUN_ON(network_thread_);
321 
322   bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
323                     bool rtcp,
324                     const rtc::PacketOptions& options);
325 
326   const bool enable_dscp_;
327   const rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> network_safety_
328       RTC_PT_GUARDED_BY(network_thread_);
329   webrtc::TaskQueueBase* const network_thread_;
330   NetworkInterface* network_interface_ RTC_GUARDED_BY(network_thread_) =
331       nullptr;
332   rtc::DiffServCodePoint preferred_dscp_ RTC_GUARDED_BY(network_thread_) =
333       rtc::DSCP_DEFAULT;
334   bool extmap_allow_mixed_ = false;
335 };
336 
337 // The stats information is structured as follows:
338 // Media are represented by either MediaSenderInfo or MediaReceiverInfo.
339 // Media contains a vector of SSRC infos that are exclusively used by this
340 // media. (SSRCs shared between media streams can't be represented.)
341 
342 // Information about an SSRC.
343 // This data may be locally recorded, or received in an RTCP SR or RR.
344 struct SsrcSenderInfo {
345   uint32_t ssrc = 0;
346   double timestamp = 0.0;  // NTP timestamp, represented as seconds since epoch.
347 };
348 
349 struct SsrcReceiverInfo {
350   uint32_t ssrc = 0;
351   double timestamp = 0.0;
352 };
353 
354 struct MediaSenderInfo {
355   MediaSenderInfo();
356   ~MediaSenderInfo();
add_ssrcMediaSenderInfo357   void add_ssrc(const SsrcSenderInfo& stat) { local_stats.push_back(stat); }
358   // Temporary utility function for call sites that only provide SSRC.
359   // As more info is added into SsrcSenderInfo, this function should go away.
add_ssrcMediaSenderInfo360   void add_ssrc(uint32_t ssrc) {
361     SsrcSenderInfo stat;
362     stat.ssrc = ssrc;
363     add_ssrc(stat);
364   }
365   // Utility accessor for clients that are only interested in ssrc numbers.
ssrcsMediaSenderInfo366   std::vector<uint32_t> ssrcs() const {
367     std::vector<uint32_t> retval;
368     for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
369          it != local_stats.end(); ++it) {
370       retval.push_back(it->ssrc);
371     }
372     return retval;
373   }
374   // Returns true if the media has been connected.
connectedMediaSenderInfo375   bool connected() const { return local_stats.size() > 0; }
376   // Utility accessor for clients that make the assumption only one ssrc
377   // exists per media.
378   // This will eventually go away.
379   // Call sites that compare this to zero should use connected() instead.
380   // https://bugs.webrtc.org/8694
ssrcMediaSenderInfo381   uint32_t ssrc() const {
382     if (connected()) {
383       return local_stats[0].ssrc;
384     } else {
385       return 0;
386     }
387   }
388   // https://w3c.github.io/webrtc-stats/#dom-rtcsentrtpstreamstats-bytessent
389   int64_t payload_bytes_sent = 0;
390   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-headerbytessent
391   int64_t header_and_padding_bytes_sent = 0;
392   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedbytessent
393   uint64_t retransmitted_bytes_sent = 0;
394   int packets_sent = 0;
395   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedpacketssent
396   uint64_t retransmitted_packets_sent = 0;
397   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-nackcount
398   uint32_t nacks_rcvd = 0;
399   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-targetbitrate
400   absl::optional<double> target_bitrate;
401   int packets_lost = 0;
402   float fraction_lost = 0.0f;
403   int64_t rtt_ms = 0;
404   std::string codec_name;
405   absl::optional<int> codec_payload_type;
406   std::vector<SsrcSenderInfo> local_stats;
407   std::vector<SsrcReceiverInfo> remote_stats;
408   // A snapshot of the most recent Report Block with additional data of interest
409   // to statistics. Used to implement RTCRemoteInboundRtpStreamStats. Within
410   // this list, the ReportBlockData::RTCPReportBlock::source_ssrc(), which is
411   // the SSRC of the corresponding outbound RTP stream, is unique.
412   std::vector<webrtc::ReportBlockData> report_block_datas;
413   absl::optional<bool> active;
414   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalpacketsenddelay
415   webrtc::TimeDelta total_packet_send_delay = webrtc::TimeDelta::Zero();
416 };
417 
418 struct MediaReceiverInfo {
419   MediaReceiverInfo();
420   ~MediaReceiverInfo();
add_ssrcMediaReceiverInfo421   void add_ssrc(const SsrcReceiverInfo& stat) { local_stats.push_back(stat); }
422   // Temporary utility function for call sites that only provide SSRC.
423   // As more info is added into SsrcSenderInfo, this function should go away.
add_ssrcMediaReceiverInfo424   void add_ssrc(uint32_t ssrc) {
425     SsrcReceiverInfo stat;
426     stat.ssrc = ssrc;
427     add_ssrc(stat);
428   }
ssrcsMediaReceiverInfo429   std::vector<uint32_t> ssrcs() const {
430     std::vector<uint32_t> retval;
431     for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
432          it != local_stats.end(); ++it) {
433       retval.push_back(it->ssrc);
434     }
435     return retval;
436   }
437   // Returns true if the media has been connected.
connectedMediaReceiverInfo438   bool connected() const { return local_stats.size() > 0; }
439   // Utility accessor for clients that make the assumption only one ssrc
440   // exists per media.
441   // This will eventually go away.
442   // Call sites that compare this to zero should use connected();
443   // https://bugs.webrtc.org/8694
ssrcMediaReceiverInfo444   uint32_t ssrc() const {
445     if (connected()) {
446       return local_stats[0].ssrc;
447     } else {
448       return 0;
449     }
450   }
451 
452   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-bytesreceived
453   int64_t payload_bytes_rcvd = 0;
454   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-headerbytesreceived
455   int64_t header_and_padding_bytes_rcvd = 0;
456   int packets_rcvd = 0;
457   int packets_lost = 0;
458   absl::optional<uint32_t> nacks_sent;
459   // Jitter (network-related) latency (cumulative).
460   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferdelay
461   double jitter_buffer_delay_seconds = 0.0;
462   // Target delay for the jitter buffer (cumulative).
463   // TODO(crbug.com/webrtc/14244): This metric is only implemented for
464   // audio, it should be implemented for video as well.
465   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbuffertargetdelay
466   absl::optional<double> jitter_buffer_target_delay_seconds;
467   // Minimum obtainable delay for the jitter buffer (cumulative).
468   // TODO(crbug.com/webrtc/14244): This metric is only implemented for
469   // audio, it should be implemented for video as well.
470   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferminimumdelay
471   absl::optional<double> jitter_buffer_minimum_delay_seconds;
472   // Number of observations for cumulative jitter latency.
473   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferemittedcount
474   uint64_t jitter_buffer_emitted_count = 0;
475   // The timestamp at which the last packet was received, i.e. the time of the
476   // local clock when it was received - not the RTP timestamp of that packet.
477   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-lastpacketreceivedtimestamp
478   absl::optional<int64_t> last_packet_received_timestamp_ms;
479   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp
480   absl::optional<int64_t> estimated_playout_ntp_timestamp_ms;
481   std::string codec_name;
482   absl::optional<int> codec_payload_type;
483   std::vector<SsrcReceiverInfo> local_stats;
484   std::vector<SsrcSenderInfo> remote_stats;
485 };
486 
487 struct VoiceSenderInfo : public MediaSenderInfo {
488   VoiceSenderInfo();
489   ~VoiceSenderInfo();
490   int jitter_ms = 0;
491   // Current audio level, expressed linearly [0,32767].
492   int audio_level = 0;
493   // See description of "totalAudioEnergy" in the WebRTC stats spec:
494   // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
495   double total_input_energy = 0.0;
496   double total_input_duration = 0.0;
497   webrtc::ANAStats ana_statistics;
498   webrtc::AudioProcessingStats apm_statistics;
499 };
500 
501 struct VoiceReceiverInfo : public MediaReceiverInfo {
502   VoiceReceiverInfo();
503   ~VoiceReceiverInfo();
504   int jitter_ms = 0;
505   int jitter_buffer_ms = 0;
506   int jitter_buffer_preferred_ms = 0;
507   int delay_estimate_ms = 0;
508   int audio_level = 0;
509   // Stats below correspond to similarly-named fields in the WebRTC stats spec.
510   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats
511   double total_output_energy = 0.0;
512   uint64_t total_samples_received = 0;
513   double total_output_duration = 0.0;
514   uint64_t concealed_samples = 0;
515   uint64_t silent_concealed_samples = 0;
516   uint64_t concealment_events = 0;
517   uint64_t inserted_samples_for_deceleration = 0;
518   uint64_t removed_samples_for_acceleration = 0;
519   uint64_t fec_packets_received = 0;
520   uint64_t fec_packets_discarded = 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   // Remote outbound stats derived by the received RTCP sender reports.
563   // https://w3c.github.io/webrtc-stats/#remoteoutboundrtpstats-dict*
564   absl::optional<int64_t> last_sender_report_timestamp_ms;
565   absl::optional<int64_t> last_sender_report_remote_timestamp_ms;
566   uint32_t sender_reports_packets_sent = 0;
567   uint64_t sender_reports_bytes_sent = 0;
568   uint64_t sender_reports_reports_count = 0;
569   absl::optional<webrtc::TimeDelta> round_trip_time;
570   webrtc::TimeDelta total_round_trip_time = webrtc::TimeDelta::Zero();
571   int round_trip_time_measurements = 0;
572 };
573 
574 struct VideoSenderInfo : public MediaSenderInfo {
575   VideoSenderInfo();
576   ~VideoSenderInfo();
577   std::vector<SsrcGroup> ssrc_groups;
578   std::string encoder_implementation_name;
579   int firs_rcvd = 0;
580   int plis_rcvd = 0;
581   int send_frame_width = 0;
582   int send_frame_height = 0;
583   int frames = 0;
584   double framerate_input = 0;
585   int framerate_sent = 0;
586   int aggregated_framerate_sent = 0;
587   int nominal_bitrate = 0;
588   int adapt_reason = 0;
589   int adapt_changes = 0;
590   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
591   webrtc::QualityLimitationReason quality_limitation_reason =
592       webrtc::QualityLimitationReason::kNone;
593   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
594   std::map<webrtc::QualityLimitationReason, int64_t>
595       quality_limitation_durations_ms;
596   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges
597   uint32_t quality_limitation_resolution_changes = 0;
598   int avg_encode_ms = 0;
599   int encode_usage_percent = 0;
600   uint32_t frames_encoded = 0;
601   uint32_t key_frames_encoded = 0;
602   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
603   uint64_t total_encode_time_ms = 0;
604   // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
605   uint64_t total_encoded_bytes_target = 0;
606   bool has_entered_low_resolution = false;
607   absl::optional<uint64_t> qp_sum;
608   webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
609   uint32_t frames_sent = 0;
610   // https://w3c.github.io/webrtc-stats/#dom-rtcvideosenderstats-hugeframessent
611   uint32_t huge_frames_sent = 0;
612   uint32_t aggregated_huge_frames_sent = 0;
613   absl::optional<std::string> rid;
614   absl::optional<bool> power_efficient_encoder;
615 };
616 
617 struct VideoReceiverInfo : public MediaReceiverInfo {
618   VideoReceiverInfo();
619   ~VideoReceiverInfo();
620   std::vector<SsrcGroup> ssrc_groups;
621   std::string decoder_implementation_name;
622   absl::optional<bool> power_efficient_decoder;
623   int packets_concealed = 0;
624   int firs_sent = 0;
625   int plis_sent = 0;
626   int frame_width = 0;
627   int frame_height = 0;
628   int framerate_rcvd = 0;
629   int framerate_decoded = 0;
630   int framerate_output = 0;
631   // Framerate as sent to the renderer.
632   int framerate_render_input = 0;
633   // Framerate that the renderer reports.
634   int framerate_render_output = 0;
635   uint32_t frames_received = 0;
636   uint32_t frames_dropped = 0;
637   uint32_t frames_decoded = 0;
638   uint32_t key_frames_decoded = 0;
639   uint32_t frames_rendered = 0;
640   absl::optional<uint64_t> qp_sum;
641   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totaldecodetime
642   webrtc::TimeDelta total_decode_time = webrtc::TimeDelta::Zero();
643   // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalprocessingdelay
644   webrtc::TimeDelta total_processing_delay = webrtc::TimeDelta::Zero();
645   webrtc::TimeDelta total_assembly_time = webrtc::TimeDelta::Zero();
646   uint32_t frames_assembled_from_multiple_packets = 0;
647   double total_inter_frame_delay = 0;
648   double total_squared_inter_frame_delay = 0;
649   int64_t interframe_delay_max_ms = -1;
650   uint32_t freeze_count = 0;
651   uint32_t pause_count = 0;
652   uint32_t total_freezes_duration_ms = 0;
653   uint32_t total_pauses_duration_ms = 0;
654   uint32_t total_frames_duration_ms = 0;
655   double sum_squared_frame_durations = 0.0;
656   uint32_t jitter_ms = 0;
657 
658   webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
659 
660   // All stats below are gathered per-VideoReceiver, but some will be correlated
661   // across MediaStreamTracks.  NOTE(hta): when sinking stats into per-SSRC
662   // structures, reflect this in the new layout.
663 
664   // Current frame decode latency.
665   int decode_ms = 0;
666   // Maximum observed frame decode latency.
667   int max_decode_ms = 0;
668   // Jitter (network-related) latency.
669   int jitter_buffer_ms = 0;
670   // Requested minimum playout latency.
671   int min_playout_delay_ms = 0;
672   // Requested latency to account for rendering delay.
673   int render_delay_ms = 0;
674   // Target overall delay: network+decode+render, accounting for
675   // min_playout_delay_ms.
676   int target_delay_ms = 0;
677   // Current overall delay, possibly ramping towards target_delay_ms.
678   int current_delay_ms = 0;
679 
680   // Estimated capture start time in NTP time in ms.
681   int64_t capture_start_ntp_time_ms = -1;
682 
683   // First frame received to first frame decoded latency.
684   int64_t first_frame_received_to_decoded_ms = -1;
685 
686   // Timing frame info: all important timestamps for a full lifetime of a
687   // single 'timing frame'.
688   absl::optional<webrtc::TimingFrameInfo> timing_frame_info;
689 };
690 
691 struct BandwidthEstimationInfo {
692   int available_send_bandwidth = 0;
693   int available_recv_bandwidth = 0;
694   int target_enc_bitrate = 0;
695   int actual_enc_bitrate = 0;
696   int retransmit_bitrate = 0;
697   int transmit_bitrate = 0;
698   int64_t bucket_delay = 0;
699 };
700 
701 // Maps from payload type to `RtpCodecParameters`.
702 typedef std::map<int, webrtc::RtpCodecParameters> RtpCodecParametersMap;
703 
704 struct VoiceMediaInfo {
705   VoiceMediaInfo();
706   ~VoiceMediaInfo();
ClearVoiceMediaInfo707   void Clear() {
708     senders.clear();
709     receivers.clear();
710     send_codecs.clear();
711     receive_codecs.clear();
712   }
713   std::vector<VoiceSenderInfo> senders;
714   std::vector<VoiceReceiverInfo> receivers;
715   RtpCodecParametersMap send_codecs;
716   RtpCodecParametersMap receive_codecs;
717   int32_t device_underrun_count = 0;
718 };
719 
720 struct VideoMediaInfo {
721   VideoMediaInfo();
722   ~VideoMediaInfo();
ClearVideoMediaInfo723   void Clear() {
724     senders.clear();
725     aggregated_senders.clear();
726     receivers.clear();
727     send_codecs.clear();
728     receive_codecs.clear();
729   }
730   // Each sender info represents one "outbound-rtp" stream.In non - simulcast,
731   // this means one info per RtpSender but if simulcast is used this means
732   // one info per simulcast layer.
733   std::vector<VideoSenderInfo> senders;
734   // Used for legacy getStats() API's "ssrc" stats and modern getStats() API's
735   // "track" stats. If simulcast is used, instead of having one sender info per
736   // simulcast layer, the metrics of all layers of an RtpSender are aggregated
737   // into a single sender info per RtpSender.
738   std::vector<VideoSenderInfo> aggregated_senders;
739   std::vector<VideoReceiverInfo> receivers;
740   RtpCodecParametersMap send_codecs;
741   RtpCodecParametersMap receive_codecs;
742 };
743 
744 struct RtcpParameters {
745   bool reduced_size = false;
746   bool remote_estimate = false;
747 };
748 
749 template <class Codec>
750 struct RtpParameters {
751   virtual ~RtpParameters() = default;
752 
753   std::vector<Codec> codecs;
754   std::vector<webrtc::RtpExtension> extensions;
755   // For a send stream this is true if we've neogtiated a send direction,
756   // for a receive stream this is true if we've negotiated a receive direction.
757   bool is_stream_active = true;
758 
759   // TODO(pthatcher): Add streams.
760   RtcpParameters rtcp;
761 
ToStringRtpParameters762   std::string ToString() const {
763     rtc::StringBuilder ost;
764     ost << "{";
765     const char* separator = "";
766     for (const auto& entry : ToStringMap()) {
767       ost << separator << entry.first << ": " << entry.second;
768       separator = ", ";
769     }
770     ost << "}";
771     return ost.Release();
772   }
773 
774  protected:
ToStringMapRtpParameters775   virtual std::map<std::string, std::string> ToStringMap() const {
776     return {{"codecs", VectorToString(codecs)},
777             {"extensions", VectorToString(extensions)}};
778   }
779 };
780 
781 // TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
782 // encapsulate all the parameters needed for an RtpSender.
783 template <class Codec>
784 struct RtpSendParameters : RtpParameters<Codec> {
785   int max_bandwidth_bps = -1;
786   // This is the value to be sent in the MID RTP header extension (if the header
787   // extension in included in the list of extensions).
788   std::string mid;
789   bool extmap_allow_mixed = false;
790 
791  protected:
ToStringMapRtpSendParameters792   std::map<std::string, std::string> ToStringMap() const override {
793     auto params = RtpParameters<Codec>::ToStringMap();
794     params["max_bandwidth_bps"] = rtc::ToString(max_bandwidth_bps);
795     params["mid"] = (mid.empty() ? "<not set>" : mid);
796     params["extmap-allow-mixed"] = extmap_allow_mixed ? "true" : "false";
797     return params;
798   }
799 };
800 
801 struct AudioSendParameters : RtpSendParameters<AudioCodec> {
802   AudioSendParameters();
803   ~AudioSendParameters() override;
804   AudioOptions options;
805 
806  protected:
807   std::map<std::string, std::string> ToStringMap() const override;
808 };
809 
810 struct AudioRecvParameters : RtpParameters<AudioCodec> {};
811 
812 class VoiceMediaChannel : public MediaChannel, public Delayable {
813  public:
814   VoiceMediaChannel(webrtc::TaskQueueBase* network_thread,
815                     bool enable_dscp = false)
MediaChannel(network_thread,enable_dscp)816       : MediaChannel(network_thread, enable_dscp) {}
~VoiceMediaChannel()817   ~VoiceMediaChannel() override {}
818 
819   cricket::MediaType media_type() const override;
820   virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
821   virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
822   // Get the receive parameters for the incoming stream identified by `ssrc`.
823   virtual webrtc::RtpParameters GetRtpReceiveParameters(
824       uint32_t ssrc) const = 0;
825   // Retrieve the receive parameters for the default receive
826   // stream, which is used when SSRCs are not signaled.
827   virtual webrtc::RtpParameters GetDefaultRtpReceiveParameters() const = 0;
828   // Starts or stops playout of received audio.
829   virtual void SetPlayout(bool playout) = 0;
830   // Starts or stops sending (and potentially capture) of local audio.
831   virtual void SetSend(bool send) = 0;
832   // Configure stream for sending.
833   virtual bool SetAudioSend(uint32_t ssrc,
834                             bool enable,
835                             const AudioOptions* options,
836                             AudioSource* source) = 0;
837   // Set speaker output volume of the specified ssrc.
838   virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
839   // Set speaker output volume for future unsignaled streams.
840   virtual bool SetDefaultOutputVolume(double volume) = 0;
841   // Returns if the telephone-event has been negotiated.
842   virtual bool CanInsertDtmf() = 0;
843   // Send a DTMF `event`. The DTMF out-of-band signal will be used.
844   // The `ssrc` should be either 0 or a valid send stream ssrc.
845   // The valid value for the `event` are 0 to 15 which corresponding to
846   // DTMF event 0-9, *, #, A-D.
847   virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
848   // Gets quality stats for the channel.
849   virtual bool GetStats(VoiceMediaInfo* info,
850                         bool get_and_clear_legacy_stats) = 0;
851 
852   virtual void SetRawAudioSink(
853       uint32_t ssrc,
854       std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
855   virtual void SetDefaultRawAudioSink(
856       std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
857 
858   virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
859 };
860 
861 // TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
862 // encapsulate all the parameters needed for a video RtpSender.
863 struct VideoSendParameters : RtpSendParameters<VideoCodec> {
864   VideoSendParameters();
865   ~VideoSendParameters() override;
866   // Use conference mode? This flag comes from the remote
867   // description's SDP line 'a=x-google-flag:conference', copied over
868   // by VideoChannel::SetRemoteContent_w, and ultimately used by
869   // conference mode screencast logic in
870   // WebRtcVideoChannel::WebRtcVideoSendStream::CreateVideoEncoderConfig.
871   // The special screencast behaviour is disabled by default.
872   bool conference_mode = false;
873 
874  protected:
875   std::map<std::string, std::string> ToStringMap() const override;
876 };
877 
878 // TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
879 // encapsulate all the parameters needed for a video RtpReceiver.
880 struct VideoRecvParameters : RtpParameters<VideoCodec> {};
881 
882 class VideoMediaChannel : public MediaChannel, public Delayable {
883  public:
884   explicit VideoMediaChannel(webrtc::TaskQueueBase* network_thread,
885                              bool enable_dscp = false)
MediaChannel(network_thread,enable_dscp)886       : MediaChannel(network_thread, enable_dscp) {}
~VideoMediaChannel()887   ~VideoMediaChannel() override {}
888 
889   cricket::MediaType media_type() const override;
890   virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
891   virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
892   // Get the receive parameters for the incoming stream identified by `ssrc`.
893   virtual webrtc::RtpParameters GetRtpReceiveParameters(
894       uint32_t ssrc) const = 0;
895   // Retrieve the receive parameters for the default receive
896   // stream, which is used when SSRCs are not signaled.
897   virtual webrtc::RtpParameters GetDefaultRtpReceiveParameters() const = 0;
898   // Gets the currently set codecs/payload types to be used for outgoing media.
899   virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
900   // Starts or stops transmission (and potentially capture) of local video.
901   virtual bool SetSend(bool send) = 0;
902   // Configure stream for sending and register a source.
903   // The `ssrc` must correspond to a registered send stream.
904   virtual bool SetVideoSend(
905       uint32_t ssrc,
906       const VideoOptions* options,
907       rtc::VideoSourceInterface<webrtc::VideoFrame>* source) = 0;
908   // Sets the sink object to be used for the specified stream.
909   virtual bool SetSink(uint32_t ssrc,
910                        rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) = 0;
911   // The sink is used for the 'default' stream.
912   virtual void SetDefaultSink(
913       rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) = 0;
914   // This fills the "bitrate parts" (rtx, video bitrate) of the
915   // BandwidthEstimationInfo, since that part that isn't possible to get
916   // through webrtc::Call::GetStats, as they are statistics of the send
917   // streams.
918   // TODO(holmer): We should change this so that either BWE graphs doesn't
919   // need access to bitrates of the streams, or change the (RTC)StatsCollector
920   // so that it's getting the send stream stats separately by calling
921   // GetStats(), and merges with BandwidthEstimationInfo by itself.
922   virtual void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) = 0;
923   // Gets quality stats for the channel.
924   virtual bool GetStats(VideoMediaInfo* info) = 0;
925   // Set recordable encoded frame callback for `ssrc`
926   virtual void SetRecordableEncodedFrameCallback(
927       uint32_t ssrc,
928       std::function<void(const webrtc::RecordableEncodedFrame&)> callback) = 0;
929   // Clear recordable encoded frame callback for `ssrc`
930   virtual void ClearRecordableEncodedFrameCallback(uint32_t ssrc) = 0;
931   // Request generation of a keyframe for `ssrc` on a receiving channel via
932   // RTCP feedback.
933   virtual void RequestRecvKeyFrame(uint32_t ssrc) = 0;
934   // Cause generation of a keyframe for `ssrc` on a sending channel.
935   virtual void GenerateSendKeyFrame(uint32_t ssrc,
936                                     const std::vector<std::string>& rids) = 0;
937 
938   virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
939 };
940 
941 // Info about data received in DataMediaChannel.  For use in
942 // DataMediaChannel::SignalDataReceived and in all of the signals that
943 // signal fires, on up the chain.
944 struct ReceiveDataParams {
945   // The in-packet stream indentifier.
946   // SCTP data channels use SIDs.
947   int sid = 0;
948   // The type of message (binary, text, or control).
949   webrtc::DataMessageType type = webrtc::DataMessageType::kText;
950   // A per-stream value incremented per packet in the stream.
951   int seq_num = 0;
952 };
953 
954 enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
955 
956 }  // namespace cricket
957 
958 #endif  // MEDIA_BASE_MEDIA_CHANNEL_H_
959