blob: 87c82cce61e827afb7512077950728508189e28b [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
kwibergfffa42b2016-02-23 10:46:32 -080014#include <memory>
hbos8d609f62017-04-10 07:39:05 -070015#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio/audio_mixer.h"
Ivo Creusenc3d1f9b2019-11-01 11:47:51 +010018#include "api/neteq/neteq_factory.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020019#include "api/rtp_headers.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "audio/audio_state.h"
22#include "call/audio_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "call/syncable.h"
Chen Xing054e3bb2019-08-02 10:29:26 +000024#include "modules/rtp_rtcp/source/source_tracker.h"
Tommi02df2eb2021-05-31 12:57:53 +020025#include "rtc_base/system/no_unique_address.h"
Sebastian Jansson977b3352019-03-04 17:43:34 +010026#include "system_wrappers/include/clock.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020027
28namespace webrtc {
solenberg3ebbcb52017-01-31 03:58:40 -080029class PacketRouter;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010030class ProcessThread;
ivoc14d5dbe2016-07-04 07:06:55 -070031class RtcEventLog;
nisse657bab22017-02-21 06:28:10 -080032class RtpPacketReceived;
nisse0f15f922017-06-21 01:05:22 -070033class RtpStreamReceiverControllerInterface;
34class RtpStreamReceiverInterface;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020035
solenberg13725082015-11-25 08:16:52 -080036namespace voe {
Niels Möller349ade32018-11-16 09:50:42 +010037class ChannelReceiveInterface;
solenberg13725082015-11-25 08:16:52 -080038} // namespace voe
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020039
solenberg13725082015-11-25 08:16:52 -080040namespace internal {
solenberg7602aab2016-11-14 11:30:07 -080041class AudioSendStream;
Tommif888bb52015-12-12 01:37:01 +010042
aleloiaed581a2016-10-20 06:32:39 -070043class AudioReceiveStream final : public webrtc::AudioReceiveStream,
solenberg3ebbcb52017-01-31 03:58:40 -080044 public AudioMixer::Source,
nisse0f15f922017-06-21 01:05:22 -070045 public Syncable {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020046 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010047 AudioReceiveStream(Clock* clock,
nisse0f15f922017-06-21 01:05:22 -070048 PacketRouter* packet_router,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010049 ProcessThread* module_process_thread,
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.
Fredrik Solenberg3b903d02018-01-10 15:17:10 +010084 void Reconfigure(const webrtc::AudioReceiveStream::Config& config) override;
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;
88
Niels Möller6b4d9622020-09-14 10:47:50 +020089 webrtc::AudioReceiveStream::Stats GetStats(
90 bool get_and_clear_legacy_stats) const override;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010091 void SetSink(AudioSinkInterface* sink) override;
solenberg217fb662016-06-17 08:30:54 -070092 void SetGain(float gain) override;
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +010093 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
94 int GetBaseMinimumPlayoutDelayMs() const override;
hbos8d609f62017-04-10 07:39:05 -070095 std::vector<webrtc::RtpSource> GetSources() const override;
Tommif888bb52015-12-12 01:37:01 +010096
solenberg3ebbcb52017-01-31 03:58:40 -080097 // AudioMixer::Source
98 AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz,
99 AudioFrame* audio_frame) override;
100 int Ssrc() const override;
101 int PreferredSampleRate() const override;
102
103 // Syncable
Åsa Persson74d2b1d2020-02-10 16:33:29 +0100104 uint32_t id() const override;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200105 absl::optional<Syncable::Info> GetInfo() const override;
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200106 bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
107 int64_t* time_ms) const override;
108 void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
109 int64_t time_ms) override;
Ivo Creusenbef7b052020-09-08 16:30:25 +0200110 bool SetMinimumPlayoutDelay(int delay_ms) override;
solenberg3ebbcb52017-01-31 03:58:40 -0800111
solenberg7602aab2016-11-14 11:30:07 -0800112 void AssociateSendStream(AudioSendStream* send_stream);
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100113 void DeliverRtcp(const uint8_t* packet, size_t length);
pbosa2f30de2015-10-15 05:22:13 -0700114 const webrtc::AudioReceiveStream::Config& config() const;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100115 const AudioSendStream* GetAssociatedSendStreamForTesting() const;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200116
117 private:
aleloi04c07222016-11-22 06:42:53 -0800118 AudioState* audio_state() const;
solenberg7add0582015-11-20 09:59:34 -0800119
Tommi02df2eb2021-05-31 12:57:53 +0200120 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_;
121 // TODO(bugs.webrtc.org/11993): This checker conceptually represents
122 // operations that belong to the network thread. The Call class is currently
123 // moving towards handling network packets on the network thread and while
124 // that work is ongoing, this checker may in practice represent the worker
125 // thread, but still serves as a mechanism of grouping together concepts
126 // that belong to the network thread. Once the packets are fully delivered
127 // on the network thread, this comment will be deleted.
Tommi90738dd2021-05-31 17:36:47 +0200128 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100129 webrtc::AudioReceiveStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800130 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Chen Xing054e3bb2019-08-02 10:29:26 +0000131 SourceTracker source_tracker_;
Ranveer Aggarwaldea374a2021-01-23 12:27:19 +0530132 const std::unique_ptr<voe::ChannelReceiveInterface> channel_receive_;
Tommi02df2eb2021-05-31 12:57:53 +0200133 AudioSendStream* associated_send_stream_
Tommi90738dd2021-05-31 17:36:47 +0200134 RTC_GUARDED_BY(packet_sequence_checker_) = nullptr;
solenberg85a04962015-10-27 03:35:21 -0700135
Niels Möller1e062892018-02-07 10:18:32 +0100136 bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false;
aleloi04c07222016-11-22 06:42:53 -0800137
Tommi02df2eb2021-05-31 12:57:53 +0200138 std::unique_ptr<RtpStreamReceiverInterface> rtp_stream_receiver_
Tommi90738dd2021-05-31 17:36:47 +0200139 RTC_GUARDED_BY(packet_sequence_checker_);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200140};
141} // namespace internal
142} // namespace webrtc
143
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // AUDIO_AUDIO_RECEIVE_STREAM_H_