blob: cfebfe56354aee8fd2b2e5c037ee936f1d672268 [file] [log] [blame]
Ruslan Burakov501bfba2019-02-11 10:29:19 +01001/*
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>
Tommi4ccdf932021-05-17 14:50:10 +020015
Ruslan Burakov501bfba2019-02-11 10:29:19 +010016#include <string>
17#include <vector>
18
19#include "absl/types/optional.h"
20#include "api/crypto/frame_decryptor_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "api/dtls_transport_interface.h"
22#include "api/frame_transformer_interface.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010023#include "api/media_stream_interface.h"
24#include "api/media_types.h"
25#include "api/rtp_parameters.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000026#include "api/rtp_receiver_interface.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010027#include "api/scoped_refptr.h"
Tommi4ccdf932021-05-17 14:50:10 +020028#include "api/sequence_checker.h"
Artem Titovc374d112022-06-16 21:27:45 +020029#include "api/task_queue/pending_task_safety_flag.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000030#include "api/transport/rtp/rtp_source.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010031#include "media/base/media_channel.h"
Harald Alvestrand1ee33252020-09-24 13:31:15 +000032#include "pc/audio_track.h"
Tommi4ccdf932021-05-17 14:50:10 +020033#include "pc/jitter_buffer_delay.h"
Markus Handella1b82012021-05-26 18:56:30 +020034#include "pc/media_stream_track_proxy.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010035#include "pc/remote_audio_source.h"
36#include "pc/rtp_receiver.h"
Tommi4ccdf932021-05-17 14:50:10 +020037#include "rtc_base/system/no_unique_address.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010038#include "rtc_base/thread.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000039#include "rtc_base/thread_annotations.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010040
41namespace webrtc {
42
43class AudioRtpReceiver : public ObserverInterface,
44 public AudioSourceInterface::AudioObserver,
Tommi4ccdf932021-05-17 14:50:10 +020045 public RtpReceiverInternal {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010046 public:
Tommi6589def2022-02-17 23:36:47 +010047 // 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.
Ruslan Burakov501bfba2019-02-11 10:29:19 +010053 AudioRtpReceiver(rtc::Thread* worker_thread,
54 std::string receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020055 std::vector<std::string> stream_ids,
Tommi6589def2022-02-17 23:36:47 +010056 bool is_unified_plan,
57 cricket::VoiceMediaChannel* voice_channel = nullptr);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010058 // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
59 AudioRtpReceiver(
60 rtc::Thread* worker_thread,
61 const std::string& receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020062 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
Tommi6589def2022-02-17 23:36:47 +010063 bool is_unified_plan,
64 cricket::VoiceMediaChannel* media_channel = nullptr);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010065 virtual ~AudioRtpReceiver();
66
67 // ObserverInterface implementation
68 void OnChanged() override;
69
70 // AudioSourceInterface::AudioObserver implementation
71 void OnSetVolume(double volume) override;
72
Tommi4ccdf932021-05-17 14:50:10 +020073 rtc::scoped_refptr<AudioTrackInterface> audio_track() const { return track_; }
Ruslan Burakov501bfba2019-02-11 10:29:19 +010074
75 // RtpReceiverInterface implementation
76 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
Tommi4ccdf932021-05-17 14:50:10 +020077 return track_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010078 }
Tommi4ccdf932021-05-17 14:50:10 +020079 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010080 std::vector<std::string> stream_ids() const override;
81 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
Tommi4ccdf932021-05-17 14:50:10 +020082 const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010083
84 cricket::MediaType media_type() const override {
85 return cricket::MEDIA_TYPE_AUDIO;
86 }
87
88 std::string id() const override { return id_; }
89
90 RtpParameters GetParameters() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010091
92 void SetFrameDecryptor(
93 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
94
95 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
96 const override;
97
98 // RtpReceiverInternal implementation.
99 void Stop() override;
100 void SetupMediaChannel(uint32_t ssrc) override;
Saurav Das7262fc22019-09-11 16:23:05 -0700101 void SetupUnsignaledMediaChannel() override;
Tommi4ccdf932021-05-17 14:50:10 +0200102 uint32_t ssrc() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100103 void NotifyFirstPacketReceived() override;
104 void set_stream_ids(std::vector<std::string> stream_ids) override;
105 void set_transport(
Tommi4ccdf932021-05-17 14:50:10 +0200106 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100107 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
108 streams) override;
109 void SetObserver(RtpReceiverObserverInterface* observer) override;
110
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200111 void SetJitterBufferMinimumDelay(
112 absl::optional<double> delay_seconds) override;
113
Harald Alvestrand36fafc82022-12-08 08:47:42 +0000114 void SetMediaChannel(
115 cricket::MediaReceiveChannelInterface* media_channel) override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100116
117 std::vector<RtpSource> GetSources() const override;
118 int AttachmentId() const override { return attachment_id_; }
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200119 void SetDepacketizerToDecoderFrameTransformer(
120 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
121 override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100122
123 private:
Tommi6589def2022-02-17 23:36:47 +0100124 void RestartMediaChannel(absl::optional<uint32_t> ssrc)
125 RTC_RUN_ON(&signaling_thread_checker_);
126 void RestartMediaChannel_w(absl::optional<uint32_t> ssrc,
127 bool track_enabled,
128 MediaSourceInterface::SourceState state)
Tommi4ccdf932021-05-17 14:50:10 +0200129 RTC_RUN_ON(worker_thread_);
Tommi6589def2022-02-17 23:36:47 +0100130 void Reconfigure(bool track_enabled) RTC_RUN_ON(worker_thread_);
Tommi4ccdf932021-05-17 14:50:10 +0200131 void SetOutputVolume_w(double volume) RTC_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100132
Tommi4ccdf932021-05-17 14:50:10 +0200133 RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100134 rtc::Thread* const worker_thread_;
135 const std::string id_;
136 const rtc::scoped_refptr<RemoteAudioSource> source_;
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000137 const rtc::scoped_refptr<AudioTrackProxyWithInternal<AudioTrack>> track_;
Tommi4ccdf932021-05-17 14:50:10 +0200138 cricket::VoiceMediaChannel* media_channel_ RTC_GUARDED_BY(worker_thread_) =
139 nullptr;
140 absl::optional<uint32_t> ssrc_ RTC_GUARDED_BY(worker_thread_);
141 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 23:36:47 +0100144 double cached_volume_ RTC_GUARDED_BY(worker_thread_) = 1.0;
Tommi4ccdf932021-05-17 14:50:10 +0200145 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 22:30:23 +0200155 // `SetJitterBufferMinimumDelay` is called before start.
Tommi4ccdf932021-05-17 14:50:10 +0200156 JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200157 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
158 RTC_GUARDED_BY(worker_thread_);
Tommi4ccdf932021-05-17 14:50:10 +0200159 const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100160};
161
162} // namespace webrtc
163
164#endif // PC_AUDIO_RTP_RECEIVER_H_