blob: c991de39ea26a88b664101d67b17dcc5a8f4abd0 [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 Sukhanov4f08faa2019-05-21 11:12:57 -070025#include "api/media_transport_config.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"
Henrik Boströmce33b6a2019-05-28 17:42:38 +020034#include "common_video/include/quality_limitation_reason.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "media/base/codec.h"
Ruslan Burakov493a6502019-02-27 15:32:48 +010036#include "media/base/delayable.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "media/base/media_config.h"
38#include "media/base/media_constants.h"
39#include "media/base/stream_params.h"
Ivo Creusen56d46092017-11-24 17:29:59 +010040#include "modules/audio_processing/include/audio_processing_statistics.h"
Henrik Boström87e3f9d2019-05-27 10:44:24 +020041#include "modules/rtp_rtcp/include/report_block_data.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/async_packet_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/copy_on_write_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/dscp.h"
46#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020050#include "rtc_base/strings/string_builder.h"
Artem Titove41c4332018-07-25 15:04:28 +020051#include "rtc_base/third_party/sigslot/sigslot.h"
Patrik Höglundaba85d12017-11-28 15:46:08 +010052
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054class Timing;
55}
56
Tommif888bb52015-12-12 01:37:01 +010057namespace webrtc {
58class AudioSinkInterface;
nisseacd935b2016-11-11 03:55:13 -080059class VideoFrame;
Yves Gerey665174f2018-06-19 15:03:05 +020060} // namespace webrtc
Tommif888bb52015-12-12 01:37:01 +010061
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062namespace cricket {
63
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080064class AudioSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065class VideoCapturer;
tommi1d5c19d2015-12-13 22:54:29 -080066struct RtpHeader;
67struct VideoFormat;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069const int kScreencastDefaultFps = 5;
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071template <class T>
Danil Chapovalov00c71832018-06-15 15:58:38 +020072static std::string ToStringIfSet(const char* key,
73 const absl::optional<T>& val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 std::string str;
kwiberg102c6a62015-10-30 02:47:38 -070075 if (val) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 str = key;
77 str += ": ";
kwiberg102c6a62015-10-30 02:47:38 -070078 str += val ? rtc::ToString(*val) : "";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 str += ", ";
80 }
81 return str;
82}
83
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070084template <class T>
85static std::string VectorToString(const std::vector<T>& vals) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020086 rtc::StringBuilder ost; // no-presubmit-check TODO(webrtc:8982)
Yves Gerey665174f2018-06-19 15:03:05 +020087 ost << "[";
88 for (size_t i = 0; i < vals.size(); ++i) {
89 if (i > 0) {
90 ost << ", ";
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070091 }
Yves Gerey665174f2018-06-19 15:03:05 +020092 ost << vals[i].ToString();
93 }
94 ost << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +020095 return ost.Release();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -070096}
97
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098// Options that can be applied to a VideoMediaChannel or a VideoMediaEngine.
99// Used to be flags, but that makes it hard to selectively apply options.
100// We are moving all of the setting of options to structs like this,
101// but some things currently still use flags.
102struct VideoOptions {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200103 VideoOptions();
104 ~VideoOptions();
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 void SetAll(const VideoOptions& change) {
kwiberg102c6a62015-10-30 02:47:38 -0700107 SetFrom(&video_noise_reduction, change.video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800108 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100109 SetFrom(&is_screencast, change.is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 }
111
112 bool operator==(const VideoOptions& o) const {
nisseb163c3f2016-01-29 01:14:38 -0800113 return video_noise_reduction == o.video_noise_reduction &&
Niels Möller60653ba2016-03-02 11:41:36 +0100114 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps &&
115 is_screencast == o.is_screencast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 }
deadbeef119760a2016-04-04 11:43:27 -0700117 bool operator!=(const VideoOptions& o) const { return !(*this == o); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200120 rtc::StringBuilder ost;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 ost << "VideoOptions {";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 ost << ToStringIfSet("noise reduction", video_noise_reduction);
nisseb163c3f2016-01-29 01:14:38 -0800123 ost << ToStringIfSet("screencast min bitrate kbps",
124 screencast_min_bitrate_kbps);
Niels Möller60653ba2016-03-02 11:41:36 +0100125 ost << ToStringIfSet("is_screencast ", is_screencast);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200127 return ost.Release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129
nisseb163c3f2016-01-29 01:14:38 -0800130 // Enable denoising? This flag comes from the getUserMedia
eladalonf1841382017-06-12 01:16:46 -0700131 // constraint 'googNoiseReduction', and WebRtcVideoEngine passes it
nisseb163c3f2016-01-29 01:14:38 -0800132 // on to the codec options. Disabled by default.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200133 absl::optional<bool> video_noise_reduction;
nisseb163c3f2016-01-29 01:14:38 -0800134 // Force screencast to use a minimum bitrate. This flag comes from
135 // the PeerConnection constraint 'googScreencastMinBitrate'. It is
eladalonf1841382017-06-12 01:16:46 -0700136 // copied to the encoder config by WebRtcVideoChannel.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200137 absl::optional<int> screencast_min_bitrate_kbps;
Niels Möller60653ba2016-03-02 11:41:36 +0100138 // Set by screencast sources. Implies selection of encoding settings
139 // suitable for screencast. Most likely not the right way to do
140 // things, e.g., screencast of a text document and screencast of a
141 // youtube video have different needs.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200142 absl::optional<bool> is_screencast;
kwiberg102c6a62015-10-30 02:47:38 -0700143
144 private:
145 template <typename T>
Danil Chapovalov00c71832018-06-15 15:58:38 +0200146 static void SetFrom(absl::optional<T>* s, const absl::optional<T>& o) {
kwiberg102c6a62015-10-30 02:47:38 -0700147 if (o) {
148 *s = o;
149 }
150 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151};
152
isheriffa1c548b2016-05-31 16:12:24 -0700153// TODO(isheriff): Remove this once client usage is fixed to use RtpExtension.
154struct RtpHeaderExtension {
155 RtpHeaderExtension() : id(0) {}
156 RtpHeaderExtension(const std::string& uri, int id) : uri(uri), id(id) {}
157
158 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200159 rtc::StringBuilder ost;
isheriffa1c548b2016-05-31 16:12:24 -0700160 ost << "{";
161 ost << "uri: " << uri;
162 ost << ", id: " << id;
163 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200164 return ost.Release();
isheriffa1c548b2016-05-31 16:12:24 -0700165 }
166
167 std::string uri;
168 int id;
169};
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171class MediaChannel : public sigslot::has_slots<> {
172 public:
173 class NetworkInterface {
174 public:
175 enum SocketType { ST_RTP, ST_RTCP };
jbaucheec21bd2016-03-20 06:15:43 -0700176 virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700177 const rtc::PacketOptions& options) = 0;
jbaucheec21bd2016-03-20 06:15:43 -0700178 virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700179 const rtc::PacketOptions& options) = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200180 virtual int SetOption(SocketType type,
181 rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 int option) = 0;
183 virtual ~NetworkInterface() {}
184 };
185
Benjamin Wright84583f62018-10-04 14:22:34 -0700186 explicit MediaChannel(const MediaConfig& config);
187 MediaChannel();
188 ~MediaChannel() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800190 virtual cricket::MediaType media_type() const = 0;
191
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700192 // Sets the abstract interface class for sending RTP/RTCP data and
193 // interface for media transport (experimental). If media transport is
194 // provided, it should be used instead of RTP/RTCP.
195 // TODO(sukhanov): Currently media transport can co-exist with RTP/RTCP, but
196 // in the future we will refactor code to send all frames with media
197 // transport.
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700198 virtual void SetInterface(
199 NetworkInterface* iface,
200 const webrtc::MediaTransportConfig& media_transport_config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 // Called when a RTP packet is received.
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700202 virtual void OnPacketReceived(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 a RTCP packet is received.
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700205 virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100206 int64_t packet_time_us) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 // Called when the socket's ability to send has changed.
208 virtual void OnReadyToSend(bool ready) = 0;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700209 // Called when the network route used for sending packets changed.
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700210 virtual void OnNetworkRouteChanged(
211 const std::string& transport_name,
212 const rtc::NetworkRoute& network_route) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 // Creates a new outgoing media stream with SSRCs and CNAME as described
214 // by sp.
215 virtual bool AddSendStream(const StreamParams& sp) = 0;
216 // Removes an outgoing media stream.
Seth Hampson5897a6e2018-04-03 11:16:33 -0700217 // SSRC must be the first SSRC of the media stream if the stream uses
218 // multiple SSRCs. In the case of an ssrc of 0, the possibly cached
219 // StreamParams is removed.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220 virtual bool RemoveSendStream(uint32_t ssrc) = 0;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700221 // Creates a new incoming media stream with SSRCs, CNAME as described
222 // by sp. In the case of a sp without SSRCs, the unsignaled sp is cached
223 // to be used later for unsignaled streams received.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 virtual bool AddRecvStream(const StreamParams& sp) = 0;
225 // Removes an incoming media stream.
226 // ssrc must be the first SSRC of the media stream if the stream uses
227 // multiple SSRCs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200228 virtual bool RemoveRecvStream(uint32_t ssrc) = 0;
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +0000229 // Returns the absoulte sendtime extension id value from media channel.
Paulina Hensman11b34f42018-04-09 14:24:52 +0200230 virtual int GetRtpSendTimeExtnId() const;
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700231 // Set the frame encryptor to use on all outgoing frames. This is optional.
232 // This pointers lifetime is managed by the set of RtpSender it is attached
233 // to.
Benjamin Wright84583f62018-10-04 14:22:34 -0700234 // TODO(benwright) make pure virtual once internal supports it.
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700235 virtual void SetFrameEncryptor(
Benjamin Wright84583f62018-10-04 14:22:34 -0700236 uint32_t ssrc,
237 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700238 // Set the frame decryptor to use on all incoming frames. This is optional.
239 // This pointers lifetimes is managed by the set of RtpReceivers it is
240 // attached to.
Benjamin Wright84583f62018-10-04 14:22:34 -0700241 // TODO(benwright) make pure virtual once internal supports it.
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700242 virtual void SetFrameDecryptor(
Benjamin Wright84583f62018-10-04 14:22:34 -0700243 uint32_t ssrc,
244 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000246 // Base method to send packet using NetworkInterface.
jbaucheec21bd2016-03-20 06:15:43 -0700247 bool SendPacket(rtc::CopyOnWriteBuffer* packet,
248 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700249 return DoSendPacket(packet, false, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000250 }
251
jbaucheec21bd2016-03-20 06:15:43 -0700252 bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
253 const rtc::PacketOptions& options) {
stefanc1aeaf02015-10-15 07:26:07 -0700254 return DoSendPacket(packet, true, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000255 }
256
257 int SetOption(NetworkInterface::SocketType type,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000258 rtc::Socket::Option opt,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000259 int option) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000260 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000261 if (!network_interface_)
262 return -1;
263
264 return network_interface_->SetOption(type, opt, option);
265 }
266
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700267 const webrtc::MediaTransportConfig& media_transport_config() const {
268 return media_transport_config_;
269 }
270
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700271 webrtc::MediaTransportInterface* media_transport() {
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700272 return media_transport_config_.media_transport;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700273 }
274
Johannes Kron9190b822018-10-29 11:22:05 +0100275 // Corresponds to the SDP attribute extmap-allow-mixed, see RFC8285.
276 // Set to true if it's allowed to mix one- and two-byte RTP header extensions
277 // in the same stream. The setter and getter must only be called from
278 // worker_thread.
279 void SetExtmapAllowMixed(bool extmap_allow_mixed) {
280 extmap_allow_mixed_ = extmap_allow_mixed;
281 }
282 bool ExtmapAllowMixed() const { return extmap_allow_mixed_; }
283
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800284 virtual webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const = 0;
285 virtual webrtc::RTCError SetRtpSendParameters(
286 uint32_t ssrc,
287 const webrtc::RtpParameters& parameters) = 0;
288
Tim Haloun6ca98362018-09-17 17:06:08 -0700289 protected:
Tim Haloun6ca98362018-09-17 17:06:08 -0700290 bool DscpEnabled() const { return enable_dscp_; }
291
Steve Antone25f5952019-03-08 15:09:16 -0800292 // This is the DSCP value used for both RTP and RTCP channels if DSCP is
293 // enabled. It can be changed at any time via |SetPreferredDscp|.
294 rtc::DiffServCodePoint PreferredDscp() const {
295 rtc::CritScope cs(&network_interface_crit_);
296 return preferred_dscp_;
297 }
298
299 int SetPreferredDscp(rtc::DiffServCodePoint preferred_dscp) {
300 rtc::CritScope cs(&network_interface_crit_);
301 if (preferred_dscp == preferred_dscp_) {
302 return 0;
303 }
304 preferred_dscp_ = preferred_dscp;
305 return UpdateDscp();
306 }
307
308 private:
309 // Apply the preferred DSCP setting to the underlying network interface RTP
310 // and RTCP channels. If DSCP is disabled, then apply the default DSCP value.
311 int UpdateDscp() RTC_EXCLUSIVE_LOCKS_REQUIRED(network_interface_crit_) {
Tim Haloun648d28a2018-10-18 16:52:22 -0700312 rtc::DiffServCodePoint value =
Steve Antone25f5952019-03-08 15:09:16 -0800313 enable_dscp_ ? preferred_dscp_ : rtc::DSCP_DEFAULT;
314 int ret = SetOption(NetworkInterface::ST_RTP, rtc::Socket::OPT_DSCP, value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000315 if (ret == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200316 ret = SetOption(NetworkInterface::ST_RTCP, rtc::Socket::OPT_DSCP, value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000317 }
318 return ret;
319 }
320
jbaucheec21bd2016-03-20 06:15:43 -0700321 bool DoSendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700322 bool rtcp,
323 const rtc::PacketOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000324 rtc::CritScope cs(&network_interface_crit_);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000325 if (!network_interface_)
326 return false;
327
stefanc1aeaf02015-10-15 07:26:07 -0700328 return (!rtcp) ? network_interface_->SendPacket(packet, options)
329 : network_interface_->SendRtcp(packet, options);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000330 }
331
nisse51542be2016-02-12 02:27:06 -0800332 const bool enable_dscp_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000333 // |network_interface_| can be accessed from the worker_thread and
334 // from any MediaEngine threads. This critical section is to protect accessing
335 // of network_interface_ object.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000336 rtc::CriticalSection network_interface_crit_;
Steve Antone25f5952019-03-08 15:09:16 -0800337 NetworkInterface* network_interface_ RTC_GUARDED_BY(network_interface_crit_) =
338 nullptr;
339 rtc::DiffServCodePoint preferred_dscp_
340 RTC_GUARDED_BY(network_interface_crit_) = rtc::DSCP_DEFAULT;
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700341 webrtc::MediaTransportConfig media_transport_config_;
Johannes Kron9190b822018-10-29 11:22:05 +0100342 bool extmap_allow_mixed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343};
344
wu@webrtc.org97077a32013-10-25 21:18:33 +0000345// The stats information is structured as follows:
346// Media are represented by either MediaSenderInfo or MediaReceiverInfo.
347// Media contains a vector of SSRC infos that are exclusively used by this
348// media. (SSRCs shared between media streams can't be represented.)
349
350// Information about an SSRC.
351// This data may be locally recorded, or received in an RTCP SR or RR.
352struct SsrcSenderInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800353 uint32_t ssrc = 0;
354 double timestamp = 0.0; // NTP timestamp, represented as seconds since epoch.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000355};
356
357struct SsrcReceiverInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800358 uint32_t ssrc = 0;
359 double timestamp = 0.0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000360};
361
362struct MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200363 MediaSenderInfo();
364 ~MediaSenderInfo();
Yves Gerey665174f2018-06-19 15:03:05 +0200365 void add_ssrc(const SsrcSenderInfo& stat) { local_stats.push_back(stat); }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000366 // Temporary utility function for call sites that only provide SSRC.
367 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200368 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000369 SsrcSenderInfo stat;
370 stat.ssrc = ssrc;
371 add_ssrc(stat);
372 }
373 // Utility accessor for clients that are only interested in ssrc numbers.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200374 std::vector<uint32_t> ssrcs() const {
375 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000376 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin();
377 it != local_stats.end(); ++it) {
378 retval.push_back(it->ssrc);
379 }
380 return retval;
381 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100382 // Returns true if the media has been connected.
383 bool connected() const { return local_stats.size() > 0; }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000384 // Utility accessor for clients that make the assumption only one ssrc
385 // exists per media.
386 // This will eventually go away.
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100387 // Call sites that compare this to zero should use connected() instead.
388 // https://bugs.webrtc.org/8694
Peter Boström0c4e06b2015-10-07 12:23:21 +0200389 uint32_t ssrc() const {
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100390 if (connected()) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000391 return local_stats[0].ssrc;
392 } else {
393 return 0;
394 }
395 }
Steve Anton002f9212018-01-09 16:38:15 -0800396 int64_t bytes_sent = 0;
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200397 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedbytessent
398 uint64_t retransmitted_bytes_sent = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800399 int packets_sent = 0;
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200400 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedpacketssent
401 uint64_t retransmitted_packets_sent = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800402 int packets_lost = 0;
403 float fraction_lost = 0.0f;
404 int64_t rtt_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000405 std::string codec_name;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200406 absl::optional<int> codec_payload_type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000407 std::vector<SsrcSenderInfo> local_stats;
408 std::vector<SsrcReceiverInfo> remote_stats;
Henrik Boström87e3f9d2019-05-27 10:44:24 +0200409 // A snapshot of the most recent Report Block with additional data of interest
410 // to statistics. Used to implement RTCRemoteInboundRtpStreamStats. Within
411 // this list, the ReportBlockData::RTCPReportBlock::source_ssrc(), which is
412 // the SSRC of the corresponding outbound RTP stream, is unique.
413 std::vector<webrtc::ReportBlockData> report_block_datas;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000414};
415
416struct MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200417 MediaReceiverInfo();
418 ~MediaReceiverInfo();
Yves Gerey665174f2018-06-19 15:03:05 +0200419 void add_ssrc(const SsrcReceiverInfo& stat) { local_stats.push_back(stat); }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000420 // Temporary utility function for call sites that only provide SSRC.
421 // As more info is added into SsrcSenderInfo, this function should go away.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200422 void add_ssrc(uint32_t ssrc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000423 SsrcReceiverInfo stat;
424 stat.ssrc = ssrc;
425 add_ssrc(stat);
426 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200427 std::vector<uint32_t> ssrcs() const {
428 std::vector<uint32_t> retval;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000429 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin();
430 it != local_stats.end(); ++it) {
431 retval.push_back(it->ssrc);
432 }
433 return retval;
434 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100435 // Returns true if the media has been connected.
436 bool connected() const { return local_stats.size() > 0; }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000437 // Utility accessor for clients that make the assumption only one ssrc
438 // exists per media.
439 // This will eventually go away.
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100440 // Call sites that compare this to zero should use connected();
441 // https://bugs.webrtc.org/8694
Peter Boström0c4e06b2015-10-07 12:23:21 +0200442 uint32_t ssrc() const {
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100443 if (connected()) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000444 return local_stats[0].ssrc;
445 } else {
446 return 0;
447 }
448 }
449
Steve Anton002f9212018-01-09 16:38:15 -0800450 int64_t bytes_rcvd = 0;
451 int packets_rcvd = 0;
452 int packets_lost = 0;
453 float fraction_lost = 0.0f;
Henrik Boström01738c62019-04-15 17:32:00 +0200454 // The timestamp at which the last packet was received, i.e. the time of the
455 // local clock when it was received - not the RTP timestamp of that packet.
456 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-lastpacketreceivedtimestamp
457 absl::optional<int64_t> last_packet_received_timestamp_ms;
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000458 std::string codec_name;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200459 absl::optional<int> codec_payload_type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000460 std::vector<SsrcReceiverInfo> local_stats;
461 std::vector<SsrcSenderInfo> remote_stats;
462};
463
464struct VoiceSenderInfo : public MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200465 VoiceSenderInfo();
466 ~VoiceSenderInfo();
Steve Anton002f9212018-01-09 16:38:15 -0800467 int ext_seqnum = 0;
468 int jitter_ms = 0;
469 int audio_level = 0;
zsteine76bd3a2017-07-14 12:17:49 -0700470 // See description of "totalAudioEnergy" in the WebRTC stats spec:
471 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
Steve Anton002f9212018-01-09 16:38:15 -0800472 double total_input_energy = 0.0;
473 double total_input_duration = 0.0;
Steve Anton002f9212018-01-09 16:38:15 -0800474 bool typing_noise_detected = false;
ivoce1198e02017-09-08 08:13:19 -0700475 webrtc::ANAStats ana_statistics;
Ivo Creusen56d46092017-11-24 17:29:59 +0100476 webrtc::AudioProcessingStats apm_statistics;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477};
478
wu@webrtc.org97077a32013-10-25 21:18:33 +0000479struct VoiceReceiverInfo : public MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200480 VoiceReceiverInfo();
481 ~VoiceReceiverInfo();
Steve Anton002f9212018-01-09 16:38:15 -0800482 int ext_seqnum = 0;
483 int jitter_ms = 0;
484 int jitter_buffer_ms = 0;
485 int jitter_buffer_preferred_ms = 0;
486 int delay_estimate_ms = 0;
487 int audio_level = 0;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200488 // Stats below correspond to similarly-named fields in the WebRTC stats spec.
489 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats
Steve Anton002f9212018-01-09 16:38:15 -0800490 double total_output_energy = 0.0;
491 uint64_t total_samples_received = 0;
492 double total_output_duration = 0.0;
493 uint64_t concealed_samples = 0;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200494 uint64_t silent_concealed_samples = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800495 uint64_t concealment_events = 0;
Chen Xing0acffb52019-01-15 15:46:29 +0100496 double jitter_buffer_delay_seconds = 0.0;
497 uint64_t jitter_buffer_emitted_count = 0;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200498 uint64_t inserted_samples_for_deceleration = 0;
499 uint64_t removed_samples_for_acceleration = 0;
500 uint64_t fec_packets_received = 0;
501 uint64_t fec_packets_discarded = 0;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200502 // Stats below DO NOT correspond directly to anything in the WebRTC stats
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000503 // fraction of synthesized audio inserted through expansion.
Steve Anton002f9212018-01-09 16:38:15 -0800504 float expand_rate = 0.0f;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000505 // fraction of synthesized speech inserted through expansion.
Steve Anton002f9212018-01-09 16:38:15 -0800506 float speech_expand_rate = 0.0f;
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000507 // fraction of data out of secondary decoding, including FEC and RED.
Steve Anton002f9212018-01-09 16:38:15 -0800508 float secondary_decoded_rate = 0.0f;
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200509 // Fraction of secondary data, including FEC and RED, that is discarded.
510 // Discarding of secondary data can be caused by the reception of the primary
511 // data, obsoleting the secondary data. It can also be caused by early
512 // or late arrival of secondary data. This metric is the percentage of
513 // discarded secondary data since last query of receiver info.
Steve Anton002f9212018-01-09 16:38:15 -0800514 float secondary_discarded_rate = 0.0f;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200515 // Fraction of data removed through time compression.
Steve Anton002f9212018-01-09 16:38:15 -0800516 float accelerate_rate = 0.0f;
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200517 // Fraction of data inserted through time stretching.
Steve Anton002f9212018-01-09 16:38:15 -0800518 float preemptive_expand_rate = 0.0f;
519 int decoding_calls_to_silence_generator = 0;
520 int decoding_calls_to_neteq = 0;
521 int decoding_normal = 0;
522 int decoding_plc = 0;
523 int decoding_cng = 0;
524 int decoding_plc_cng = 0;
525 int decoding_muted_output = 0;
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000526 // Estimated capture start time in NTP time in ms.
Steve Anton002f9212018-01-09 16:38:15 -0800527 int64_t capture_start_ntp_time_ms = -1;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100528 // Count of the number of buffer flushes.
529 uint64_t jitter_buffer_flushes = 0;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100530 // Number of samples expanded due to delayed packets.
531 uint64_t delayed_packet_outage_samples = 0;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100532 // Arrival delay of received audio packets.
533 double relative_packet_arrival_delay_seconds = 0.0;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200534 // Count and total duration of audio interruptions (loss-concealement periods
535 // longer than 150 ms).
536 int32_t interruption_count = 0;
537 int32_t total_interruption_duration_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538};
539
wu@webrtc.org97077a32013-10-25 21:18:33 +0000540struct VideoSenderInfo : public MediaSenderInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200541 VideoSenderInfo();
542 ~VideoSenderInfo();
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000543 std::vector<SsrcGroup> ssrc_groups;
hbosa65704b2016-11-14 02:28:16 -0800544 // TODO(hbos): Move this to |VideoMediaInfo::send_codecs|?
Peter Boströmb7d9a972015-12-18 16:01:11 +0100545 std::string encoder_implementation_name;
Steve Anton002f9212018-01-09 16:38:15 -0800546 int firs_rcvd = 0;
547 int plis_rcvd = 0;
548 int nacks_rcvd = 0;
549 int send_frame_width = 0;
550 int send_frame_height = 0;
551 int framerate_input = 0;
552 int framerate_sent = 0;
553 int nominal_bitrate = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800554 int adapt_reason = 0;
555 int adapt_changes = 0;
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200556 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
557 webrtc::QualityLimitationReason quality_limitation_reason =
558 webrtc::QualityLimitationReason::kNone;
559 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
560 std::map<webrtc::QualityLimitationReason, int64_t>
561 quality_limitation_durations_ms;
Steve Anton002f9212018-01-09 16:38:15 -0800562 int avg_encode_ms = 0;
563 int encode_usage_percent = 0;
564 uint32_t frames_encoded = 0;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200565 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
566 uint64_t total_encode_time_ms = 0;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200567 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
568 uint64_t total_encoded_bytes_target = 0;
Henrik Boström9fe18342019-05-16 18:38:20 +0200569 uint64_t total_packet_send_delay_ms = 0;
Steve Anton002f9212018-01-09 16:38:15 -0800570 bool has_entered_low_resolution = false;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200571 absl::optional<uint64_t> qp_sum;
Steve Anton002f9212018-01-09 16:38:15 -0800572 webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100573 // https://w3c.github.io/webrtc-stats/#dom-rtcvideosenderstats-hugeframessent
574 uint32_t huge_frames_sent = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575};
576
wu@webrtc.org97077a32013-10-25 21:18:33 +0000577struct VideoReceiverInfo : public MediaReceiverInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200578 VideoReceiverInfo();
579 ~VideoReceiverInfo();
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000580 std::vector<SsrcGroup> ssrc_groups;
hbosa65704b2016-11-14 02:28:16 -0800581 // TODO(hbos): Move this to |VideoMediaInfo::receive_codecs|?
Peter Boströmb7d9a972015-12-18 16:01:11 +0100582 std::string decoder_implementation_name;
Steve Anton002f9212018-01-09 16:38:15 -0800583 int packets_concealed = 0;
584 int firs_sent = 0;
585 int plis_sent = 0;
586 int nacks_sent = 0;
587 int frame_width = 0;
588 int frame_height = 0;
589 int framerate_rcvd = 0;
590 int framerate_decoded = 0;
591 int framerate_output = 0;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000592 // Framerate as sent to the renderer.
Steve Anton002f9212018-01-09 16:38:15 -0800593 int framerate_render_input = 0;
pbos@webrtc.org1ed62242015-02-19 13:57:03 +0000594 // Framerate that the renderer reports.
Steve Anton002f9212018-01-09 16:38:15 -0800595 int framerate_render_output = 0;
596 uint32_t frames_received = 0;
597 uint32_t frames_decoded = 0;
598 uint32_t frames_rendered = 0;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200599 absl::optional<uint64_t> qp_sum;
Steve Anton002f9212018-01-09 16:38:15 -0800600 int64_t interframe_delay_max_ms = -1;
Sergey Silkin02371062019-01-31 16:45:42 +0100601 uint32_t freeze_count = 0;
602 uint32_t pause_count = 0;
603 uint32_t total_freezes_duration_ms = 0;
604 uint32_t total_pauses_duration_ms = 0;
605 uint32_t total_frames_duration_ms = 0;
606 double sum_squared_frame_durations = 0.0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000607
Steve Anton002f9212018-01-09 16:38:15 -0800608 webrtc::VideoContentType content_type = webrtc::VideoContentType::UNSPECIFIED;
ilnik2e1b40b2017-09-04 07:57:17 -0700609
wu@webrtc.org97077a32013-10-25 21:18:33 +0000610 // All stats below are gathered per-VideoReceiver, but some will be correlated
611 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC
612 // structures, reflect this in the new layout.
613
614 // Current frame decode latency.
Steve Anton002f9212018-01-09 16:38:15 -0800615 int decode_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000616 // Maximum observed frame decode latency.
Steve Anton002f9212018-01-09 16:38:15 -0800617 int max_decode_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000618 // Jitter (network-related) latency.
Steve Anton002f9212018-01-09 16:38:15 -0800619 int jitter_buffer_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000620 // Requested minimum playout latency.
Steve Anton002f9212018-01-09 16:38:15 -0800621 int min_playout_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000622 // Requested latency to account for rendering delay.
Steve Anton002f9212018-01-09 16:38:15 -0800623 int render_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000624 // Target overall delay: network+decode+render, accounting for
625 // min_playout_delay_ms.
Steve Anton002f9212018-01-09 16:38:15 -0800626 int target_delay_ms = 0;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000627 // Current overall delay, possibly ramping towards target_delay_ms.
Steve Anton002f9212018-01-09 16:38:15 -0800628 int current_delay_ms = 0;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000629
630 // Estimated capture start time in NTP time in ms.
Steve Anton002f9212018-01-09 16:38:15 -0800631 int64_t capture_start_ntp_time_ms = -1;
ilnik2edc6842017-07-06 03:06:50 -0700632
Benjamin Wright514f0842018-12-10 09:55:17 -0800633 // First frame received to first frame decoded latency.
634 int64_t first_frame_received_to_decoded_ms = -1;
635
ilnik2edc6842017-07-06 03:06:50 -0700636 // Timing frame info: all important timestamps for a full lifetime of a
637 // single 'timing frame'.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200638 absl::optional<webrtc::TimingFrameInfo> timing_frame_info;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639};
640
wu@webrtc.org97077a32013-10-25 21:18:33 +0000641struct DataSenderInfo : public MediaSenderInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800642 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643};
644
wu@webrtc.org97077a32013-10-25 21:18:33 +0000645struct DataReceiverInfo : public MediaReceiverInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800646 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647};
648
649struct BandwidthEstimationInfo {
Steve Anton002f9212018-01-09 16:38:15 -0800650 int available_send_bandwidth = 0;
651 int available_recv_bandwidth = 0;
652 int target_enc_bitrate = 0;
653 int actual_enc_bitrate = 0;
654 int retransmit_bitrate = 0;
655 int transmit_bitrate = 0;
656 int64_t bucket_delay = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657};
658
hbosa65704b2016-11-14 02:28:16 -0800659// Maps from payload type to |RtpCodecParameters|.
660typedef std::map<int, webrtc::RtpCodecParameters> RtpCodecParametersMap;
661
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662struct VoiceMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200663 VoiceMediaInfo();
664 ~VoiceMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 void Clear() {
666 senders.clear();
667 receivers.clear();
hbos1acfbd22016-11-17 23:43:29 -0800668 send_codecs.clear();
669 receive_codecs.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 }
671 std::vector<VoiceSenderInfo> senders;
672 std::vector<VoiceReceiverInfo> receivers;
hbos1acfbd22016-11-17 23:43:29 -0800673 RtpCodecParametersMap send_codecs;
674 RtpCodecParametersMap receive_codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675};
676
677struct VideoMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200678 VideoMediaInfo();
679 ~VideoMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 void Clear() {
681 senders.clear();
682 receivers.clear();
charujaind72098a2017-06-01 08:54:47 -0700683 bw_estimations.clear();
hbosa65704b2016-11-14 02:28:16 -0800684 send_codecs.clear();
685 receive_codecs.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 }
687 std::vector<VideoSenderInfo> senders;
688 std::vector<VideoReceiverInfo> receivers;
stefanf79ade12017-06-02 06:44:03 -0700689 // Deprecated.
690 // TODO(holmer): Remove once upstream projects no longer use this.
charujaind72098a2017-06-01 08:54:47 -0700691 std::vector<BandwidthEstimationInfo> bw_estimations;
hbosa65704b2016-11-14 02:28:16 -0800692 RtpCodecParametersMap send_codecs;
693 RtpCodecParametersMap receive_codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694};
695
696struct DataMediaInfo {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200697 DataMediaInfo();
698 ~DataMediaInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 void Clear() {
700 senders.clear();
701 receivers.clear();
702 }
703 std::vector<DataSenderInfo> senders;
704 std::vector<DataReceiverInfo> receivers;
705};
706
deadbeef13871492015-12-09 12:37:51 -0800707struct RtcpParameters {
708 bool reduced_size = false;
709};
710
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700711template <class Codec>
712struct RtpParameters {
Steve Anton003930a2018-03-29 12:37:21 -0700713 virtual ~RtpParameters() = default;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700714
715 std::vector<Codec> codecs;
isheriff6f8d6862016-05-26 11:24:55 -0700716 std::vector<webrtc::RtpExtension> extensions;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700717 // TODO(pthatcher): Add streams.
deadbeef13871492015-12-09 12:37:51 -0800718 RtcpParameters rtcp;
Steve Anton003930a2018-03-29 12:37:21 -0700719
720 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200721 rtc::StringBuilder ost;
Steve Anton003930a2018-03-29 12:37:21 -0700722 ost << "{";
723 const char* separator = "";
724 for (const auto& entry : ToStringMap()) {
725 ost << separator << entry.first << ": " << entry.second;
726 separator = ", ";
727 }
728 ost << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200729 return ost.Release();
Steve Anton003930a2018-03-29 12:37:21 -0700730 }
731
732 protected:
733 virtual std::map<std::string, std::string> ToStringMap() const {
734 return {{"codecs", VectorToString(codecs)},
735 {"extensions", VectorToString(extensions)}};
736 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700737};
738
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700739// TODO(deadbeef): Rename to RtpSenderParameters, since they're intended to
740// encapsulate all the parameters needed for an RtpSender.
nisse05103312016-03-16 02:22:50 -0700741template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700742struct RtpSendParameters : RtpParameters<Codec> {
nisse05103312016-03-16 02:22:50 -0700743 int max_bandwidth_bps = -1;
Steve Antonbb50ce52018-03-26 10:24:32 -0700744 // This is the value to be sent in the MID RTP header extension (if the header
745 // extension in included in the list of extensions).
746 std::string mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100747 bool extmap_allow_mixed = false;
Steve Anton003930a2018-03-29 12:37:21 -0700748
749 protected:
750 std::map<std::string, std::string> ToStringMap() const override {
751 auto params = RtpParameters<Codec>::ToStringMap();
752 params["max_bandwidth_bps"] = rtc::ToString(max_bandwidth_bps);
753 params["mid"] = (mid.empty() ? "<not set>" : mid);
Johannes Kron9190b822018-10-29 11:22:05 +0100754 params["extmap-allow-mixed"] = extmap_allow_mixed ? "true" : "false";
Steve Anton003930a2018-03-29 12:37:21 -0700755 return params;
756 }
nisse05103312016-03-16 02:22:50 -0700757};
758
759struct AudioSendParameters : RtpSendParameters<AudioCodec> {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200760 AudioSendParameters();
761 ~AudioSendParameters() override;
nisse05103312016-03-16 02:22:50 -0700762 AudioOptions options;
Steve Anton003930a2018-03-29 12:37:21 -0700763
764 protected:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200765 std::map<std::string, std::string> ToStringMap() const override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700766};
767
Yves Gerey665174f2018-06-19 15:03:05 +0200768struct AudioRecvParameters : RtpParameters<AudioCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700769
Ruslan Burakov493a6502019-02-27 15:32:48 +0100770class VoiceMediaChannel : public MediaChannel, public Delayable {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 VoiceMediaChannel() {}
terelius54f91712016-06-01 11:18:56 -0700773 explicit VoiceMediaChannel(const MediaConfig& config)
774 : MediaChannel(config) {}
Paulina Hensman11b34f42018-04-09 14:24:52 +0200775 ~VoiceMediaChannel() override {}
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800776
777 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200778 virtual bool SetSendParameters(const AudioSendParameters& params) = 0;
779 virtual bool SetRecvParameters(const AudioRecvParameters& params) = 0;
deadbeef3bc15102017-04-20 19:25:07 -0700780 // Get the receive parameters for the incoming stream identified by |ssrc|.
781 // If |ssrc| is 0, retrieve the receive parameters for the default receive
782 // stream, which is used when SSRCs are not signaled. Note that calling with
783 // an |ssrc| of 0 will return encoding parameters with an unset |ssrc|
784 // member.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700785 virtual webrtc::RtpParameters GetRtpReceiveParameters(
786 uint32_t ssrc) const = 0;
787 virtual bool SetRtpReceiveParameters(
788 uint32_t ssrc,
789 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 // Starts or stops playout of received audio.
aleloi84ef6152016-08-04 05:28:21 -0700791 virtual void SetPlayout(bool playout) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 // Starts or stops sending (and potentially capture) of local audio.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800793 virtual void SetSend(bool send) = 0;
solenberg1dd98f32015-09-10 01:57:14 -0700794 // Configure stream for sending.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200795 virtual bool SetAudioSend(uint32_t ssrc,
796 bool enable,
solenbergdfc8f4f2015-10-01 02:31:10 -0700797 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800798 AudioSource* source) = 0;
solenberg4bac9c52015-10-09 02:32:53 -0700799 // Set speaker output volume of the specified ssrc.
800 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 // Returns if the telephone-event has been negotiated.
solenberg1d63dd02015-12-02 12:35:09 -0800802 virtual bool CanInsertDtmf() = 0;
803 // Send a DTMF |event|. The DTMF out-of-band signal will be used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 // The |ssrc| should be either 0 or a valid send stream ssrc.
henrike@webrtc.org9de257d2013-07-17 14:42:53 +0000805 // The valid value for the |event| are 0 to 15 which corresponding to
806 // DTMF event 0-9, *, #, A-D.
solenberg1d63dd02015-12-02 12:35:09 -0800807 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 // Gets quality stats for the channel.
809 virtual bool GetStats(VoiceMediaInfo* info) = 0;
Tommif888bb52015-12-12 01:37:01 +0100810
811 virtual void SetRawAudioSink(
812 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -0800813 std::unique_ptr<webrtc::AudioSinkInterface> sink) = 0;
zhihuang38ede132017-06-15 12:52:32 -0700814
815 virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816};
817
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700818// TODO(deadbeef): Rename to VideoSenderParameters, since they're intended to
819// encapsulate all the parameters needed for a video RtpSender.
nisse05103312016-03-16 02:22:50 -0700820struct VideoSendParameters : RtpSendParameters<VideoCodec> {
Paulina Hensman11b34f42018-04-09 14:24:52 +0200821 VideoSendParameters();
822 ~VideoSendParameters() override;
nisse4b4dc862016-02-17 05:25:36 -0800823 // Use conference mode? This flag comes from the remote
824 // description's SDP line 'a=x-google-flag:conference', copied over
825 // by VideoChannel::SetRemoteContent_w, and ultimately used by
826 // conference mode screencast logic in
eladalonf1841382017-06-12 01:16:46 -0700827 // WebRtcVideoChannel::WebRtcVideoSendStream::CreateVideoEncoderConfig.
nisse4b4dc862016-02-17 05:25:36 -0800828 // The special screencast behaviour is disabled by default.
829 bool conference_mode = false;
Steve Anton003930a2018-03-29 12:37:21 -0700830
831 protected:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200832 std::map<std::string, std::string> ToStringMap() const override;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700833};
834
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700835// TODO(deadbeef): Rename to VideoReceiverParameters, since they're intended to
836// encapsulate all the parameters needed for a video RtpReceiver.
Yves Gerey665174f2018-06-19 15:03:05 +0200837struct VideoRecvParameters : RtpParameters<VideoCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700838
Ruslan Burakov493a6502019-02-27 15:32:48 +0100839class VideoMediaChannel : public MediaChannel, public Delayable {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 public:
nisse08582ff2016-02-04 01:24:52 -0800841 VideoMediaChannel() {}
terelius54f91712016-06-01 11:18:56 -0700842 explicit VideoMediaChannel(const MediaConfig& config)
843 : MediaChannel(config) {}
Paulina Hensman11b34f42018-04-09 14:24:52 +0200844 ~VideoMediaChannel() override {}
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200845
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800846 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200847 virtual bool SetSendParameters(const VideoSendParameters& params) = 0;
848 virtual bool SetRecvParameters(const VideoRecvParameters& params) = 0;
deadbeef3bc15102017-04-20 19:25:07 -0700849 // Get the receive parameters for the incoming stream identified by |ssrc|.
850 // If |ssrc| is 0, retrieve the receive parameters for the default receive
851 // stream, which is used when SSRCs are not signaled. Note that calling with
852 // an |ssrc| of 0 will return encoding parameters with an unset |ssrc|
853 // member.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700854 virtual webrtc::RtpParameters GetRtpReceiveParameters(
855 uint32_t ssrc) const = 0;
856 virtual bool SetRtpReceiveParameters(
857 uint32_t ssrc,
858 const webrtc::RtpParameters& parameters) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 // Gets the currently set codecs/payload types to be used for outgoing media.
860 virtual bool GetSendCodec(VideoCodec* send_codec) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 // Starts or stops transmission (and potentially capture) of local video.
862 virtual bool SetSend(bool send) = 0;
deadbeef5a4a75a2016-06-02 16:23:38 -0700863 // Configure stream for sending and register a source.
864 // The |ssrc| must correspond to a registered send stream.
865 virtual bool SetVideoSend(
866 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -0700867 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -0800868 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) = 0;
nisse08582ff2016-02-04 01:24:52 -0800869 // Sets the sink object to be used for the specified stream.
deadbeef3bc15102017-04-20 19:25:07 -0700870 // If SSRC is 0, the sink is used for the 'default' stream.
nisse08582ff2016-02-04 01:24:52 -0800871 virtual bool SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -0800872 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) = 0;
stefanf79ade12017-06-02 06:44:03 -0700873 // This fills the "bitrate parts" (rtx, video bitrate) of the
874 // BandwidthEstimationInfo, since that part that isn't possible to get
875 // through webrtc::Call::GetStats, as they are statistics of the send
876 // streams.
877 // TODO(holmer): We should change this so that either BWE graphs doesn't
878 // need access to bitrates of the streams, or change the (RTC)StatsCollector
879 // so that it's getting the send stream stats separately by calling
880 // GetStats(), and merges with BandwidthEstimationInfo by itself.
881 virtual void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 // Gets quality stats for the channel.
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000883 virtual bool GetStats(VideoMediaInfo* info) = 0;
Jonas Oreland49ac5952018-09-26 16:04:32 +0200884
885 virtual std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886};
887
888enum DataMessageType {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000889 // Chrome-Internal use only. See SctpDataMediaChannel for the actual PPID
890 // values.
891 DMT_NONE = 0,
892 DMT_CONTROL = 1,
893 DMT_BINARY = 2,
894 DMT_TEXT = 3,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895};
896
897// Info about data received in DataMediaChannel. For use in
898// DataMediaChannel::SignalDataReceived and in all of the signals that
899// signal fires, on up the chain.
900struct ReceiveDataParams {
901 // The in-packet stream indentifier.
deadbeef953c2ce2017-01-09 14:53:41 -0800902 // RTP data channels use SSRCs, SCTP data channels use SIDs.
903 union {
904 uint32_t ssrc;
Steve Anton002f9212018-01-09 16:38:15 -0800905 int sid = 0;
deadbeef953c2ce2017-01-09 14:53:41 -0800906 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 // The type of message (binary, text, or control).
Steve Anton002f9212018-01-09 16:38:15 -0800908 DataMessageType type = DMT_TEXT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 // A per-stream value incremented per packet in the stream.
Steve Anton002f9212018-01-09 16:38:15 -0800910 int seq_num = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 // A per-stream value monotonically increasing with time.
Steve Anton002f9212018-01-09 16:38:15 -0800912 int timestamp = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913};
914
915struct SendDataParams {
916 // The in-packet stream indentifier.
deadbeef953c2ce2017-01-09 14:53:41 -0800917 // RTP data channels use SSRCs, SCTP data channels use SIDs.
918 union {
919 uint32_t ssrc;
Steve Anton002f9212018-01-09 16:38:15 -0800920 int sid = 0;
deadbeef953c2ce2017-01-09 14:53:41 -0800921 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 // The type of message (binary, text, or control).
Steve Anton002f9212018-01-09 16:38:15 -0800923 DataMessageType type = DMT_TEXT;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924
Steve Anton002f9212018-01-09 16:38:15 -0800925 // TODO(pthatcher): Make |ordered| and |reliable| true by default?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 // For SCTP, whether to send messages flagged as ordered or not.
927 // If false, messages can be received out of order.
Steve Anton002f9212018-01-09 16:38:15 -0800928 bool ordered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 // For SCTP, whether the messages are sent reliably or not.
930 // If false, messages may be lost.
Steve Anton002f9212018-01-09 16:38:15 -0800931 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 // For SCTP, if reliable == false, provide partial reliability by
933 // resending up to this many times. Either count or millis
934 // is supported, not both at the same time.
Steve Anton002f9212018-01-09 16:38:15 -0800935 int max_rtx_count = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 // For SCTP, if reliable == false, provide partial reliability by
937 // resending for up to this many milliseconds. Either count or millis
938 // is supported, not both at the same time.
Steve Anton002f9212018-01-09 16:38:15 -0800939 int max_rtx_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940};
941
942enum SendDataResult { SDR_SUCCESS, SDR_ERROR, SDR_BLOCK };
943
Yves Gerey665174f2018-06-19 15:03:05 +0200944struct DataSendParameters : RtpSendParameters<DataCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700945
Yves Gerey665174f2018-06-19 15:03:05 +0200946struct DataRecvParameters : RtpParameters<DataCodec> {};
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700947
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948class DataMediaChannel : public MediaChannel {
949 public:
Paulina Hensman11b34f42018-04-09 14:24:52 +0200950 DataMediaChannel();
951 explicit DataMediaChannel(const MediaConfig& config);
952 ~DataMediaChannel() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800954 cricket::MediaType media_type() const override;
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200955 virtual bool SetSendParameters(const DataSendParameters& params) = 0;
956 virtual bool SetRecvParameters(const DataRecvParameters& params) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000957
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800958 // RtpParameter methods are not supported for Data channel.
959 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
960 webrtc::RTCError SetRtpSendParameters(
961 uint32_t ssrc,
962 const webrtc::RtpParameters& parameters) override;
963
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 // TODO(pthatcher): Implement this.
Paulina Hensman11b34f42018-04-09 14:24:52 +0200965 virtual bool GetStats(DataMediaInfo* info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
967 virtual bool SetSend(bool send) = 0;
968 virtual bool SetReceive(bool receive) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969
Paulina Hensman11b34f42018-04-09 14:24:52 +0200970 void OnNetworkRouteChanged(const std::string& transport_name,
971 const rtc::NetworkRoute& network_route) override {}
Honghai Zhangcc411c02016-03-29 17:27:21 -0700972
Yves Gerey665174f2018-06-19 15:03:05 +0200973 virtual bool SendData(const SendDataParams& params,
974 const rtc::CopyOnWriteBuffer& payload,
975 SendDataResult* result = NULL) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 // Signals when data is received (params, data, len)
Yves Gerey665174f2018-06-19 15:03:05 +0200977 sigslot::signal3<const ReceiveDataParams&, const char*, size_t>
978 SignalDataReceived;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000979 // Signal when the media channel is ready to send the stream. Arguments are:
980 // writable(bool)
981 sigslot::signal1<bool> SignalReadyToSend;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982};
983
984} // namespace cricket
985
Steve Anton10542f22019-01-11 09:11:00 -0800986#endif // MEDIA_BASE_MEDIA_CHANNEL_H_