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