blob: b0df0a7f0dd1ccbc90959cb0591f38805f5e30ac [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;
Tommidddbbeb2022-05-20 15:21:33 +020043} // namespace internal
Tommif888bb52015-12-12 01:37:01 +010044
Tommi3176ef72022-05-22 20:47:28 +020045class AudioReceiveStreamImpl final : public webrtc::AudioReceiveStreamInterface,
Tommidddbbeb2022-05-20 15:21:33 +020046 public AudioMixer::Source,
47 public Syncable {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020048 public:
Tommidddbbeb2022-05-20 15:21:33 +020049 AudioReceiveStreamImpl(
50 Clock* clock,
51 PacketRouter* packet_router,
52 NetEqFactory* neteq_factory,
Tommi3176ef72022-05-22 20:47:28 +020053 const webrtc::AudioReceiveStreamInterface::Config& config,
Tommidddbbeb2022-05-20 15:21:33 +020054 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
55 webrtc::RtcEventLog* event_log);
Niels Möller349ade32018-11-16 09:50:42 +010056 // For unit tests, which need to supply a mock channel receive.
Tommidddbbeb2022-05-20 15:21:33 +020057 AudioReceiveStreamImpl(
Sebastian Jansson977b3352019-03-04 17:43:34 +010058 Clock* clock,
Niels Möller349ade32018-11-16 09:50:42 +010059 PacketRouter* packet_router,
Tommi3176ef72022-05-22 20:47:28 +020060 const webrtc::AudioReceiveStreamInterface::Config& config,
Niels Möller349ade32018-11-16 09:50:42 +010061 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
62 webrtc::RtcEventLog* event_log,
63 std::unique_ptr<voe::ChannelReceiveInterface> channel_receive);
Niels Möllerde953292020-09-29 09:46:21 +020064
Tommidddbbeb2022-05-20 15:21:33 +020065 AudioReceiveStreamImpl() = delete;
66 AudioReceiveStreamImpl(const AudioReceiveStreamImpl&) = delete;
67 AudioReceiveStreamImpl& operator=(const AudioReceiveStreamImpl&) = delete;
Niels Möllerde953292020-09-29 09:46:21 +020068
Tommi02df2eb2021-05-31 12:57:53 +020069 // Destruction happens on the worker thread. Prior to destruction the caller
70 // must ensure that a registration with the transport has been cleared. See
71 // `RegisterWithTransport` for details.
72 // TODO(tommi): As a further improvement to this, performing the full
73 // destruction on the network thread could be made the default.
Tommidddbbeb2022-05-20 15:21:33 +020074 ~AudioReceiveStreamImpl() override;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020075
Tommi02df2eb2021-05-31 12:57:53 +020076 // Called on the network thread to register/unregister with the network
77 // transport.
78 void RegisterWithTransport(
79 RtpStreamReceiverControllerInterface* receiver_controller);
80 // If registration has previously been done (via `RegisterWithTransport`) then
81 // `UnregisterFromTransport` must be called prior to destruction, on the
82 // network thread.
83 void UnregisterFromTransport();
84
Tommi3176ef72022-05-22 20:47:28 +020085 // webrtc::AudioReceiveStreamInterface implementation.
Jelena Marusiccd670222015-07-16 09:30:09 +020086 void Start() override;
87 void Stop() override;
Tomas Gunnarsson8467cf22021-01-17 14:36:44 +010088 bool IsRunning() const override;
Tommi6eda26c2021-06-09 13:46:28 +020089 void SetDepacketizerToDecoderFrameTransformer(
90 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
91 override;
92 void SetDecoderMap(std::map<int, SdpAudioFormat> decoder_map) override;
Tommia136ed42022-05-30 15:08:13 +020093 void SetNackHistory(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;
Tomas Gunnarsson8467cf22021-01-17 14:36:44 +010097
Tommi3176ef72022-05-22 20:47:28 +020098 webrtc::AudioReceiveStreamInterface::Stats GetStats(
Niels Möller6b4d9622020-09-14 10:47:50 +020099 bool get_and_clear_legacy_stats) const override;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100100 void SetSink(AudioSinkInterface* sink) override;
solenberg217fb662016-06-17 08:30:54 -0700101 void SetGain(float gain) override;
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +0100102 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
103 int GetBaseMinimumPlayoutDelayMs() const override;
hbos8d609f62017-04-10 07:39:05 -0700104 std::vector<webrtc::RtpSource> GetSources() const override;
Tommif888bb52015-12-12 01:37:01 +0100105
solenberg3ebbcb52017-01-31 03:58:40 -0800106 // AudioMixer::Source
107 AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
108 AudioFrame* audio_frame) override;
109 int Ssrc() const override;
110 int PreferredSampleRate() const override;
111
112 // Syncable
Åsa Persson74d2b1d2020-02-10 16:33:29 +0100113 uint32_t id() const override;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200114 absl::optional<Syncable::Info> GetInfo() const override;
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200115 bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
116 int64_t* time_ms) const override;
117 void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
118 int64_t time_ms) override;
Ivo Creusenbef7b052020-09-08 16:30:25 +0200119 bool SetMinimumPlayoutDelay(int delay_ms) override;
solenberg3ebbcb52017-01-31 03:58:40 -0800120
Tommidddbbeb2022-05-20 15:21:33 +0200121 void AssociateSendStream(internal::AudioSendStream* send_stream);
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100122 void DeliverRtcp(const uint8_t* packet, size_t length);
Tommi6eda26c2021-06-09 13:46:28 +0200123
Ali Tofigh4d278e22022-03-01 10:32:20 +0100124 void SetSyncGroup(absl::string_view sync_group);
Tommi55107c82021-06-16 16:31:18 +0200125
Tommi08be9ba2021-06-15 23:01:57 +0200126 void SetLocalSsrc(uint32_t local_ssrc);
127
128 uint32_t local_ssrc() const;
Tommi6eda26c2021-06-09 13:46:28 +0200129
Tommicb7c7362022-05-09 14:49:37 +0000130 uint32_t remote_ssrc() const override {
Tommi6eda26c2021-06-09 13:46:28 +0200131 // The remote_ssrc member variable of config_ will never change and can be
132 // considered const.
133 return config_.rtp.remote_ssrc;
134 }
135
Tommicc50b042022-05-09 10:22:48 +0000136 // Returns a reference to the currently set sync group of the stream.
137 // Must be called on the packet delivery thread.
138 const std::string& sync_group() const;
139
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100140 const AudioSendStream* GetAssociatedSendStreamForTesting() const;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200141
Tommi6eda26c2021-06-09 13:46:28 +0200142 // TODO(tommi): Remove this method.
Tommi3176ef72022-05-22 20:47:28 +0200143 void ReconfigureForTesting(
144 const webrtc::AudioReceiveStreamInterface::Config& config);
Tommi6eda26c2021-06-09 13:46:28 +0200145
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200146 private:
Tommidddbbeb2022-05-20 15:21:33 +0200147 internal::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_;
Tommi3176ef72022-05-22 20:47:28 +0200158 webrtc::AudioReceiveStreamInterface::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};
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200170} // namespace webrtc
171
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200172#endif // AUDIO_AUDIO_RECEIVE_STREAM_H_