xref: /aosp_15_r20/external/webrtc/call/audio_send_stream.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef CALL_AUDIO_SEND_STREAM_H_
12*d9f75844SAndroid Build Coastguard Worker #define CALL_AUDIO_SEND_STREAM_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker #include <vector>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_codec_pair_id.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_encoder.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_encoder_factory.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_format.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/call/transport.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/crypto/crypto_options.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/crypto/frame_encryptor_interface.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/frame_transformer_interface.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_parameters.h"
28*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_sender_interface.h"
29*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
30*d9f75844SAndroid Build Coastguard Worker #include "call/audio_sender.h"
31*d9f75844SAndroid Build Coastguard Worker #include "call/rtp_config.h"
32*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/include/audio_processing_statistics.h"
33*d9f75844SAndroid Build Coastguard Worker #include "modules/rtp_rtcp/include/report_block_data.h"
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker class AudioSendStream : public AudioSender {
38*d9f75844SAndroid Build Coastguard Worker  public:
39*d9f75844SAndroid Build Coastguard Worker   struct Stats {
40*d9f75844SAndroid Build Coastguard Worker     Stats();
41*d9f75844SAndroid Build Coastguard Worker     ~Stats();
42*d9f75844SAndroid Build Coastguard Worker 
43*d9f75844SAndroid Build Coastguard Worker     // TODO(solenberg): Harmonize naming and defaults with receive stream stats.
44*d9f75844SAndroid Build Coastguard Worker     uint32_t local_ssrc = 0;
45*d9f75844SAndroid Build Coastguard Worker     int64_t payload_bytes_sent = 0;
46*d9f75844SAndroid Build Coastguard Worker     int64_t header_and_padding_bytes_sent = 0;
47*d9f75844SAndroid Build Coastguard Worker     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedbytessent
48*d9f75844SAndroid Build Coastguard Worker     uint64_t retransmitted_bytes_sent = 0;
49*d9f75844SAndroid Build Coastguard Worker     int32_t packets_sent = 0;
50*d9f75844SAndroid Build Coastguard Worker     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalpacketsenddelay
51*d9f75844SAndroid Build Coastguard Worker     TimeDelta total_packet_send_delay = TimeDelta::Zero();
52*d9f75844SAndroid Build Coastguard Worker     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedpacketssent
53*d9f75844SAndroid Build Coastguard Worker     uint64_t retransmitted_packets_sent = 0;
54*d9f75844SAndroid Build Coastguard Worker     int32_t packets_lost = -1;
55*d9f75844SAndroid Build Coastguard Worker     float fraction_lost = -1.0f;
56*d9f75844SAndroid Build Coastguard Worker     std::string codec_name;
57*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> codec_payload_type;
58*d9f75844SAndroid Build Coastguard Worker     int32_t jitter_ms = -1;
59*d9f75844SAndroid Build Coastguard Worker     int64_t rtt_ms = -1;
60*d9f75844SAndroid Build Coastguard Worker     int16_t audio_level = 0;
61*d9f75844SAndroid Build Coastguard Worker     // See description of "totalAudioEnergy" in the WebRTC stats spec:
62*d9f75844SAndroid Build Coastguard Worker     // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
63*d9f75844SAndroid Build Coastguard Worker     double total_input_energy = 0.0;
64*d9f75844SAndroid Build Coastguard Worker     double total_input_duration = 0.0;
65*d9f75844SAndroid Build Coastguard Worker 
66*d9f75844SAndroid Build Coastguard Worker     ANAStats ana_statistics;
67*d9f75844SAndroid Build Coastguard Worker     AudioProcessingStats apm_statistics;
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker     int64_t target_bitrate_bps = 0;
70*d9f75844SAndroid Build Coastguard Worker     // A snapshot of Report Blocks with additional data of interest to
71*d9f75844SAndroid Build Coastguard Worker     // statistics. Within this list, the sender-source SSRC pair is unique and
72*d9f75844SAndroid Build Coastguard Worker     // per-pair the ReportBlockData represents the latest Report Block that was
73*d9f75844SAndroid Build Coastguard Worker     // received for that pair.
74*d9f75844SAndroid Build Coastguard Worker     std::vector<ReportBlockData> report_block_datas;
75*d9f75844SAndroid Build Coastguard Worker     uint32_t nacks_rcvd = 0;
76*d9f75844SAndroid Build Coastguard Worker   };
77*d9f75844SAndroid Build Coastguard Worker 
78*d9f75844SAndroid Build Coastguard Worker   struct Config {
79*d9f75844SAndroid Build Coastguard Worker     Config() = delete;
80*d9f75844SAndroid Build Coastguard Worker     explicit Config(Transport* send_transport);
81*d9f75844SAndroid Build Coastguard Worker     ~Config();
82*d9f75844SAndroid Build Coastguard Worker     std::string ToString() const;
83*d9f75844SAndroid Build Coastguard Worker 
84*d9f75844SAndroid Build Coastguard Worker     // Send-stream specific RTP settings.
85*d9f75844SAndroid Build Coastguard Worker     struct Rtp {
86*d9f75844SAndroid Build Coastguard Worker       Rtp();
87*d9f75844SAndroid Build Coastguard Worker       ~Rtp();
88*d9f75844SAndroid Build Coastguard Worker       std::string ToString() const;
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker       // Sender SSRC.
91*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc = 0;
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker       // The value to send in the RID RTP header extension if the extension is
94*d9f75844SAndroid Build Coastguard Worker       // included in the list of extensions.
95*d9f75844SAndroid Build Coastguard Worker       std::string rid;
96*d9f75844SAndroid Build Coastguard Worker 
97*d9f75844SAndroid Build Coastguard Worker       // The value to send in the MID RTP header extension if the extension is
98*d9f75844SAndroid Build Coastguard Worker       // included in the list of extensions.
99*d9f75844SAndroid Build Coastguard Worker       std::string mid;
100*d9f75844SAndroid Build Coastguard Worker 
101*d9f75844SAndroid Build Coastguard Worker       // Corresponds to the SDP attribute extmap-allow-mixed.
102*d9f75844SAndroid Build Coastguard Worker       bool extmap_allow_mixed = false;
103*d9f75844SAndroid Build Coastguard Worker 
104*d9f75844SAndroid Build Coastguard Worker       // RTP header extensions used for the sent stream.
105*d9f75844SAndroid Build Coastguard Worker       std::vector<RtpExtension> extensions;
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker       // RTCP CNAME, see RFC 3550.
108*d9f75844SAndroid Build Coastguard Worker       std::string c_name;
109*d9f75844SAndroid Build Coastguard Worker     } rtp;
110*d9f75844SAndroid Build Coastguard Worker 
111*d9f75844SAndroid Build Coastguard Worker     // Time interval between RTCP report for audio
112*d9f75844SAndroid Build Coastguard Worker     int rtcp_report_interval_ms = 5000;
113*d9f75844SAndroid Build Coastguard Worker 
114*d9f75844SAndroid Build Coastguard Worker     // Transport for outgoing packets. The transport is expected to exist for
115*d9f75844SAndroid Build Coastguard Worker     // the entire life of the AudioSendStream and is owned by the API client.
116*d9f75844SAndroid Build Coastguard Worker     Transport* send_transport = nullptr;
117*d9f75844SAndroid Build Coastguard Worker 
118*d9f75844SAndroid Build Coastguard Worker     // Bitrate limits used for variable audio bitrate streams. Set both to -1 to
119*d9f75844SAndroid Build Coastguard Worker     // disable audio bitrate adaptation.
120*d9f75844SAndroid Build Coastguard Worker     // Note: This is still an experimental feature and not ready for real usage.
121*d9f75844SAndroid Build Coastguard Worker     int min_bitrate_bps = -1;
122*d9f75844SAndroid Build Coastguard Worker     int max_bitrate_bps = -1;
123*d9f75844SAndroid Build Coastguard Worker 
124*d9f75844SAndroid Build Coastguard Worker     double bitrate_priority = 1.0;
125*d9f75844SAndroid Build Coastguard Worker     bool has_dscp = false;
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker     // Defines whether to turn on audio network adaptor, and defines its config
128*d9f75844SAndroid Build Coastguard Worker     // string.
129*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::string> audio_network_adaptor_config;
130*d9f75844SAndroid Build Coastguard Worker 
131*d9f75844SAndroid Build Coastguard Worker     struct SendCodecSpec {
132*d9f75844SAndroid Build Coastguard Worker       SendCodecSpec(int payload_type, const SdpAudioFormat& format);
133*d9f75844SAndroid Build Coastguard Worker       ~SendCodecSpec();
134*d9f75844SAndroid Build Coastguard Worker       std::string ToString() const;
135*d9f75844SAndroid Build Coastguard Worker 
136*d9f75844SAndroid Build Coastguard Worker       bool operator==(const SendCodecSpec& rhs) const;
137*d9f75844SAndroid Build Coastguard Worker       bool operator!=(const SendCodecSpec& rhs) const {
138*d9f75844SAndroid Build Coastguard Worker         return !(*this == rhs);
139*d9f75844SAndroid Build Coastguard Worker       }
140*d9f75844SAndroid Build Coastguard Worker 
141*d9f75844SAndroid Build Coastguard Worker       int payload_type;
142*d9f75844SAndroid Build Coastguard Worker       SdpAudioFormat format;
143*d9f75844SAndroid Build Coastguard Worker       bool nack_enabled = false;
144*d9f75844SAndroid Build Coastguard Worker       bool transport_cc_enabled = false;
145*d9f75844SAndroid Build Coastguard Worker       bool enable_non_sender_rtt = false;
146*d9f75844SAndroid Build Coastguard Worker       absl::optional<int> cng_payload_type;
147*d9f75844SAndroid Build Coastguard Worker       absl::optional<int> red_payload_type;
148*d9f75844SAndroid Build Coastguard Worker       // If unset, use the encoder's default target bitrate.
149*d9f75844SAndroid Build Coastguard Worker       absl::optional<int> target_bitrate_bps;
150*d9f75844SAndroid Build Coastguard Worker     };
151*d9f75844SAndroid Build Coastguard Worker 
152*d9f75844SAndroid Build Coastguard Worker     absl::optional<SendCodecSpec> send_codec_spec;
153*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<AudioEncoderFactory> encoder_factory;
154*d9f75844SAndroid Build Coastguard Worker     absl::optional<AudioCodecPairId> codec_pair_id;
155*d9f75844SAndroid Build Coastguard Worker 
156*d9f75844SAndroid Build Coastguard Worker     // Track ID as specified during track creation.
157*d9f75844SAndroid Build Coastguard Worker     std::string track_id;
158*d9f75844SAndroid Build Coastguard Worker 
159*d9f75844SAndroid Build Coastguard Worker     // Per PeerConnection crypto options.
160*d9f75844SAndroid Build Coastguard Worker     webrtc::CryptoOptions crypto_options;
161*d9f75844SAndroid Build Coastguard Worker 
162*d9f75844SAndroid Build Coastguard Worker     // An optional custom frame encryptor that allows the entire frame to be
163*d9f75844SAndroid Build Coastguard Worker     // encryptor in whatever way the caller choses. This is not required by
164*d9f75844SAndroid Build Coastguard Worker     // default.
165*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor;
166*d9f75844SAndroid Build Coastguard Worker 
167*d9f75844SAndroid Build Coastguard Worker     // An optional frame transformer used by insertable streams to transform
168*d9f75844SAndroid Build Coastguard Worker     // encoded frames.
169*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
170*d9f75844SAndroid Build Coastguard Worker   };
171*d9f75844SAndroid Build Coastguard Worker 
172*d9f75844SAndroid Build Coastguard Worker   virtual ~AudioSendStream() = default;
173*d9f75844SAndroid Build Coastguard Worker 
174*d9f75844SAndroid Build Coastguard Worker   virtual const webrtc::AudioSendStream::Config& GetConfig() const = 0;
175*d9f75844SAndroid Build Coastguard Worker 
176*d9f75844SAndroid Build Coastguard Worker   // Reconfigure the stream according to the Configuration.
177*d9f75844SAndroid Build Coastguard Worker   virtual void Reconfigure(const Config& config,
178*d9f75844SAndroid Build Coastguard Worker                            SetParametersCallback callback) = 0;
179*d9f75844SAndroid Build Coastguard Worker 
180*d9f75844SAndroid Build Coastguard Worker   // Starts stream activity.
181*d9f75844SAndroid Build Coastguard Worker   // When a stream is active, it can receive, process and deliver packets.
182*d9f75844SAndroid Build Coastguard Worker   virtual void Start() = 0;
183*d9f75844SAndroid Build Coastguard Worker   // Stops stream activity.
184*d9f75844SAndroid Build Coastguard Worker   // When a stream is stopped, it can't receive, process or deliver packets.
185*d9f75844SAndroid Build Coastguard Worker   virtual void Stop() = 0;
186*d9f75844SAndroid Build Coastguard Worker 
187*d9f75844SAndroid Build Coastguard Worker   // TODO(solenberg): Make payload_type a config property instead.
188*d9f75844SAndroid Build Coastguard Worker   virtual bool SendTelephoneEvent(int payload_type,
189*d9f75844SAndroid Build Coastguard Worker                                   int payload_frequency,
190*d9f75844SAndroid Build Coastguard Worker                                   int event,
191*d9f75844SAndroid Build Coastguard Worker                                   int duration_ms) = 0;
192*d9f75844SAndroid Build Coastguard Worker 
193*d9f75844SAndroid Build Coastguard Worker   virtual void SetMuted(bool muted) = 0;
194*d9f75844SAndroid Build Coastguard Worker 
195*d9f75844SAndroid Build Coastguard Worker   virtual Stats GetStats() const = 0;
196*d9f75844SAndroid Build Coastguard Worker   virtual Stats GetStats(bool has_remote_tracks) const = 0;
197*d9f75844SAndroid Build Coastguard Worker };
198*d9f75844SAndroid Build Coastguard Worker 
199*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
200*d9f75844SAndroid Build Coastguard Worker 
201*d9f75844SAndroid Build Coastguard Worker #endif  // CALL_AUDIO_SEND_STREAM_H_
202