blob: 4f63155377936a4eaaaa7234a8a77b667ac86544 [file] [log] [blame]
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001/*
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_RECEIVE_STREAM_H_
12#define AUDIO_AUDIO_RECEIVE_STREAM_H_
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020013
Tommi6eda26c2021-06-09 13:46:28 +020014#include <map>
kwibergfffa42b2016-02-23 10:46:32 -080015#include <memory>
hbos8d609f62017-04-10 07:39:05 -070016#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio/audio_mixer.h"
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010019#include "api/neteq/neteq_factory.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020020#include "api/rtp_headers.h"
Artem Titovd15a5752021-02-10 14:31:24 +010021#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "audio/audio_state.h"
23#include "call/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "call/syncable.h"
Chen Xing054e3bb2019-08-02 10:29:26 +000025#include "modules/rtp_rtcp/source/source_tracker.h"
Tommi02df2eb2021-05-31 12:57:53 +020026#include "rtc_base/system/no_unique_address.h"
Sebastian Jansson977b3352019-03-04 17:43:34 +010027#include "system_wrappers/include/clock.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020028
29namespace webrtc {
solenberg3ebbcb52017-01-31 03:58:40 -080030class PacketRouter;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010031class ProcessThread;
ivoc14d5dbe2016-07-04 07:06:55 -070032class RtcEventLog;
nisse657bab22017-02-21 06:28:10 -080033class RtpPacketReceived;
nisse0f15f922017-06-21 01:05:22 -070034class RtpStreamReceiverControllerInterface;
35class RtpStreamReceiverInterface;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020036
solenberg13725082015-11-25 08:16:52 -080037namespace voe {
Niels Möller349ade32018-11-16 09:50:42 +010038class ChannelReceiveInterface;
solenberg13725082015-11-25 08:16:52 -080039} // namespace voe
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020040
solenberg13725082015-11-25 08:16:52 -080041namespace internal {
solenberg7602aab2016-11-14 11:30:07 -080042class AudioSendStream;
Tommif888bb52015-12-12 01:37:01 +010043
aleloiaed581a2016-10-20 06:32:39 -070044class AudioReceiveStream final : public webrtc::AudioReceiveStream,
solenberg3ebbcb52017-01-31 03:58:40 -080045 public AudioMixer::Source,
nisse0f15f922017-06-21 01:05:22 -070046 public Syncable {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020047 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010048 AudioReceiveStream(Clock* clock,
nisse0f15f922017-06-21 01:05:22 -070049 PacketRouter* packet_router,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010050 ProcessThread* module_process_thread,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010051 NetEqFactory* neteq_factory,
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020052 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070053 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
54 webrtc::RtcEventLog* event_log);
Niels Möller349ade32018-11-16 09:50:42 +010055 // For unit tests, which need to supply a mock channel receive.
56 AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +010057 Clock* clock,
Niels Möller349ade32018-11-16 09:50:42 +010058 PacketRouter* packet_router,
59 const webrtc::AudioReceiveStream::Config& config,
60 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
61 webrtc::RtcEventLog* event_log,
62 std::unique_ptr<voe::ChannelReceiveInterface> channel_receive);
Niels Möllerde953292020-09-29 09:46:21 +020063
64 AudioReceiveStream() = delete;
65 AudioReceiveStream(const AudioReceiveStream&) = delete;
66 AudioReceiveStream& operator=(const AudioReceiveStream&) = delete;
67
Tommi02df2eb2021-05-31 12:57:53 +020068 // Destruction happens on the worker thread. Prior to destruction the caller
69 // must ensure that a registration with the transport has been cleared. See
70 // `RegisterWithTransport` for details.
71 // TODO(tommi): As a further improvement to this, performing the full
72 // destruction on the network thread could be made the default.
pbosa2f30de2015-10-15 05:22:13 -070073 ~AudioReceiveStream() override;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020074
Tommi02df2eb2021-05-31 12:57:53 +020075 // Called on the network thread to register/unregister with the network
76 // transport.
77 void RegisterWithTransport(
78 RtpStreamReceiverControllerInterface* receiver_controller);
79 // If registration has previously been done (via `RegisterWithTransport`) then
80 // `UnregisterFromTransport` must be called prior to destruction, on the
81 // network thread.
82 void UnregisterFromTransport();
83
pbos1ba8d392016-05-01 20:18:34 -070084 // webrtc::AudioReceiveStream implementation.
Jelena Marusiccd670222015-07-16 09:30:09 +020085 void Start() override;
86 void Stop() override;
Tomas Gunnarsson8467cf22021-01-17 14:36:44 +010087 bool IsRunning() const override;
Tommi6eda26c2021-06-09 13:46:28 +020088 void SetDepacketizerToDecoderFrameTransformer(
89 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
90 override;
91 void SetDecoderMap(std::map<int, SdpAudioFormat> decoder_map) override;
92 void SetUseTransportCcAndNackHistory(bool use_transport_cc,
93 int history_ms) override;
Tomas Gunnarsson8467cf22021-01-17 14:36:44 +010094
Niels Möller6b4d9622020-09-14 10:47:50 +020095 webrtc::AudioReceiveStream::Stats GetStats(
96 bool get_and_clear_legacy_stats) const override;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010097 void SetSink(AudioSinkInterface* sink) override;
solenberg217fb662016-06-17 08:30:54 -070098 void SetGain(float gain) override;
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +010099 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
100 int GetBaseMinimumPlayoutDelayMs() const override;
hbos8d609f62017-04-10 07:39:05 -0700101 std::vector<webrtc::RtpSource> GetSources() const override;
Tommif888bb52015-12-12 01:37:01 +0100102
solenberg3ebbcb52017-01-31 03:58:40 -0800103 // AudioMixer::Source
104 AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
105 AudioFrame* audio_frame) override;
106 int Ssrc() const override;
107 int PreferredSampleRate() const override;
108
109 // Syncable
Åsa Persson74d2b1d2020-02-10 16:33:29 +0100110 uint32_t id() const override;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200111 absl::optional<Syncable::Info> GetInfo() const override;
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200112 bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
113 int64_t* time_ms) const override;
114 void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
115 int64_t time_ms) override;
Ivo Creusenbef7b052020-09-08 16:30:25 +0200116 bool SetMinimumPlayoutDelay(int delay_ms) override;
solenberg3ebbcb52017-01-31 03:58:40 -0800117
solenberg7602aab2016-11-14 11:30:07 -0800118 void AssociateSendStream(AudioSendStream* send_stream);
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100119 void DeliverRtcp(const uint8_t* packet, size_t length);
Tommi6eda26c2021-06-09 13:46:28 +0200120
121 uint32_t local_ssrc() const {
122 // The local_ssrc member variable of config_ will never change and can be
123 // considered const.
124 return config_.rtp.local_ssrc;
125 }
126
127 uint32_t remote_ssrc() const {
128 // The remote_ssrc member variable of config_ will never change and can be
129 // considered const.
130 return config_.rtp.remote_ssrc;
131 }
132
pbosa2f30de2015-10-15 05:22:13 -0700133 const webrtc::AudioReceiveStream::Config& config() const;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100134 const AudioSendStream* GetAssociatedSendStreamForTesting() const;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200135
Tommi6eda26c2021-06-09 13:46:28 +0200136 // TODO(tommi): Remove this method.
137 void ReconfigureForTesting(const webrtc::AudioReceiveStream::Config& config);
138
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200139 private:
aleloi04c07222016-11-22 06:42:53 -0800140 AudioState* audio_state() const;
solenberg7add0582015-11-20 09:59:34 -0800141
Tommi02df2eb2021-05-31 12:57:53 +0200142 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_;
143 // TODO(bugs.webrtc.org/11993): This checker conceptually represents
144 // operations that belong to the network thread. The Call class is currently
145 // moving towards handling network packets on the network thread and while
146 // that work is ongoing, this checker may in practice represent the worker
147 // thread, but still serves as a mechanism of grouping together concepts
148 // that belong to the network thread. Once the packets are fully delivered
149 // on the network thread, this comment will be deleted.
Tommi90738dd2021-05-31 17:36:47 +0200150 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100151 webrtc::AudioReceiveStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800152 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Chen Xing054e3bb2019-08-02 10:29:26 +0000153 SourceTracker source_tracker_;
Ranveer Aggarwaldea374a2021-01-23 12:27:19 +0530154 const std::unique_ptr<voe::ChannelReceiveInterface> channel_receive_;
Tommi02df2eb2021-05-31 12:57:53 +0200155 AudioSendStream* associated_send_stream_
Tommi90738dd2021-05-31 17:36:47 +0200156 RTC_GUARDED_BY(packet_sequence_checker_) = nullptr;
solenberg85a04962015-10-27 03:35:21 -0700157
Niels Möller1e062892018-02-07 10:18:32 +0100158 bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false;
aleloi04c07222016-11-22 06:42:53 -0800159
Tommi02df2eb2021-05-31 12:57:53 +0200160 std::unique_ptr<RtpStreamReceiverInterface> rtp_stream_receiver_
Tommi90738dd2021-05-31 17:36:47 +0200161 RTC_GUARDED_BY(packet_sequence_checker_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200162};
163} // namespace internal
164} // namespace webrtc
165
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200166#endif // AUDIO_AUDIO_RECEIVE_STREAM_H_