blob: c51c7a3b2f015b1296395fd915857cf2ee82f38d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "audio/time_interval.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"
23#include "rtc_base/constructormagic.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010024#include "rtc_base/race_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread_checker.h"
solenbergc7a8b082015-10-16 14:35:07 -070026
27namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070028class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080029class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080030class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070031class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080032
solenberg13725082015-11-25 08:16:52 -080033namespace voe {
34class ChannelProxy;
35} // namespace voe
solenbergc7a8b082015-10-16 14:35:07 -070036
solenberg13725082015-11-25 08:16:52 -080037namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010038class AudioState;
39
mflodman86cc6ff2016-07-26 04:44:06 -070040class AudioSendStream final : public webrtc::AudioSendStream,
elad.alond12a8e12017-03-23 11:04:48 -070041 public webrtc::BitrateAllocatorObserver,
42 public webrtc::PacketFeedbackObserver {
solenbergc7a8b082015-10-16 14:35:07 -070043 public:
solenberg85a04962015-10-27 03:35:21 -070044 AudioSendStream(const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010045 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070046 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010047 ProcessThread* module_process_thread,
nisseb8f9a322017-03-27 05:36:15 -070048 RtpTransportControllerSendInterface* transport,
tereliuse035e2d2016-09-21 06:51:47 -070049 BitrateAllocator* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080050 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070051 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrisson06953ba2018-02-01 16:53:16 +010052 const rtc::Optional<RtpState>& suspended_rtp_state,
53 TimeInterval* overall_call_lifetime);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010054 // 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 Zackrisson06953ba2018-02-01 16:53:16 +010063 TimeInterval* overall_call_lifetime,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010064 std::unique_ptr<voe::ChannelProxy> channel_proxy);
solenbergc7a8b082015-10-16 14:35:07 -070065 ~AudioSendStream() override;
66
pbos1ba8d392016-05-01 20:18:34 -070067 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070068 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070069 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070070 void Start() override;
71 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010072 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
solenbergffbbcac2016-11-17 05:25:37 -080073 bool SendTelephoneEvent(int payload_type, int payload_frequency, int event,
solenberg8842c3e2016-03-11 03:06:41 -080074 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070075 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070076 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010077 webrtc::AudioSendStream::Stats GetStats(
78 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070079
pbos1ba8d392016-05-01 20:18:34 -070080 void SignalNetworkState(NetworkState state);
81 bool DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070082
83 // Implements BitrateAllocatorObserver.
84 uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
85 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080086 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070087 int64_t bwe_period_ms) override;
mflodman86cc6ff2016-07-26 04:44:06 -070088
elad.alond12a8e12017-03-23 11:04:48 -070089 // 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
michaelt79e05882016-11-08 02:50:09 -080094 void SetTransportOverhead(int transport_overhead_per_packet);
solenbergc7a8b082015-10-16 14:35:07 -070095
ossuc3d4b482017-05-23 06:07:11 -070096 RtpState GetRtpState() const;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010097 const voe::ChannelProxy& GetChannelProxy() const;
ossuc3d4b482017-05-23 06:07:11 -070098
solenbergc7a8b082015-10-16 14:35:07 -070099 private:
sazac58f8c02017-07-19 00:39:19 -0700100 class TimedTransport;
101
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100102 internal::AudioState* audio_state();
103 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800104
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100105 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
106
ossu20a4b3f2017-04-27 02:08:52 -0700107 // 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 Hampson24722b32017-12-22 09:36:42 -0800120 void ConfigureBitrateObserver(int min_bitrate_bps,
121 int max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100122 double bitrate_priority,
123 bool has_packet_feedback);
ossu20a4b3f2017-04-27 02:08:52 -0700124 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700125
ossu3b9ff382017-04-27 08:03:42 -0700126 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
127
elad.alond12a8e12017-03-23 11:04:48 -0700128 rtc::ThreadChecker worker_thread_checker_;
129 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100130 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700131 rtc::TaskQueue* worker_queue_;
ossu20a4b3f2017-04-27 02:08:52 -0700132 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800133 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
kwibergfffa42b2016-02-23 10:46:32 -0800134 std::unique_ptr<voe::ChannelProxy> channel_proxy_;
ossu20a4b3f2017-04-27 02:08:52 -0700135 RtcEventLog* const event_log_;
solenberg85a04962015-10-27 03:35:21 -0700136
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100137 int encoder_sample_rate_hz_ = 0;
138 size_t encoder_num_channels_ = 0;
139 bool sending_ = false;
140
mflodman86cc6ff2016-07-26 04:44:06 -0700141 BitrateAllocator* const bitrate_allocator_;
nisseb8f9a322017-03-27 05:36:15 -0700142 RtpTransportControllerSendInterface* const transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700143
elad.alond12a8e12017-03-23 11:04:48 -0700144 rtc::CriticalSection packet_loss_tracker_cs_;
145 TransportFeedbackPacketLossTracker packet_loss_tracker_
danilchapa37de392017-09-09 04:17:22 -0700146 RTC_GUARDED_BY(&packet_loss_tracker_cs_);
elad.alond12a8e12017-03-23 11:04:48 -0700147
ossuc3d4b482017-05-23 06:07:11 -0700148 RtpRtcp* rtp_rtcp_module_;
149 rtc::Optional<RtpState> const suspended_rtp_state_;
150
sazac58f8c02017-07-19 00:39:19 -0700151 std::unique_ptr<TimedTransport> timed_send_transport_adapter_;
152 TimeInterval active_lifetime_;
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100153 TimeInterval* overall_call_lifetime_ = nullptr;
sazac58f8c02017-07-19 00:39:19 -0700154
Alex Narestcedd3512017-12-07 20:54:55 +0100155 // 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 Antonbb50ce52018-03-26 10:24:32 -0700161 int mid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100162 };
163 static ExtensionIds FindExtensionIds(
164 const std::vector<RtpExtension>& extensions);
165
solenberg85a04962015-10-27 03:35:21 -0700166 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700167};
168} // namespace internal
169} // namespace webrtc
170
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200171#endif // AUDIO_AUDIO_SEND_STREAM_H_