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