blob: eb8c952f7f4724599d8c5561749f8e95859f36f5 [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef AUDIO_AUDIO_SEND_STREAM_H_
12#define AUDIO_AUDIO_SEND_STREAM_H_
solenbergc7a8b082015-10-16 14:35:07 -070013
kwibergfffa42b2016-02-23 10:46:32 -080014#include <memory>
elad.alond12a8e12017-03-23 11:04:48 -070015#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080016
Niels Möllerdced9f62018-11-19 10:27:07 +010017#include "audio/channel_send.h"
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010018#include "audio/transport_feedback_packet_loss_tracker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "call/audio_send_stream.h"
20#include "call/audio_state.h"
21#include "call/bitrate_allocator.h"
22#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
Sebastian Jansson470a5ea2019-01-23 12:37:49 +010024#include "rtc_base/experiments/audio_allocation_settings.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010025#include "rtc_base/race_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread_checker.h"
solenbergc7a8b082015-10-16 14:35:07 -070027
28namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070029class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080030class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080031class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070032class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080033
solenberg13725082015-11-25 08:16:52 -080034namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010035class AudioState;
36
mflodman86cc6ff2016-07-26 04:44:06 -070037class AudioSendStream final : public webrtc::AudioSendStream,
elad.alond12a8e12017-03-23 11:04:48 -070038 public webrtc::BitrateAllocatorObserver,
39 public webrtc::PacketFeedbackObserver {
solenbergc7a8b082015-10-16 14:35:07 -070040 public:
solenberg85a04962015-10-27 03:35:21 -070041 AudioSendStream(const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010042 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070043 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010044 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020045 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020046 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080047 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070048 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010049 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010050 // For unit tests, which need to supply a mock ChannelSend.
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010051 AudioSendStream(const webrtc::AudioSendStream::Config& config,
52 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
53 rtc::TaskQueue* worker_queue,
Niels Möller7d76a312018-10-26 12:57:07 +020054 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020055 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010056 RtcEventLog* event_log,
57 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020058 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010059 std::unique_ptr<voe::ChannelSendInterface> channel_send);
solenbergc7a8b082015-10-16 14:35:07 -070060 ~AudioSendStream() override;
61
pbos1ba8d392016-05-01 20:18:34 -070062 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070063 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070064 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070065 void Start() override;
66 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010067 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020068 bool SendTelephoneEvent(int payload_type,
69 int payload_frequency,
70 int event,
solenberg8842c3e2016-03-11 03:06:41 -080071 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070072 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070073 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010074 webrtc::AudioSendStream::Stats GetStats(
75 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070076
pbos1ba8d392016-05-01 20:18:34 -070077 void SignalNetworkState(NetworkState state);
78 bool DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070079
80 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020081 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -070082
elad.alond12a8e12017-03-23 11:04:48 -070083 // From PacketFeedbackObserver.
84 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
85 void OnPacketFeedbackVector(
86 const std::vector<PacketFeedback>& packet_feedback_vector) override;
87
michaelt79e05882016-11-08 02:50:09 -080088 void SetTransportOverhead(int transport_overhead_per_packet);
solenbergc7a8b082015-10-16 14:35:07 -070089
ossuc3d4b482017-05-23 06:07:11 -070090 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +010091 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -070092
solenbergc7a8b082015-10-16 14:35:07 -070093 private:
sazac58f8c02017-07-19 00:39:19 -070094 class TimedTransport;
95
Fredrik Solenberg2a877972017-12-15 16:42:15 +010096 internal::AudioState* audio_state();
97 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -080098
Fredrik Solenberg2a877972017-12-15 16:42:15 +010099 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
100
ossu20a4b3f2017-04-27 02:08:52 -0700101 // These are all static to make it less likely that (the old) config_ is
102 // accessed unintentionally.
103 static void ConfigureStream(AudioSendStream* stream,
104 const Config& new_config,
105 bool first_time);
106 static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config);
107 static bool ReconfigureSendCodec(AudioSendStream* stream,
108 const Config& new_config);
109 static void ReconfigureANA(AudioSendStream* stream, const Config& new_config);
110 static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config);
111 static void ReconfigureBitrateObserver(AudioSendStream* stream,
112 const Config& new_config);
113
Seth Hampson24722b32017-12-22 09:36:42 -0800114 void ConfigureBitrateObserver(int min_bitrate_bps,
115 int max_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100116 double bitrate_priority);
ossu20a4b3f2017-04-27 02:08:52 -0700117 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700118
ossu3b9ff382017-04-27 08:03:42 -0700119 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
120
elad.alond12a8e12017-03-23 11:04:48 -0700121 rtc::ThreadChecker worker_thread_checker_;
122 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100123 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700124 rtc::TaskQueue* worker_queue_;
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100125 const AudioAllocationSettings allocation_settings_;
ossu20a4b3f2017-04-27 02:08:52 -0700126 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800127 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100128 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700129 RtcEventLog* const event_log_;
solenberg85a04962015-10-27 03:35:21 -0700130
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100131 int encoder_sample_rate_hz_ = 0;
132 size_t encoder_num_channels_ = 0;
133 bool sending_ = false;
134
Niels Möller67b011d2018-10-22 13:00:40 +0200135 BitrateAllocatorInterface* const bitrate_allocator_;
Niels Möller7d76a312018-10-26 12:57:07 +0200136 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700137
elad.alond12a8e12017-03-23 11:04:48 -0700138 rtc::CriticalSection packet_loss_tracker_cs_;
139 TransportFeedbackPacketLossTracker packet_loss_tracker_
danilchapa37de392017-09-09 04:17:22 -0700140 RTC_GUARDED_BY(&packet_loss_tracker_cs_);
elad.alond12a8e12017-03-23 11:04:48 -0700141
ossuc3d4b482017-05-23 06:07:11 -0700142 RtpRtcp* rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200143 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700144
Alex Narestcedd3512017-12-07 20:54:55 +0100145 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
146 // reserved for padding and MUST NOT be used as a local identifier.
147 // So it should be safe to use 0 here to indicate "not configured".
148 struct ExtensionIds {
149 int audio_level = 0;
150 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700151 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800152 int rid = 0;
153 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100154 };
155 static ExtensionIds FindExtensionIds(
156 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100157 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100158
solenberg85a04962015-10-27 03:35:21 -0700159 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700160};
161} // namespace internal
162} // namespace webrtc
163
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200164#endif // AUDIO_AUDIO_SEND_STREAM_H_