blob: 6fc9f7fe62d91df998081a32f2bf6b892c1fb929 [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>
Tommi55107c82021-06-16 16:31:18 +020016#include <string>
hbos8d609f62017-04-10 07:39:05 -070017#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080018
Ali Tofigh4d278e22022-03-01 10:32:20 +010019#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio/audio_mixer.h"
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010021#include "api/neteq/neteq_factory.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020022#include "api/rtp_headers.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "audio/audio_state.h"
25#include "call/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/syncable.h"
Chen Xing054e3bb2019-08-02 10:29:26 +000027#include "modules/rtp_rtcp/source/source_tracker.h"
Tommi02df2eb2021-05-31 12:57:53 +020028#include "rtc_base/system/no_unique_address.h"
Sebastian Jansson977b3352019-03-04 17:43:34 +010029#include "system_wrappers/include/clock.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020030
31namespace webrtc {
solenberg3ebbcb52017-01-31 03:58:40 -080032class PacketRouter;
ivoc14d5dbe2016-07-04 07:06:55 -070033class RtcEventLog;
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,
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010050 NetEqFactory* neteq_factory,
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020051 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070052 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
53 webrtc::RtcEventLog* event_log);
Niels Möller349ade32018-11-16 09:50:42 +010054 // For unit tests, which need to supply a mock channel receive.
55 AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +010056 Clock* clock,
Niels Möller349ade32018-11-16 09:50:42 +010057 PacketRouter* packet_router,
58 const webrtc::AudioReceiveStream::Config& config,
59 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
60 webrtc::RtcEventLog* event_log,
61 std::unique_ptr<voe::ChannelReceiveInterface> channel_receive);
Niels Möllerde953292020-09-29 09:46:21 +020062
63 AudioReceiveStream() = delete;
64 AudioReceiveStream(const AudioReceiveStream&) = delete;
65 AudioReceiveStream& operator=(const AudioReceiveStream&) = delete;
66
Tommi02df2eb2021-05-31 12:57:53 +020067 // Destruction happens on the worker thread. Prior to destruction the caller
68 // must ensure that a registration with the transport has been cleared. See
69 // `RegisterWithTransport` for details.
70 // TODO(tommi): As a further improvement to this, performing the full
71 // destruction on the network thread could be made the default.
pbosa2f30de2015-10-15 05:22:13 -070072 ~AudioReceiveStream() override;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020073
Tommi02df2eb2021-05-31 12:57:53 +020074 // Called on the network thread to register/unregister with the network
75 // transport.
76 void RegisterWithTransport(
77 RtpStreamReceiverControllerInterface* receiver_controller);
78 // If registration has previously been done (via `RegisterWithTransport`) then
79 // `UnregisterFromTransport` must be called prior to destruction, on the
80 // network thread.
81 void UnregisterFromTransport();
82
pbos1ba8d392016-05-01 20:18:34 -070083 // webrtc::AudioReceiveStream implementation.
Jelena Marusiccd670222015-07-16 09:30:09 +020084 void Start() override;
85 void Stop() override;
Tommid3500062021-06-14 19:39:45 +020086 const RtpConfig& rtp_config() const override { return config_.rtp; }
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;
Ivo Creusen2562cf02021-09-03 14:51:22 +000094 void SetNonSenderRttMeasurement(bool enabled) override;
Tommie0972822021-06-14 08:11:10 +020095 void SetFrameDecryptor(rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
96 frame_decryptor) override;
Tommi3008bcd2021-06-14 20:00:04 +020097 void SetRtpExtensions(std::vector<RtpExtension> extensions) override;
Tomas Gunnarsson8467cf22021-01-17 14:36:44 +010098
Niels Möller6b4d9622020-09-14 10:47:50 +020099 webrtc::AudioReceiveStream::Stats GetStats(
100 bool get_and_clear_legacy_stats) const override;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100101 void SetSink(AudioSinkInterface* sink) override;
solenberg217fb662016-06-17 08:30:54 -0700102 void SetGain(float gain) override;
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +0100103 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
104 int GetBaseMinimumPlayoutDelayMs() const override;
hbos8d609f62017-04-10 07:39:05 -0700105 std::vector<webrtc::RtpSource> GetSources() const override;
Tommif888bb52015-12-12 01:37:01 +0100106
solenberg3ebbcb52017-01-31 03:58:40 -0800107 // AudioMixer::Source
108 AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
109 AudioFrame* audio_frame) override;
110 int Ssrc() const override;
111 int PreferredSampleRate() const override;
112
113 // Syncable
Åsa Persson74d2b1d2020-02-10 16:33:29 +0100114 uint32_t id() const override;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200115 absl::optional<Syncable::Info> GetInfo() const override;
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200116 bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
117 int64_t* time_ms) const override;
118 void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
119 int64_t time_ms) override;
Ivo Creusenbef7b052020-09-08 16:30:25 +0200120 bool SetMinimumPlayoutDelay(int delay_ms) override;
solenberg3ebbcb52017-01-31 03:58:40 -0800121
solenberg7602aab2016-11-14 11:30:07 -0800122 void AssociateSendStream(AudioSendStream* send_stream);
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100123 void DeliverRtcp(const uint8_t* packet, size_t length);
Tommi6eda26c2021-06-09 13:46:28 +0200124
Ali Tofigh4d278e22022-03-01 10:32:20 +0100125 void SetSyncGroup(absl::string_view sync_group);
Tommi55107c82021-06-16 16:31:18 +0200126
Tommi08be9ba2021-06-15 23:01:57 +0200127 void SetLocalSsrc(uint32_t local_ssrc);
128
129 uint32_t local_ssrc() const;
Tommi6eda26c2021-06-09 13:46:28 +0200130
131 uint32_t remote_ssrc() const {
132 // The remote_ssrc member variable of config_ will never change and can be
133 // considered const.
134 return config_.rtp.remote_ssrc;
135 }
136
Tommicc50b042022-05-09 10:22:48 +0000137 // Returns a reference to the currently set sync group of the stream.
138 // Must be called on the packet delivery thread.
139 const std::string& sync_group() const;
140
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100141 const AudioSendStream* GetAssociatedSendStreamForTesting() const;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200142
Tommi6eda26c2021-06-09 13:46:28 +0200143 // TODO(tommi): Remove this method.
144 void ReconfigureForTesting(const webrtc::AudioReceiveStream::Config& config);
145
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200146 private:
aleloi04c07222016-11-22 06:42:53 -0800147 AudioState* audio_state() const;
solenberg7add0582015-11-20 09:59:34 -0800148
Tommi02df2eb2021-05-31 12:57:53 +0200149 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_;
150 // TODO(bugs.webrtc.org/11993): This checker conceptually represents
151 // operations that belong to the network thread. The Call class is currently
152 // moving towards handling network packets on the network thread and while
153 // that work is ongoing, this checker may in practice represent the worker
154 // thread, but still serves as a mechanism of grouping together concepts
155 // that belong to the network thread. Once the packets are fully delivered
156 // on the network thread, this comment will be deleted.
Tommi90738dd2021-05-31 17:36:47 +0200157 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100158 webrtc::AudioReceiveStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800159 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Chen Xing054e3bb2019-08-02 10:29:26 +0000160 SourceTracker source_tracker_;
Ranveer Aggarwaldea374a2021-01-23 12:27:19 +0530161 const std::unique_ptr<voe::ChannelReceiveInterface> channel_receive_;
Tommi02df2eb2021-05-31 12:57:53 +0200162 AudioSendStream* associated_send_stream_
Tommi90738dd2021-05-31 17:36:47 +0200163 RTC_GUARDED_BY(packet_sequence_checker_) = nullptr;
solenberg85a04962015-10-27 03:35:21 -0700164
Niels Möller1e062892018-02-07 10:18:32 +0100165 bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false;
aleloi04c07222016-11-22 06:42:53 -0800166
Tommi02df2eb2021-05-31 12:57:53 +0200167 std::unique_ptr<RtpStreamReceiverInterface> rtp_stream_receiver_
Tommi90738dd2021-05-31 17:36:47 +0200168 RTC_GUARDED_BY(packet_sequence_checker_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200169};
170} // namespace internal
171} // namespace webrtc
172
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200173#endif // AUDIO_AUDIO_RECEIVE_STREAM_H_