blob: aef497db7627600b6553762f354ccfd077b630b1 [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"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000029#include "api/transport/rtp/rtp_source.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010030#include "media/base/media_channel.h"
Harald Alvestrand1ee33252020-09-24 13:31:15 +000031#include "pc/audio_track.h"
Tommi4ccdf932021-05-17 14:50:10 +020032#include "pc/jitter_buffer_delay.h"
Markus Handella1b82012021-05-26 18:56:30 +020033#include "pc/media_stream_track_proxy.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010034#include "pc/remote_audio_source.h"
35#include "pc/rtp_receiver.h"
36#include "rtc_base/ref_counted_object.h"
Tommi4ccdf932021-05-17 14:50:10 +020037#include "rtc_base/system/no_unique_address.h"
38#include "rtc_base/task_utils/pending_task_safety_flag.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010039#include "rtc_base/thread.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000040#include "rtc_base/thread_annotations.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010041
42namespace webrtc {
43
44class AudioRtpReceiver : public ObserverInterface,
45 public AudioSourceInterface::AudioObserver,
Tommi4ccdf932021-05-17 14:50:10 +020046 public RtpReceiverInternal {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010047 public:
48 AudioRtpReceiver(rtc::Thread* worker_thread,
49 std::string receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020050 std::vector<std::string> stream_ids,
51 bool is_unified_plan);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010052 // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
53 AudioRtpReceiver(
54 rtc::Thread* worker_thread,
55 const std::string& receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020056 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
57 bool is_unified_plan);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010058 virtual ~AudioRtpReceiver();
59
60 // ObserverInterface implementation
61 void OnChanged() override;
62
63 // AudioSourceInterface::AudioObserver implementation
64 void OnSetVolume(double volume) override;
65
Tommi4ccdf932021-05-17 14:50:10 +020066 rtc::scoped_refptr<AudioTrackInterface> audio_track() const { return track_; }
Ruslan Burakov501bfba2019-02-11 10:29:19 +010067
68 // RtpReceiverInterface implementation
69 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
Tommi4ccdf932021-05-17 14:50:10 +020070 return track_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010071 }
Tommi4ccdf932021-05-17 14:50:10 +020072 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010073 std::vector<std::string> stream_ids() const override;
74 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
Tommi4ccdf932021-05-17 14:50:10 +020075 const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010076
77 cricket::MediaType media_type() const override {
78 return cricket::MEDIA_TYPE_AUDIO;
79 }
80
81 std::string id() const override { return id_; }
82
83 RtpParameters GetParameters() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010084
85 void SetFrameDecryptor(
86 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
87
88 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
89 const override;
90
91 // RtpReceiverInternal implementation.
92 void Stop() override;
Harald Alvestrand1ee33252020-09-24 13:31:15 +000093 void StopAndEndTrack() override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010094 void SetupMediaChannel(uint32_t ssrc) override;
Saurav Das7262fc22019-09-11 16:23:05 -070095 void SetupUnsignaledMediaChannel() override;
Tommi4ccdf932021-05-17 14:50:10 +020096 uint32_t ssrc() const override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +010097 void NotifyFirstPacketReceived() override;
98 void set_stream_ids(std::vector<std::string> stream_ids) override;
99 void set_transport(
Tommi4ccdf932021-05-17 14:50:10 +0200100 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100101 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
102 streams) override;
103 void SetObserver(RtpReceiverObserverInterface* observer) override;
104
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200105 void SetJitterBufferMinimumDelay(
106 absl::optional<double> delay_seconds) override;
107
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100108 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
109
110 std::vector<RtpSource> GetSources() const override;
111 int AttachmentId() const override { return attachment_id_; }
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200112 void SetDepacketizerToDecoderFrameTransformer(
113 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
114 override;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100115
116 private:
Saurav Das7262fc22019-09-11 16:23:05 -0700117 void RestartMediaChannel(absl::optional<uint32_t> ssrc);
Tommi4ccdf932021-05-17 14:50:10 +0200118 void Reconfigure(bool track_enabled, double volume)
119 RTC_RUN_ON(worker_thread_);
120 void SetOutputVolume_w(double volume) RTC_RUN_ON(worker_thread_);
121 void SetMediaChannel_w(cricket::MediaChannel* media_channel)
122 RTC_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100123
Tommi4ccdf932021-05-17 14:50:10 +0200124 RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100125 rtc::Thread* const worker_thread_;
126 const std::string id_;
127 const rtc::scoped_refptr<RemoteAudioSource> source_;
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000128 const rtc::scoped_refptr<AudioTrackProxyWithInternal<AudioTrack>> track_;
Tommi4ccdf932021-05-17 14:50:10 +0200129 cricket::VoiceMediaChannel* media_channel_ RTC_GUARDED_BY(worker_thread_) =
130 nullptr;
131 absl::optional<uint32_t> ssrc_ RTC_GUARDED_BY(worker_thread_);
132 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_
133 RTC_GUARDED_BY(&signaling_thread_checker_);
134 bool cached_track_enabled_ RTC_GUARDED_BY(&signaling_thread_checker_);
135 double cached_volume_ RTC_GUARDED_BY(&signaling_thread_checker_) = 1.0;
136 bool stopped_ RTC_GUARDED_BY(&signaling_thread_checker_) = true;
137 RtpReceiverObserverInterface* observer_
138 RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
139 bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
140 false;
141 const int attachment_id_;
142 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
143 RTC_GUARDED_BY(worker_thread_);
144 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_
145 RTC_GUARDED_BY(&signaling_thread_checker_);
146 // Stores and updates the playout delay. Handles caching cases if
Artem Titov880fa812021-07-30 22:30:23 +0200147 // `SetJitterBufferMinimumDelay` is called before start.
Tommi4ccdf932021-05-17 14:50:10 +0200148 JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200149 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
150 RTC_GUARDED_BY(worker_thread_);
Tommi4ccdf932021-05-17 14:50:10 +0200151 const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100152};
153
154} // namespace webrtc
155
156#endif // PC_AUDIO_RTP_RECEIVER_H_