solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef AUDIO_AUDIO_SEND_STREAM_H_ |
| 12 | #define AUDIO_AUDIO_SEND_STREAM_H_ |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 13 | |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 14 | #include <memory> |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 15 | #include <vector> |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 16 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 17 | #include "audio/channel_send.h" |
Fredrik Solenberg | a8b7c7f | 2018-01-17 11:18:31 +0100 | [diff] [blame] | 18 | #include "audio/transport_feedback_packet_loss_tracker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 24 | #include "rtc_base/race_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread_checker.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
terelius | e035e2d | 2016-09-21 06:51:47 -0700 | [diff] [blame] | 28 | class RtcEventLog; |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 29 | class RtcpBandwidthObserver; |
michaelt | 9332b7d | 2016-11-30 07:51:13 -0800 | [diff] [blame] | 30 | class RtcpRttStats; |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 31 | class RtpTransportControllerSendInterface; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 32 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 33 | namespace internal { |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 34 | class AudioState; |
| 35 | |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 36 | class AudioSendStream final : public webrtc::AudioSendStream, |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 37 | public webrtc::BitrateAllocatorObserver, |
| 38 | public webrtc::PacketFeedbackObserver { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 39 | public: |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 40 | AudioSendStream(const webrtc::AudioSendStream::Config& config, |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 41 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 42 | rtc::TaskQueue* worker_queue, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 43 | ProcessThread* module_process_thread, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 44 | RtpTransportControllerSendInterface* rtp_transport, |
Niels Möller | 67b011d | 2018-10-22 13:00:40 +0200 | [diff] [blame] | 45 | BitrateAllocatorInterface* bitrate_allocator, |
michaelt | 9332b7d | 2016-11-30 07:51:13 -0800 | [diff] [blame] | 46 | RtcEventLog* event_log, |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 47 | RtcpRttStats* rtcp_rtt_stats, |
Sam Zackrisson | ff05816 | 2018-11-20 17:15:13 +0100 | [diff] [blame] | 48 | const absl::optional<RtpState>& suspended_rtp_state); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 49 | // For unit tests, which need to supply a mock ChannelSend. |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 50 | AudioSendStream(const webrtc::AudioSendStream::Config& config, |
| 51 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
| 52 | rtc::TaskQueue* worker_queue, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 53 | RtpTransportControllerSendInterface* rtp_transport, |
Niels Möller | 67b011d | 2018-10-22 13:00:40 +0200 | [diff] [blame] | 54 | BitrateAllocatorInterface* bitrate_allocator, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 55 | RtcEventLog* event_log, |
| 56 | RtcpRttStats* rtcp_rtt_stats, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 57 | const absl::optional<RtpState>& suspended_rtp_state, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 58 | std::unique_ptr<voe::ChannelSendInterface> channel_send); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 59 | ~AudioSendStream() override; |
| 60 | |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 61 | // webrtc::AudioSendStream implementation. |
eladalon | abbc430 | 2017-07-26 02:09:44 -0700 | [diff] [blame] | 62 | const webrtc::AudioSendStream::Config& GetConfig() const override; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 63 | void Reconfigure(const webrtc::AudioSendStream::Config& config) override; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 64 | void Start() override; |
| 65 | void Stop() override; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 66 | void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | bool SendTelephoneEvent(int payload_type, |
| 68 | int payload_frequency, |
| 69 | int event, |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 70 | int duration_ms) override; |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 71 | void SetMuted(bool muted) override; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 72 | webrtc::AudioSendStream::Stats GetStats() const override; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 73 | webrtc::AudioSendStream::Stats GetStats( |
| 74 | bool has_remote_tracks) const override; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 75 | |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 76 | void SignalNetworkState(NetworkState state); |
| 77 | bool DeliverRtcp(const uint8_t* packet, size_t length); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 78 | |
| 79 | // Implements BitrateAllocatorObserver. |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 80 | uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override; |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 81 | |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 82 | // From PacketFeedbackObserver. |
| 83 | void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override; |
| 84 | void OnPacketFeedbackVector( |
| 85 | const std::vector<PacketFeedback>& packet_feedback_vector) override; |
| 86 | |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 87 | void SetTransportOverhead(int transport_overhead_per_packet); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 88 | |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 89 | RtpState GetRtpState() const; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 90 | const voe::ChannelSendInterface* GetChannel() const; |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 91 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 92 | private: |
saza | c58f8c0 | 2017-07-19 00:39:19 -0700 | [diff] [blame] | 93 | class TimedTransport; |
| 94 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 95 | internal::AudioState* audio_state(); |
| 96 | const internal::AudioState* audio_state() const; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 97 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 98 | void StoreEncoderProperties(int sample_rate_hz, size_t num_channels); |
| 99 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 100 | // These are all static to make it less likely that (the old) config_ is |
| 101 | // accessed unintentionally. |
| 102 | static void ConfigureStream(AudioSendStream* stream, |
| 103 | const Config& new_config, |
| 104 | bool first_time); |
| 105 | static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config); |
| 106 | static bool ReconfigureSendCodec(AudioSendStream* stream, |
| 107 | const Config& new_config); |
| 108 | static void ReconfigureANA(AudioSendStream* stream, const Config& new_config); |
| 109 | static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config); |
| 110 | static void ReconfigureBitrateObserver(AudioSendStream* stream, |
| 111 | const Config& new_config); |
| 112 | |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 113 | void ConfigureBitrateObserver(int min_bitrate_bps, |
| 114 | int max_bitrate_bps, |
Sebastian Jansson | 763e947 | 2018-03-21 12:46:56 +0100 | [diff] [blame] | 115 | double bitrate_priority, |
| 116 | bool has_packet_feedback); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 117 | void RemoveBitrateObserver(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 118 | |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 119 | void RegisterCngPayloadType(int payload_type, int clockrate_hz); |
| 120 | |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 121 | rtc::ThreadChecker worker_thread_checker_; |
| 122 | rtc::ThreadChecker pacer_thread_checker_; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 123 | rtc::RaceChecker audio_capture_race_checker_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 124 | rtc::TaskQueue* worker_queue_; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 125 | webrtc::AudioSendStream::Config config_; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 126 | rtc::scoped_refptr<webrtc::AudioState> audio_state_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 127 | const std::unique_ptr<voe::ChannelSendInterface> channel_send_; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 128 | RtcEventLog* const event_log_; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 129 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 130 | int encoder_sample_rate_hz_ = 0; |
| 131 | size_t encoder_num_channels_ = 0; |
| 132 | bool sending_ = false; |
| 133 | |
Niels Möller | 67b011d | 2018-10-22 13:00:40 +0200 | [diff] [blame] | 134 | BitrateAllocatorInterface* const bitrate_allocator_; |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 135 | RtpTransportControllerSendInterface* const rtp_transport_; |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 136 | |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 137 | rtc::CriticalSection packet_loss_tracker_cs_; |
| 138 | TransportFeedbackPacketLossTracker packet_loss_tracker_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 139 | RTC_GUARDED_BY(&packet_loss_tracker_cs_); |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 140 | |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 141 | RtpRtcp* rtp_rtcp_module_; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 142 | absl::optional<RtpState> const suspended_rtp_state_; |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 143 | |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 144 | // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is |
| 145 | // reserved for padding and MUST NOT be used as a local identifier. |
| 146 | // So it should be safe to use 0 here to indicate "not configured". |
| 147 | struct ExtensionIds { |
| 148 | int audio_level = 0; |
| 149 | int transport_sequence_number = 0; |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 150 | int mid = 0; |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 151 | int rid = 0; |
| 152 | int repaired_rid = 0; |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 153 | }; |
| 154 | static ExtensionIds FindExtensionIds( |
| 155 | const std::vector<RtpExtension>& extensions); |
| 156 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 157 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 158 | }; |
| 159 | } // namespace internal |
| 160 | } // namespace webrtc |
| 161 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 162 | #endif // AUDIO_AUDIO_SEND_STREAM_H_ |