blob: 628690f57a6b8ab212af9d658dff978c5e9b772e [file] [log] [blame]
Ruslan Burakov501bfba2019-02-11 09:29:191/*
2 * Copyright 2019 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
11#ifndef PC_AUDIO_RTP_RECEIVER_H_
12#define PC_AUDIO_RTP_RECEIVER_H_
13
14#include <stdint.h>
Tommi4ccdf9322021-05-17 12:50:1015
Florent Castelli8037fc62024-08-29 13:00:4016#include <optional>
Ruslan Burakov501bfba2019-02-11 09:29:1917#include <string>
18#include <vector>
19
Ruslan Burakov501bfba2019-02-11 09:29:1920#include "api/crypto/frame_decryptor_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "api/dtls_transport_interface.h"
22#include "api/frame_transformer_interface.h"
Ruslan Burakov501bfba2019-02-11 09:29:1923#include "api/media_stream_interface.h"
24#include "api/media_types.h"
25#include "api/rtp_parameters.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0826#include "api/rtp_receiver_interface.h"
Ruslan Burakov501bfba2019-02-11 09:29:1927#include "api/scoped_refptr.h"
Tommi4ccdf9322021-05-17 12:50:1028#include "api/sequence_checker.h"
Artem Titovc374d112022-06-16 19:27:4529#include "api/task_queue/pending_task_safety_flag.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0830#include "api/transport/rtp/rtp_source.h"
Ruslan Burakov501bfba2019-02-11 09:29:1931#include "media/base/media_channel.h"
Harald Alvestrand1ee33252020-09-24 13:31:1532#include "pc/audio_track.h"
Tommi4ccdf9322021-05-17 12:50:1033#include "pc/jitter_buffer_delay.h"
Markus Handella1b82012021-05-26 16:56:3034#include "pc/media_stream_track_proxy.h"
Ruslan Burakov501bfba2019-02-11 09:29:1935#include "pc/remote_audio_source.h"
36#include "pc/rtp_receiver.h"
Tommi4ccdf9322021-05-17 12:50:1037#include "rtc_base/system/no_unique_address.h"
Ruslan Burakov501bfba2019-02-11 09:29:1938#include "rtc_base/thread.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0839#include "rtc_base/thread_annotations.h"
Ruslan Burakov501bfba2019-02-11 09:29:1940
41namespace webrtc {
42
43class AudioRtpReceiver : public ObserverInterface,
44 public AudioSourceInterface::AudioObserver,
Tommi4ccdf9322021-05-17 12:50:1045 public RtpReceiverInternal {
Ruslan Burakov501bfba2019-02-11 09:29:1946 public:
Tommi6589def2022-02-17 22:36:4747 // The constructor supports optionally passing the voice channel to the
48 // instance at construction time without having to call `SetMediaChannel()`
49 // on the worker thread straight after construction.
50 // However, when using that, the assumption is that right after construction,
51 // a call to either `SetupUnsignaledMediaChannel` or `SetupMediaChannel`
52 // will be made, which will internally start the source on the worker thread.
Harald Alvestrandc0d44d92022-12-13 12:57:2453 AudioRtpReceiver(
54 rtc::Thread* worker_thread,
55 std::string receiver_id,
56 std::vector<std::string> stream_ids,
57 bool is_unified_plan,
58 cricket::VoiceMediaReceiveChannelInterface* voice_channel = nullptr);
Ruslan Burakov501bfba2019-02-11 09:29:1959 // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
60 AudioRtpReceiver(
61 rtc::Thread* worker_thread,
62 const std::string& receiver_id,
Henrik Boströmc335b0e2021-04-08 05:25:3863 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
Tommi6589def2022-02-17 22:36:4764 bool is_unified_plan,
Harald Alvestrandc0d44d92022-12-13 12:57:2465 cricket::VoiceMediaReceiveChannelInterface* media_channel = nullptr);
Ruslan Burakov501bfba2019-02-11 09:29:1966 virtual ~AudioRtpReceiver();
67
68 // ObserverInterface implementation
69 void OnChanged() override;
70
71 // AudioSourceInterface::AudioObserver implementation
72 void OnSetVolume(double volume) override;
73
Tommi4ccdf9322021-05-17 12:50:1074 rtc::scoped_refptr<AudioTrackInterface> audio_track() const { return track_; }
Ruslan Burakov501bfba2019-02-11 09:29:1975
76 // RtpReceiverInterface implementation
77 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
Tommi4ccdf9322021-05-17 12:50:1078 return track_;
Ruslan Burakov501bfba2019-02-11 09:29:1979 }
Tommi4ccdf9322021-05-17 12:50:1080 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
Ruslan Burakov501bfba2019-02-11 09:29:1981 std::vector<std::string> stream_ids() const override;
82 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
Tommi4ccdf9322021-05-17 12:50:1083 const override;
Ruslan Burakov501bfba2019-02-11 09:29:1984
85 cricket::MediaType media_type() const override {
86 return cricket::MEDIA_TYPE_AUDIO;
87 }
88
89 std::string id() const override { return id_; }
90
91 RtpParameters GetParameters() const override;
Ruslan Burakov501bfba2019-02-11 09:29:1992
93 void SetFrameDecryptor(
94 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
95
96 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
97 const override;
98
99 // RtpReceiverInternal implementation.
100 void Stop() override;
101 void SetupMediaChannel(uint32_t ssrc) override;
Saurav Das7262fc22019-09-11 23:23:05102 void SetupUnsignaledMediaChannel() override;
Florent Castelli8037fc62024-08-29 13:00:40103 std::optional<uint32_t> ssrc() const override;
Ruslan Burakov501bfba2019-02-11 09:29:19104 void NotifyFirstPacketReceived() override;
105 void set_stream_ids(std::vector<std::string> stream_ids) override;
106 void set_transport(
Tommi4ccdf9322021-05-17 12:50:10107 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
Ruslan Burakov501bfba2019-02-11 09:29:19108 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
109 streams) override;
110 void SetObserver(RtpReceiverObserverInterface* observer) override;
111
Ruslan Burakov4bac79e2019-04-03 17:55:33112 void SetJitterBufferMinimumDelay(
Florent Castelli8037fc62024-08-29 13:00:40113 std::optional<double> delay_seconds) override;
Ruslan Burakov4bac79e2019-04-03 17:55:33114
Harald Alvestrand36fafc82022-12-08 08:47:42115 void SetMediaChannel(
116 cricket::MediaReceiveChannelInterface* media_channel) override;
Ruslan Burakov501bfba2019-02-11 09:29:19117
118 std::vector<RtpSource> GetSources() const override;
119 int AttachmentId() const override { return attachment_id_; }
Harald Alvestrandb0e70572024-04-23 14:04:18120 void SetFrameTransformer(
Harald Alvestranda6544372023-11-13 09:33:56121 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
Ruslan Burakov501bfba2019-02-11 09:29:19122
123 private:
Florent Castelli8037fc62024-08-29 13:00:40124 void RestartMediaChannel(std::optional<uint32_t> ssrc)
Tommi6589def2022-02-17 22:36:47125 RTC_RUN_ON(&signaling_thread_checker_);
Florent Castelli8037fc62024-08-29 13:00:40126 void RestartMediaChannel_w(std::optional<uint32_t> ssrc,
Tommi6589def2022-02-17 22:36:47127 bool track_enabled,
128 MediaSourceInterface::SourceState state)
Tommi4ccdf9322021-05-17 12:50:10129 RTC_RUN_ON(worker_thread_);
Tommi6589def2022-02-17 22:36:47130 void Reconfigure(bool track_enabled) RTC_RUN_ON(worker_thread_);
Tommi4ccdf9322021-05-17 12:50:10131 void SetOutputVolume_w(double volume) RTC_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 09:29:19132
Tommi4ccdf9322021-05-17 12:50:10133 RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
Ruslan Burakov501bfba2019-02-11 09:29:19134 rtc::Thread* const worker_thread_;
135 const std::string id_;
136 const rtc::scoped_refptr<RemoteAudioSource> source_;
Harald Alvestrand1ee33252020-09-24 13:31:15137 const rtc::scoped_refptr<AudioTrackProxyWithInternal<AudioTrack>> track_;
Harald Alvestrandc0d44d92022-12-13 12:57:24138 cricket::VoiceMediaReceiveChannelInterface* media_channel_
139 RTC_GUARDED_BY(worker_thread_) = nullptr;
Florent Castelli8037fc62024-08-29 13:00:40140 std::optional<uint32_t> signaled_ssrc_ RTC_GUARDED_BY(worker_thread_);
Tommi4ccdf9322021-05-17 12:50:10141 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_
142 RTC_GUARDED_BY(&signaling_thread_checker_);
143 bool cached_track_enabled_ RTC_GUARDED_BY(&signaling_thread_checker_);
Tommi6589def2022-02-17 22:36:47144 double cached_volume_ RTC_GUARDED_BY(worker_thread_) = 1.0;
Tommi4ccdf9322021-05-17 12:50:10145 RtpReceiverObserverInterface* observer_
146 RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
147 bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
148 false;
149 const int attachment_id_;
150 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
151 RTC_GUARDED_BY(worker_thread_);
152 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_
153 RTC_GUARDED_BY(&signaling_thread_checker_);
154 // Stores and updates the playout delay. Handles caching cases if
Artem Titov880fa812021-07-30 20:30:23155 // `SetJitterBufferMinimumDelay` is called before start.
Tommi4ccdf9322021-05-17 12:50:10156 JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
Marina Ciocea3e9af7f2020-04-01 05:46:16157 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
158 RTC_GUARDED_BY(worker_thread_);
Tommi4ccdf9322021-05-17 12:50:10159 const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
Ruslan Burakov501bfba2019-02-11 09:29:19160};
161
162} // namespace webrtc
163
164#endif // PC_AUDIO_RTP_RECEIVER_H_