blob: 15721c59483ba6a7a3f7bd9221ad1229cf0ba44b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef MEDIA_BASE_MEDIA_CHANNEL_H_
12#define MEDIA_BASE_MEDIA_CHANNEL_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
Steve Antone78bcb92017-10-31 09:53:08 -070014#include <map>
kwiberg686a8ef2016-02-26 03:00:35 -080015#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
Patrik Höglundaba85d12017-11-28 15:46:08 +010017#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <vector>
19
Danil Chapovalov00c71832018-06-15 15:58:38 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/audio_codecs/audio_encoder.h"
Niels Möllera6fe2612018-01-19 11:28:54 +010022#include "api/audio_options.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/crypto/frame_decryptor_interface.h"
24#include "api/crypto/frame_encryptor_interface.h"
Anton Sukhanov98a462c2018-10-17 13:15:42 -070025#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/rtc_error.h"
27#include "api/rtp_parameters.h"
28#include "api/rtp_receiver_interface.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010029#include "api/video/video_content_type.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020030#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020031#include "api/video/video_source_interface.h"
32#include "api/video/video_timing.h"
33#include "api/video_codecs/video_encoder_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "media/base/codec.h"
Ruslan Burakov493a6502019-02-27 15:32:48 +010035#include "media/base/delayable.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/base/media_config.h"
37#include "media/base/media_constants.h"
38#include "media/base/stream_params.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010039#include "modules/audio_processing/include/audio_processing_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "rtc_base/async_packet_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/dscp.h"
44#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020048#include "rtc_base/strings/string_builder.h"
Artem Titove41c4332018-07-25 15:04:28 +020049#include "rtc_base/third_party/sigslot/sigslot.h"
Patrik Höglundaba85d12017-11-28 15:46:08 +010050
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052class Timing;
53}
54
Tommif888bb52015-12-12 01:37:01 +010055namespace webrtc {
56class AudioSinkInterface;
nisseacd935b2016-11-11 03:55:13 -080057class VideoFrame;
Yves Gerey665174f2018-06-19 15:03:05 +020058} // namespace webrtc
Tommif888bb52015-12-12 01:37:01 +010059
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060namespace cricket {
61
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080062class AudioSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063class VideoCapturer;
tommi1d5c19d2015-12-13 22:54:29 -080064struct RtpHeader;
65struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067const int kScreencastDefaultFps = 5;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069template <class T>
Danil Chapovalov00c71832018-06-15 15:58:38 +020070static std::string ToStringIfSet(const char* key,
71 const absl::optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070073 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 str = key;
75 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070076 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 str += ", ";
78 }
79 return str;
80}
81
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070082template <class T>
83static std::string VectorToString(const std::vector<T>& vals) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020084 rtc::StringBuilder ost; // no-presubmit-check TODO(webrtc:8982)
Yves Gerey665174f2018-06-19 15:03:05 +020085 ost << "[";
86 for (size_t i = 0; i < vals.size(); ++i) {
87 if (i > 0) {
88 ost << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070089 }
Yves Gerey665174f2018-06-19 15:03:05 +020090 ost << vals[i].ToString();
91 }
92 ost << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +020093 return ost.Release();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070094}
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
97// Used to be flags, but that makes it hard to selectively apply options.
98// We are moving all of the setting of options to structs like this,
99// but some things currently still use flags.
100struct VideoOptions {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200101 VideoOptions();
102 ~VideoOptions();
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700105 SetFrom(&video_noise_reduction, change.video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800106 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100107 SetFrom(&is_screencast, change.is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 }
109
110 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800111 return video_noise_reduction == o.video_noise_reduction &&
Niels Möller60653ba2016-03-02 11:41:36 +0100112 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
113 is_screencast == o.is_screencast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 }
deadbeef119760a2016-04-04 11:43:27 -0700115 bool operator!=(const VideoOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
117 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200118 rtc::StringBuilder ost;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 ost << ToStringIfSet("noise reduction", video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800121 ost << ToStringIfSet("screencast min bitrate kbps",
122 screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100123 ost << ToStringIfSet("is_screencast ", is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200125 return ost.Release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 }
127
nisseb163c3f2016-01-29 01:14:38 -0800128 // Enable denoising? This flag comes from the getUserMedia
eladalonf1841382017-06-12 01:16:46 -0700129 // constraint 'googNoiseReduction', and WebRtcVideoEngine passes it
nisseb163c3f2016-01-29 01:14:38 -0800130 // on to the codec options. Disabled by default.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200131 absl::optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800132 // Force screencast to use a minimum bitrate. This flag comes from
133 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
eladalonf1841382017-06-12 01:16:46 -0700134 // copied to the encoder config by WebRtcVideoChannel.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200135 absl::optional<int> screencast_min_bitrate_kbps;
Niels Möller60653ba2016-03-02 11:41:36 +0100136 // Set by screencast sources. Implies selection of encoding settings
137 // suitable for screencast. Most likely not the right way to do
138 // things, e.g., screencast of a text document and screencast of a
139 // youtube video have different needs.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200140 absl::optional<bool> is_screencast;
kwiberg102c6a62015-10-30 02:47:38 -0700141
142 private:
143 template <typename T>
Danil Chapovalov00c71832018-06-15 15:58:38 +0200144 static void SetFrom(absl::optional<T>* s, const absl::optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700145 if (o) {
146 *s = o;
147 }
148 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149};
150
isheriffa1c548b2016-05-31 16:12:24 -0700151// TODO(isheriff): Remove this once client usage is fixed to use RtpExtension.
152struct RtpHeaderExtension {
153 RtpHeaderExtension() : id(0) {}
154 RtpHeaderExtension(const std::string& uri, int id) : uri(uri), id(id) {}
155
156 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200157 rtc::StringBuilder ost;
isheriffa1c548b2016-05-31 16:12:24 -0700158 ost << "{";
159 ost << "uri: " << uri;
160 ost << ", id: " << id;
161 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200162 return ost.Release();
isheriffa1c548b2016-05-31 16:12:24 -0700163 }
164
165 std::string uri;
166 int id;
167};
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169class MediaChannel : public sigslot::has_slots<> {
170 public:
171 class NetworkInterface {
172 public:
173 enum SocketType { ST_RTP, ST_RTCP };
jbaucheec21bd2016-03-20 06:15:43 -0700174 virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700175 const rtc::PacketOptions& options) = 0;
jbaucheec21bd2016-03-20 06:15:43 -0700176 virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700177 const rtc::PacketOptions& options) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200178 virtual int SetOption(SocketType type,
179 rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 int option) = 0;
181 virtual ~NetworkInterface() {}
182 };
183
Benjamin Wright84583f62018-10-04 14:22:34 -0700184 explicit MediaChannel(const MediaConfig& config);
185 MediaChannel();
186 ~MediaChannel() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800188 virtual cricket::MediaType media_type() const = 0;
189
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700190 // Sets the abstract interface class for sending RTP/RTCP data and
191 // interface for media transport (experimental). If media transport is
192 // provided, it should be used instead of RTP/RTCP.
193 // TODO(sukhanov): Currently media transport can co-exist with RTP/RTCP, but
194 // in the future we will refactor code to send all frames with media
195 // transport.
196 virtual void SetInterface(NetworkInterface* iface,
197 webrtc::MediaTransportInterface* media_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 // Called when a RTP packet is received.
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700199 virtual void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100200 int64_t packet_time_us) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 // Called when a RTCP packet is received.
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700202 virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100203 int64_t packet_time_us) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // Called when the socket's ability to send has changed.
205 virtual void OnReadyToSend(bool ready) = 0;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700206 // Called when the network route used for sending packets changed.
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700207 virtual void OnNetworkRouteChanged(
208 const std::string& transport_name,
209 const rtc::NetworkRoute& network_route) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // Creates a new outgoing media stream with SSRCs and CNAME as described
211 // by sp.
212 virtual bool AddSendStream(const StreamParams& sp) = 0;
213 // Removes an outgoing media stream.
Seth Hampson5897a6e2018-04-03 11:16:33 -0700214 // SSRC must be the first SSRC of the media stream if the stream uses
215 // multiple SSRCs. In the case of an ssrc of 0, the possibly cached
216 // StreamParams is removed.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700218 // Creates a new incoming media stream with SSRCs, CNAME as described
219 // by sp. In the case of a sp without SSRCs, the unsignaled sp is cached
220 // to be used later for unsignaled streams received.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 virtual bool AddRecvStream(const StreamParams& sp) = 0;
222 // Removes an incoming media stream.
223 // ssrc must be the first SSRC of the media stream if the stream uses
224 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200225 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000226 // Returns the absoulte sendtime extension id value from media channel.
Paulina Hensman11b34f42018-04-09 14:24:52 +0200227 virtual int GetRtpSendTimeExtnId() const;
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700228 // Set the frame encryptor to use on all outgoing frames. This is optional.
229 // This pointers lifetime is managed by the set of RtpSender it is attached
230 // to.
Benjamin Wright84583f62018-10-04 14:22:34 -0700231 // TODO(benwright) make pure virtual once internal supports it.
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700232 virtual void SetFrameEncryptor(
Benjamin Wright84583f62018-10-04 14:22:34 -0700233 uint32_t ssrc,
234 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700235 // Set the frame decryptor to use on all incoming frames. This is optional.
236 // This pointers lifetimes is managed by the set of RtpReceivers it is
237 // attached to.
Benjamin Wright84583f62018-10-04 14:22:34 -0700238 // TODO(benwright) make pure virtual once internal supports it.
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700239 virtual void SetFrameDecryptor(
Benjamin Wright84583f62018-10-04 14:22:34 -0700240 uint32_t ssrc,
241 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000243 // Base method to send packet using NetworkInterface.
jbaucheec21bd2016-03-20 06:15:43 -0700244 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
245 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700246 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000247 }
248
jbaucheec21bd2016-03-20 06:15:43 -0700249 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
250 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700251 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000252 }
253
254 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000256 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000258 if (!network_interface_)
259 return -1;
260
261 return network_interface_->SetOption(type, opt, option);
262 }
263
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700264 webrtc::MediaTransportInterface* media_transport() {
265 return media_transport_;
266 }
267
Johannes Kron9190b822018-10-29 11:22:05 +0100268 // Corresponds to the SDP attribute extmap-allow-mixed, see RFC8285.
269 // Set to true if it's allowed to mix one- and two-byte RTP header extensions
270 // in the same stream. The setter and getter must only be called from
271 // worker_thread.
272 void SetExtmapAllowMixed(bool extmap_allow_mixed) {
273 extmap_allow_mixed_ = extmap_allow_mixed;
274 }
275 bool ExtmapAllowMixed() const { return extmap_allow_mixed_; }
276
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800277 virtual webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const = 0;
278 virtual webrtc::RTCError SetRtpSendParameters(
279 uint32_t ssrc,
280 const webrtc::RtpParameters& parameters) = 0;
281
Tim Haloun6ca98362018-09-17 17:06:08 -0700282 protected:
Tim Haloun6ca98362018-09-17 17:06:08 -0700283 bool DscpEnabled() const { return enable_dscp_; }
284
Steve Antone25f5952019-03-08 15:09:16 -0800285 // This is the DSCP value used for both RTP and RTCP channels if DSCP is
286 // enabled. It can be changed at any time via |SetPreferredDscp|.
287 rtc::DiffServCodePoint PreferredDscp() const {
288 rtc::CritScope cs(&network_interface_crit_);
289 return preferred_dscp_;
290 }
291
292 int SetPreferredDscp(rtc::DiffServCodePoint preferred_dscp) {
293 rtc::CritScope cs(&network_interface_crit_);
294 if (preferred_dscp == preferred_dscp_) {
295 return 0;
296 }
297 preferred_dscp_ = preferred_dscp;
298 return UpdateDscp();
299 }
300
301 private:
302 // Apply the preferred DSCP setting to the underlying network interface RTP
303 // and RTCP channels. If DSCP is disabled, then apply the default DSCP value.
304 int UpdateDscp() RTC_EXCLUSIVE_LOCKS_REQUIRED(network_interface_crit_) {
Tim Haloun648d28a2018-10-18 16:52:22 -0700305 rtc::DiffServCodePoint value =
Steve Antone25f5952019-03-08 15:09:16 -0800306 enable_dscp_ ? preferred_dscp_ : rtc::DSCP_DEFAULT;
307 int ret = SetOption(NetworkInterface::ST_RTP, rtc::Socket::OPT_DSCP, value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000308 if (ret == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200309 ret = SetOption(NetworkInterface::ST_RTCP, rtc::Socket::OPT_DSCP, value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000310 }
311 return ret;
312 }
313
jbaucheec21bd2016-03-20 06:15:43 -0700314 bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700315 bool rtcp,
316 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000317 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000318 if (!network_interface_)
319 return false;
320
stefanc1aeaf02015-10-15 07:26:07 -0700321 return (!rtcp) ? network_interface_->SendPacket(packet, options)
322 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000323 }
324
nisse51542be2016-02-12 02:27:06 -0800325 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000326 // |network_interface_| can be accessed from the worker_thread and
327 // from any MediaEngine threads. This critical section is to protect accessing
328 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000329 rtc::CriticalSection network_interface_crit_;
Steve Antone25f5952019-03-08 15:09:16 -0800330 NetworkInterface* network_interface_ RTC_GUARDED_BY(network_interface_crit_) =
331 nullptr;
332 rtc::DiffServCodePoint preferred_dscp_
333 RTC_GUARDED_BY(network_interface_crit_) = rtc::DSCP_DEFAULT;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700334 webrtc::MediaTransportInterface* media_transport_ = nullptr;
Johannes Kron9190b822018-10-29 11:22:05 +0100335 bool extmap_allow_mixed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336};
337
wu@webrtc.org97077a32013-10-25 21:18:33 +0000338// The stats information is structured as follows:
339// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
340// Media contains a vector of SSRC infos that are exclusively used by this
341// media. (SSRCs shared between media streams can't be represented.)
342
343// Information about an SSRC.
344// This data may be locally recorded, or received in an RTCP SR or RR.
345struct SsrcSenderInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800346 uint32_t ssrc = 0;
347 double timestamp = 0.0; // NTP timestamp, represented as seconds since epoch.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000348};
349
350struct SsrcReceiverInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800351 uint32_t ssrc = 0;
352 double timestamp = 0.0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000353};
354
355struct MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200356 MediaSenderInfo();
357 ~MediaSenderInfo();
Yves Gerey665174f2018-06-19 15:03:05 +0200358 void add_ssrc(const SsrcSenderInfo& stat) { local_stats.push_back(stat); }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000359 // Temporary utility function for call sites that only provide SSRC.
360 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200361 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000362 SsrcSenderInfo stat;
363 stat.ssrc = ssrc;
364 add_ssrc(stat);
365 }
366 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200367 std::vector<uint32_t> ssrcs() const {
368 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000369 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
370 it != local_stats.end(); ++it) {
371 retval.push_back(it->ssrc);
372 }
373 return retval;
374 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100375 // Returns true if the media has been connected.
376 bool connected() const { return local_stats.size() > 0; }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000377 // Utility accessor for clients that make the assumption only one ssrc
378 // exists per media.
379 // This will eventually go away.
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100380 // Call sites that compare this to zero should use connected() instead.
381 // https://bugs.webrtc.org/8694
Peter Boström0c4e06b2015-10-07 12:23:21 +0200382 uint32_t ssrc() const {
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100383 if (connected()) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000384 return local_stats[0].ssrc;
385 } else {
386 return 0;
387 }
388 }
Steve Anton002f9212018-01-09 16:38:15 -0800389 int64_t bytes_sent = 0;
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200390 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedbytessent
391 uint64_t retransmitted_bytes_sent = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800392 int packets_sent = 0;
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200393 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedpacketssent
394 uint64_t retransmitted_packets_sent = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800395 int packets_lost = 0;
396 float fraction_lost = 0.0f;
397 int64_t rtt_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000398 std::string codec_name;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200399 absl::optional<int> codec_payload_type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000400 std::vector<SsrcSenderInfo> local_stats;
401 std::vector<SsrcReceiverInfo> remote_stats;
402};
403
404struct MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200405 MediaReceiverInfo();
406 ~MediaReceiverInfo();
Yves Gerey665174f2018-06-19 15:03:05 +0200407 void add_ssrc(const SsrcReceiverInfo& stat) { local_stats.push_back(stat); }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000408 // Temporary utility function for call sites that only provide SSRC.
409 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200410 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000411 SsrcReceiverInfo stat;
412 stat.ssrc = ssrc;
413 add_ssrc(stat);
414 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200415 std::vector<uint32_t> ssrcs() const {
416 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000417 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
418 it != local_stats.end(); ++it) {
419 retval.push_back(it->ssrc);
420 }
421 return retval;
422 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100423 // Returns true if the media has been connected.
424 bool connected() const { return local_stats.size() > 0; }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000425 // Utility accessor for clients that make the assumption only one ssrc
426 // exists per media.
427 // This will eventually go away.
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100428 // Call sites that compare this to zero should use connected();
429 // https://bugs.webrtc.org/8694
Peter Boström0c4e06b2015-10-07 12:23:21 +0200430 uint32_t ssrc() const {
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100431 if (connected()) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000432 return local_stats[0].ssrc;
433 } else {
434 return 0;
435 }
436 }
437
Steve Anton002f9212018-01-09 16:38:15 -0800438 int64_t bytes_rcvd = 0;
439 int packets_rcvd = 0;
440 int packets_lost = 0;
441 float fraction_lost = 0.0f;
Henrik Boström01738c62019-04-15 17:32:00 +0200442 // The timestamp at which the last packet was received, i.e. the time of the
443 // local clock when it was received - not the RTP timestamp of that packet.
444 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-lastpacketreceivedtimestamp
445 absl::optional<int64_t> last_packet_received_timestamp_ms;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000446 std::string codec_name;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200447 absl::optional<int> codec_payload_type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000448 std::vector<SsrcReceiverInfo> local_stats;
449 std::vector<SsrcSenderInfo> remote_stats;
450};
451
452struct VoiceSenderInfo : public MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200453 VoiceSenderInfo();
454 ~VoiceSenderInfo();
Steve Anton002f9212018-01-09 16:38:15 -0800455 int ext_seqnum = 0;
456 int jitter_ms = 0;
457 int audio_level = 0;
zsteine76bd3a2017-07-14 12:17:49 -0700458 // See description of "totalAudioEnergy" in the WebRTC stats spec:
459 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
Steve Anton002f9212018-01-09 16:38:15 -0800460 double total_input_energy = 0.0;
461 double total_input_duration = 0.0;
Steve Anton002f9212018-01-09 16:38:15 -0800462 bool typing_noise_detected = false;
ivoce1198e02017-09-08 08:13:19 -0700463 webrtc::ANAStats ana_statistics;
Ivo Creusen56d46092017-11-24 17:29:59 +0100464 webrtc::AudioProcessingStats apm_statistics;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465};
466
wu@webrtc.org97077a32013-10-25 21:18:33 +0000467struct VoiceReceiverInfo : public MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200468 VoiceReceiverInfo();
469 ~VoiceReceiverInfo();
Steve Anton002f9212018-01-09 16:38:15 -0800470 int ext_seqnum = 0;
471 int jitter_ms = 0;
472 int jitter_buffer_ms = 0;
473 int jitter_buffer_preferred_ms = 0;
474 int delay_estimate_ms = 0;
475 int audio_level = 0;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200476 // Stats below correspond to similarly-named fields in the WebRTC stats spec.
477 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats
Steve Anton002f9212018-01-09 16:38:15 -0800478 double total_output_energy = 0.0;
479 uint64_t total_samples_received = 0;
480 double total_output_duration = 0.0;
481 uint64_t concealed_samples = 0;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200482 uint64_t silent_concealed_samples = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800483 uint64_t concealment_events = 0;
Chen Xing0acffb52019-01-15 15:46:29 +0100484 double jitter_buffer_delay_seconds = 0.0;
485 uint64_t jitter_buffer_emitted_count = 0;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200486 uint64_t inserted_samples_for_deceleration = 0;
487 uint64_t removed_samples_for_acceleration = 0;
488 uint64_t fec_packets_received = 0;
489 uint64_t fec_packets_discarded = 0;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200490 // Stats below DO NOT correspond directly to anything in the WebRTC stats
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000491 // fraction of synthesized audio inserted through expansion.
Steve Anton002f9212018-01-09 16:38:15 -0800492 float expand_rate = 0.0f;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000493 // fraction of synthesized speech inserted through expansion.
Steve Anton002f9212018-01-09 16:38:15 -0800494 float speech_expand_rate = 0.0f;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000495 // fraction of data out of secondary decoding, including FEC and RED.
Steve Anton002f9212018-01-09 16:38:15 -0800496 float secondary_decoded_rate = 0.0f;
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200497 // Fraction of secondary data, including FEC and RED, that is discarded.
498 // Discarding of secondary data can be caused by the reception of the primary
499 // data, obsoleting the secondary data. It can also be caused by early
500 // or late arrival of secondary data. This metric is the percentage of
501 // discarded secondary data since last query of receiver info.
Steve Anton002f9212018-01-09 16:38:15 -0800502 float secondary_discarded_rate = 0.0f;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200503 // Fraction of data removed through time compression.
Steve Anton002f9212018-01-09 16:38:15 -0800504 float accelerate_rate = 0.0f;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200505 // Fraction of data inserted through time stretching.
Steve Anton002f9212018-01-09 16:38:15 -0800506 float preemptive_expand_rate = 0.0f;
507 int decoding_calls_to_silence_generator = 0;
508 int decoding_calls_to_neteq = 0;
509 int decoding_normal = 0;
510 int decoding_plc = 0;
511 int decoding_cng = 0;
512 int decoding_plc_cng = 0;
513 int decoding_muted_output = 0;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000514 // Estimated capture start time in NTP time in ms.
Steve Anton002f9212018-01-09 16:38:15 -0800515 int64_t capture_start_ntp_time_ms = -1;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100516 // Count of the number of buffer flushes.
517 uint64_t jitter_buffer_flushes = 0;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100518 // Number of samples expanded due to delayed packets.
519 uint64_t delayed_packet_outage_samples = 0;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100520 // Arrival delay of received audio packets.
521 double relative_packet_arrival_delay_seconds = 0.0;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200522 // Count and total duration of audio interruptions (loss-concealement periods
523 // longer than 150 ms).
524 int32_t interruption_count = 0;
525 int32_t total_interruption_duration_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526};
527
wu@webrtc.org97077a32013-10-25 21:18:33 +0000528struct VideoSenderInfo : public MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200529 VideoSenderInfo();
530 ~VideoSenderInfo();
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000531 std::vector<SsrcGroup> ssrc_groups;
hbosa65704b2016-11-14 02:28:16 -0800532 // TODO(hbos): Move this to |VideoMediaInfo::send_codecs|?
Peter Boströmb7d9a972015-12-18 16:01:11 +0100533 std::string encoder_implementation_name;
Steve Anton002f9212018-01-09 16:38:15 -0800534 int firs_rcvd = 0;
535 int plis_rcvd = 0;
536 int nacks_rcvd = 0;
537 int send_frame_width = 0;
538 int send_frame_height = 0;
539 int framerate_input = 0;
540 int framerate_sent = 0;
541 int nominal_bitrate = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800542 int adapt_reason = 0;
543 int adapt_changes = 0;
544 int avg_encode_ms = 0;
545 int encode_usage_percent = 0;
546 uint32_t frames_encoded = 0;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200547 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
548 uint64_t total_encode_time_ms = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800549 bool has_entered_low_resolution = false;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200550 absl::optional<uint64_t> qp_sum;
Steve Anton002f9212018-01-09 16:38:15 -0800551 webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100552 // https://w3c.github.io/webrtc-stats/#dom-rtcvideosenderstats-hugeframessent
553 uint32_t huge_frames_sent = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554};
555
wu@webrtc.org97077a32013-10-25 21:18:33 +0000556struct VideoReceiverInfo : public MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200557 VideoReceiverInfo();
558 ~VideoReceiverInfo();
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000559 std::vector<SsrcGroup> ssrc_groups;
hbosa65704b2016-11-14 02:28:16 -0800560 // TODO(hbos): Move this to |VideoMediaInfo::receive_codecs|?
Peter Boströmb7d9a972015-12-18 16:01:11 +0100561 std::string decoder_implementation_name;
Steve Anton002f9212018-01-09 16:38:15 -0800562 int packets_concealed = 0;
563 int firs_sent = 0;
564 int plis_sent = 0;
565 int nacks_sent = 0;
566 int frame_width = 0;
567 int frame_height = 0;
568 int framerate_rcvd = 0;
569 int framerate_decoded = 0;
570 int framerate_output = 0;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000571 // Framerate as sent to the renderer.
Steve Anton002f9212018-01-09 16:38:15 -0800572 int framerate_render_input = 0;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000573 // Framerate that the renderer reports.
Steve Anton002f9212018-01-09 16:38:15 -0800574 int framerate_render_output = 0;
575 uint32_t frames_received = 0;
576 uint32_t frames_decoded = 0;
577 uint32_t frames_rendered = 0;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200578 absl::optional<uint64_t> qp_sum;
Steve Anton002f9212018-01-09 16:38:15 -0800579 int64_t interframe_delay_max_ms = -1;
Sergey Silkin02371062019-01-31 16:45:42 +0100580 uint32_t freeze_count = 0;
581 uint32_t pause_count = 0;
582 uint32_t total_freezes_duration_ms = 0;
583 uint32_t total_pauses_duration_ms = 0;
584 uint32_t total_frames_duration_ms = 0;
585 double sum_squared_frame_durations = 0.0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000586
Steve Anton002f9212018-01-09 16:38:15 -0800587 webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
ilnik2e1b40b2017-09-04 07:57:17 -0700588
wu@webrtc.org97077a32013-10-25 21:18:33 +0000589 // All stats below are gathered per-VideoReceiver, but some will be correlated
590 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
591 // structures, reflect this in the new layout.
592
593 // Current frame decode latency.
Steve Anton002f9212018-01-09 16:38:15 -0800594 int decode_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000595 // Maximum observed frame decode latency.
Steve Anton002f9212018-01-09 16:38:15 -0800596 int max_decode_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000597 // Jitter (network-related) latency.
Steve Anton002f9212018-01-09 16:38:15 -0800598 int jitter_buffer_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000599 // Requested minimum playout latency.
Steve Anton002f9212018-01-09 16:38:15 -0800600 int min_playout_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000601 // Requested latency to account for rendering delay.
Steve Anton002f9212018-01-09 16:38:15 -0800602 int render_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000603 // Target overall delay: network+decode+render, accounting for
604 // min_playout_delay_ms.
Steve Anton002f9212018-01-09 16:38:15 -0800605 int target_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000606 // Current overall delay, possibly ramping towards target_delay_ms.
Steve Anton002f9212018-01-09 16:38:15 -0800607 int current_delay_ms = 0;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000608
609 // Estimated capture start time in NTP time in ms.
Steve Anton002f9212018-01-09 16:38:15 -0800610 int64_t capture_start_ntp_time_ms = -1;
ilnik2edc6842017-07-06 03:06:50 -0700611
Benjamin Wright514f0842018-12-10 09:55:17 -0800612 // First frame received to first frame decoded latency.
613 int64_t first_frame_received_to_decoded_ms = -1;
614
ilnik2edc6842017-07-06 03:06:50 -0700615 // Timing frame info: all important timestamps for a full lifetime of a
616 // single 'timing frame'.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200617 absl::optional<webrtc::TimingFrameInfo> timing_frame_info;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618};
619
wu@webrtc.org97077a32013-10-25 21:18:33 +0000620struct DataSenderInfo : public MediaSenderInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800621 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622};
623
wu@webrtc.org97077a32013-10-25 21:18:33 +0000624struct DataReceiverInfo : public MediaReceiverInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800625 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626};
627
628struct BandwidthEstimationInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800629 int available_send_bandwidth = 0;
630 int available_recv_bandwidth = 0;
631 int target_enc_bitrate = 0;
632 int actual_enc_bitrate = 0;
633 int retransmit_bitrate = 0;
634 int transmit_bitrate = 0;
635 int64_t bucket_delay = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636};
637
hbosa65704b2016-11-14 02:28:16 -0800638// Maps from payload type to |RtpCodecParameters|.
639typedef std::map<int, webrtc::RtpCodecParameters> RtpCodecParametersMap;
640
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641struct VoiceMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200642 VoiceMediaInfo();
643 ~VoiceMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 void Clear() {
645 senders.clear();
646 receivers.clear();
hbos1acfbd22016-11-17 23:43:29 -0800647 send_codecs.clear();
648 receive_codecs.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 }
650 std::vector<VoiceSenderInfo> senders;
651 std::vector<VoiceReceiverInfo> receivers;
hbos1acfbd22016-11-17 23:43:29 -0800652 RtpCodecParametersMap send_codecs;
653 RtpCodecParametersMap receive_codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654};
655
656struct VideoMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200657 VideoMediaInfo();
658 ~VideoMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 void Clear() {
660 senders.clear();
661 receivers.clear();
charujaind72098a2017-06-01 08:54:47 -0700662 bw_estimations.clear();
hbosa65704b2016-11-14 02:28:16 -0800663 send_codecs.clear();
664 receive_codecs.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 }
666 std::vector<VideoSenderInfo> senders;
667 std::vector<VideoReceiverInfo> receivers;
stefanf79ade12017-06-02 06:44:03 -0700668 // Deprecated.
669 // TODO(holmer): Remove once upstream projects no longer use this.
charujaind72098a2017-06-01 08:54:47 -0700670 std::vector<BandwidthEstimationInfo> bw_estimations;
hbosa65704b2016-11-14 02:28:16 -0800671 RtpCodecParametersMap send_codecs;
672 RtpCodecParametersMap receive_codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673};
674
675struct DataMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200676 DataMediaInfo();
677 ~DataMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 void Clear() {
679 senders.clear();
680 receivers.clear();
681 }
682 std::vector<DataSenderInfo> senders;
683 std::vector<DataReceiverInfo> receivers;
684};
685
deadbeef13871492015-12-09 12:37:51 -0800686struct RtcpParameters {
687 bool reduced_size = false;
688};
689
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700690template <class Codec>
691struct RtpParameters {
Steve Anton003930a2018-03-29 12:37:21 -0700692 virtual ~RtpParameters() = default;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700693
694 std::vector<Codec> codecs;
isheriff6f8d6862016-05-26 11:24:55 -0700695 std::vector<webrtc::RtpExtension> extensions;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700696 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800697 RtcpParameters rtcp;
Steve Anton003930a2018-03-29 12:37:21 -0700698
699 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200700 rtc::StringBuilder ost;
Steve Anton003930a2018-03-29 12:37:21 -0700701 ost << "{";
702 const char* separator = "";
703 for (const auto& entry : ToStringMap()) {
704 ost << separator << entry.first << ": " << entry.second;
705 separator = ", ";
706 }
707 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200708 return ost.Release();
Steve Anton003930a2018-03-29 12:37:21 -0700709 }
710
711 protected:
712 virtual std::map<std::string, std::string> ToStringMap() const {
713 return {{"codecs", VectorToString(codecs)},
714 {"extensions", VectorToString(extensions)}};
715 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700716};
717
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700718// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
719// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700720template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700721struct RtpSendParameters : RtpParameters<Codec> {
nisse05103312016-03-16 02:22:50 -0700722 int max_bandwidth_bps = -1;
Steve Antonbb50ce52018-03-26 10:24:32 -0700723 // This is the value to be sent in the MID RTP header extension (if the header
724 // extension in included in the list of extensions).
725 std::string mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100726 bool extmap_allow_mixed = false;
Steve Anton003930a2018-03-29 12:37:21 -0700727
728 protected:
729 std::map<std::string, std::string> ToStringMap() const override {
730 auto params = RtpParameters<Codec>::ToStringMap();
731 params["max_bandwidth_bps"] = rtc::ToString(max_bandwidth_bps);
732 params["mid"] = (mid.empty() ? "<not set>" : mid);
Johannes Kron9190b822018-10-29 11:22:05 +0100733 params["extmap-allow-mixed"] = extmap_allow_mixed ? "true" : "false";
Steve Anton003930a2018-03-29 12:37:21 -0700734 return params;
735 }
nisse05103312016-03-16 02:22:50 -0700736};
737
738struct AudioSendParameters : RtpSendParameters<AudioCodec> {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200739 AudioSendParameters();
740 ~AudioSendParameters() override;
nisse05103312016-03-16 02:22:50 -0700741 AudioOptions options;
Steve Anton003930a2018-03-29 12:37:21 -0700742
743 protected:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200744 std::map<std::string, std::string> ToStringMap() const override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700745};
746
Yves Gerey665174f2018-06-19 15:03:05 +0200747struct AudioRecvParameters : RtpParameters<AudioCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700748
Ruslan Burakov493a6502019-02-27 15:32:48 +0100749class VoiceMediaChannel : public MediaChannel, public Delayable {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 VoiceMediaChannel() {}
terelius54f91712016-06-01 11:18:56 -0700752 explicit VoiceMediaChannel(const MediaConfig& config)
753 : MediaChannel(config) {}
Paulina Hensman11b34f42018-04-09 14:24:52 +0200754 ~VoiceMediaChannel() override {}
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800755
756 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200757 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
758 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
deadbeef3bc15102017-04-20 19:25:07 -0700759 // Get the receive parameters for the incoming stream identified by |ssrc|.
760 // If |ssrc| is 0, retrieve the receive parameters for the default receive
761 // stream, which is used when SSRCs are not signaled. Note that calling with
762 // an |ssrc| of 0 will return encoding parameters with an unset |ssrc|
763 // member.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700764 virtual webrtc::RtpParameters GetRtpReceiveParameters(
765 uint32_t ssrc) const = 0;
766 virtual bool SetRtpReceiveParameters(
767 uint32_t ssrc,
768 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // Starts or stops playout of received audio.
aleloi84ef6152016-08-04 05:28:21 -0700770 virtual void SetPlayout(bool playout) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800772 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700773 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200774 virtual bool SetAudioSend(uint32_t ssrc,
775 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700776 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800777 AudioSource* source) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700778 // Set speaker output volume of the specified ssrc.
779 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800781 virtual bool CanInsertDtmf() = 0;
782 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000784 // The valid value for the |event| are 0 to 15 which corresponding to
785 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800786 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 // Gets quality stats for the channel.
788 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100789
790 virtual void SetRawAudioSink(
791 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800792 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
zhihuang38ede132017-06-15 12:52:32 -0700793
794 virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795};
796
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700797// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
798// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700799struct VideoSendParameters : RtpSendParameters<VideoCodec> {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200800 VideoSendParameters();
801 ~VideoSendParameters() override;
nisse4b4dc862016-02-17 05:25:36 -0800802 // Use conference mode? This flag comes from the remote
803 // description's SDP line 'a=x-google-flag:conference', copied over
804 // by VideoChannel::SetRemoteContent_w, and ultimately used by
805 // conference mode screencast logic in
eladalonf1841382017-06-12 01:16:46 -0700806 // WebRtcVideoChannel::WebRtcVideoSendStream::CreateVideoEncoderConfig.
nisse4b4dc862016-02-17 05:25:36 -0800807 // The special screencast behaviour is disabled by default.
808 bool conference_mode = false;
Steve Anton003930a2018-03-29 12:37:21 -0700809
810 protected:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200811 std::map<std::string, std::string> ToStringMap() const override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700812};
813
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700814// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
815// encapsulate all the parameters needed for a video RtpReceiver.
Yves Gerey665174f2018-06-19 15:03:05 +0200816struct VideoRecvParameters : RtpParameters<VideoCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700817
Ruslan Burakov493a6502019-02-27 15:32:48 +0100818class VideoMediaChannel : public MediaChannel, public Delayable {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 public:
nisse08582ff2016-02-04 01:24:52 -0800820 VideoMediaChannel() {}
terelius54f91712016-06-01 11:18:56 -0700821 explicit VideoMediaChannel(const MediaConfig& config)
822 : MediaChannel(config) {}
Paulina Hensman11b34f42018-04-09 14:24:52 +0200823 ~VideoMediaChannel() override {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200824
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800825 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200826 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
827 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
deadbeef3bc15102017-04-20 19:25:07 -0700828 // Get the receive parameters for the incoming stream identified by |ssrc|.
829 // If |ssrc| is 0, retrieve the receive parameters for the default receive
830 // stream, which is used when SSRCs are not signaled. Note that calling with
831 // an |ssrc| of 0 will return encoding parameters with an unset |ssrc|
832 // member.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700833 virtual webrtc::RtpParameters GetRtpReceiveParameters(
834 uint32_t ssrc) const = 0;
835 virtual bool SetRtpReceiveParameters(
836 uint32_t ssrc,
837 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 // Gets the currently set codecs/payload types to be used for outgoing media.
839 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 // Starts or stops transmission (and potentially capture) of local video.
841 virtual bool SetSend(bool send) = 0;
deadbeef5a4a75a2016-06-02 16:23:38 -0700842 // Configure stream for sending and register a source.
843 // The |ssrc| must correspond to a registered send stream.
844 virtual bool SetVideoSend(
845 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -0700846 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800847 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) = 0;
nisse08582ff2016-02-04 01:24:52 -0800848 // Sets the sink object to be used for the specified stream.
deadbeef3bc15102017-04-20 19:25:07 -0700849 // If SSRC is 0, the sink is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -0800850 virtual bool SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -0800851 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) = 0;
stefanf79ade12017-06-02 06:44:03 -0700852 // This fills the "bitrate parts" (rtx, video bitrate) of the
853 // BandwidthEstimationInfo, since that part that isn't possible to get
854 // through webrtc::Call::GetStats, as they are statistics of the send
855 // streams.
856 // TODO(holmer): We should change this so that either BWE graphs doesn't
857 // need access to bitrates of the streams, or change the (RTC)StatsCollector
858 // so that it's getting the send stream stats separately by calling
859 // GetStats(), and merges with BandwidthEstimationInfo by itself.
860 virtual void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000862 virtual bool GetStats(VideoMediaInfo* info) = 0;
Jonas Oreland49ac5952018-09-26 16:04:32 +0200863
864 virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865};
866
867enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000868 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
869 // values.
870 DMT_NONE = 0,
871 DMT_CONTROL = 1,
872 DMT_BINARY = 2,
873 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874};
875
876// Info about data received in DataMediaChannel. For use in
877// DataMediaChannel::SignalDataReceived and in all of the signals that
878// signal fires, on up the chain.
879struct ReceiveDataParams {
880 // The in-packet stream indentifier.
deadbeef953c2ce2017-01-09 14:53:41 -0800881 // RTP data channels use SSRCs, SCTP data channels use SIDs.
882 union {
883 uint32_t ssrc;
Steve Anton002f9212018-01-09 16:38:15 -0800884 int sid = 0;
deadbeef953c2ce2017-01-09 14:53:41 -0800885 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 // The type of message (binary, text, or control).
Steve Anton002f9212018-01-09 16:38:15 -0800887 DataMessageType type = DMT_TEXT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 // A per-stream value incremented per packet in the stream.
Steve Anton002f9212018-01-09 16:38:15 -0800889 int seq_num = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 // A per-stream value monotonically increasing with time.
Steve Anton002f9212018-01-09 16:38:15 -0800891 int timestamp = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892};
893
894struct SendDataParams {
895 // The in-packet stream indentifier.
deadbeef953c2ce2017-01-09 14:53:41 -0800896 // RTP data channels use SSRCs, SCTP data channels use SIDs.
897 union {
898 uint32_t ssrc;
Steve Anton002f9212018-01-09 16:38:15 -0800899 int sid = 0;
deadbeef953c2ce2017-01-09 14:53:41 -0800900 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 // The type of message (binary, text, or control).
Steve Anton002f9212018-01-09 16:38:15 -0800902 DataMessageType type = DMT_TEXT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903
Steve Anton002f9212018-01-09 16:38:15 -0800904 // TODO(pthatcher): Make |ordered| and |reliable| true by default?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 // For SCTP, whether to send messages flagged as ordered or not.
906 // If false, messages can be received out of order.
Steve Anton002f9212018-01-09 16:38:15 -0800907 bool ordered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 // For SCTP, whether the messages are sent reliably or not.
909 // If false, messages may be lost.
Steve Anton002f9212018-01-09 16:38:15 -0800910 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 // For SCTP, if reliable == false, provide partial reliability by
912 // resending up to this many times. Either count or millis
913 // is supported, not both at the same time.
Steve Anton002f9212018-01-09 16:38:15 -0800914 int max_rtx_count = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 // For SCTP, if reliable == false, provide partial reliability by
916 // resending for up to this many milliseconds. Either count or millis
917 // is supported, not both at the same time.
Steve Anton002f9212018-01-09 16:38:15 -0800918 int max_rtx_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919};
920
921enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
922
Yves Gerey665174f2018-06-19 15:03:05 +0200923struct DataSendParameters : RtpSendParameters<DataCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700924
Yves Gerey665174f2018-06-19 15:03:05 +0200925struct DataRecvParameters : RtpParameters<DataCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927class DataMediaChannel : public MediaChannel {
928 public:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200929 DataMediaChannel();
930 explicit DataMediaChannel(const MediaConfig& config);
931 ~DataMediaChannel() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800933 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200934 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
935 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000936
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800937 // RtpParameter methods are not supported for Data channel.
938 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
939 webrtc::RTCError SetRtpSendParameters(
940 uint32_t ssrc,
941 const webrtc::RtpParameters& parameters) override;
942
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 // TODO(pthatcher): Implement this.
Paulina Hensman11b34f42018-04-09 14:24:52 +0200944 virtual bool GetStats(DataMediaInfo* info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945
946 virtual bool SetSend(bool send) = 0;
947 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948
Paulina Hensman11b34f42018-04-09 14:24:52 +0200949 void OnNetworkRouteChanged(const std::string& transport_name,
950 const rtc::NetworkRoute& network_route) override {}
Honghai Zhangcc411c02016-03-29 17:27:21 -0700951
Yves Gerey665174f2018-06-19 15:03:05 +0200952 virtual bool SendData(const SendDataParams& params,
953 const rtc::CopyOnWriteBuffer& payload,
954 SendDataResult* result = NULL) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 // Signals when data is received (params, data, len)
Yves Gerey665174f2018-06-19 15:03:05 +0200956 sigslot::signal3<const ReceiveDataParams&, const char*, size_t>
957 SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000958 // Signal when the media channel is ready to send the stream. Arguments are:
959 // writable(bool)
960 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961};
962
963} // namespace cricket
964
Steve Anton10542f22019-01-11 09:11:00 -0800965#endif // MEDIA_BASE_MEDIA_CHANNEL_H_