blob: 82ef711a6cb47142ba28cafdd33fc5f950734a12 [file] [log] [blame]
ossu7bb87ee2017-01-23 04:56:25 -08001/*
2 * Copyright 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
11// This file contains classes that implement RtpSenderInterface.
12// An RtpSender associates a MediaStreamTrackInterface with an underlying
13// transport (provided by AudioProviderInterface/VideoProviderInterface)
14
Steve Anton10542f22019-01-11 09:11:00 -080015#ifndef PC_RTP_SENDER_H_
16#define PC_RTP_SENDER_H_
ossu7bb87ee2017-01-23 04:56:25 -080017
18#include <memory>
19#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <vector>
ossu7bb87ee2017-01-23 04:56:25 -080021
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/media_stream_interface.h"
23#include "api/rtp_sender_interface.h"
24#include "media/base/audio_source.h"
25#include "media/base/media_channel.h"
26#include "pc/dtmf_sender.h"
27#include "rtc_base/critical_section.h"
ossu7bb87ee2017-01-23 04:56:25 -080028
29namespace webrtc {
30
Steve Anton2d8609c2018-01-23 16:38:46 -080031class StatsCollector;
32
Florent Castelli892acf02018-10-01 22:47:20 +020033bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters);
34
ossu7bb87ee2017-01-23 04:56:25 -080035// Internal interface used by PeerConnection.
36class RtpSenderInternal : public RtpSenderInterface {
37 public:
Steve Anton57858b32018-02-15 15:19:50 -080038 // Sets the underlying MediaEngine channel associated with this RtpSender.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080039 // A VoiceMediaChannel should be used for audio RtpSenders and
40 // a VideoMediaChannel should be used for video RtpSenders.
41 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
42 virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0;
Steve Anton57858b32018-02-15 15:19:50 -080043
ossu7bb87ee2017-01-23 04:56:25 -080044 // Used to set the SSRC of the sender, once a local description has been set.
45 // If |ssrc| is 0, this indiates that the sender should disconnect from the
46 // underlying transport (this occurs if the sender isn't seen in a local
47 // description).
48 virtual void SetSsrc(uint32_t ssrc) = 0;
49
Henrik Andreasssoncc189172019-05-20 09:01:38 +000050 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
Florent Castelli892acf02018-10-01 22:47:20 +020051 virtual void set_init_send_encodings(
52 const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +010053 virtual void set_transport(
54 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080055
56 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 15:19:50 -080057
Amit Hilbuch619b2942019-02-26 15:55:19 -080058 // |GetParameters| and |SetParameters| operate with a transactional model.
59 // Allow access to get/set parameters without invalidating transaction id.
60 virtual RtpParameters GetParametersInternal() const = 0;
61 virtual RTCError SetParametersInternal(const RtpParameters& parameters) = 0;
62
Steve Anton57858b32018-02-15 15:19:50 -080063 // Returns an ID that changes every time SetTrack() is called, but
64 // otherwise remains constant. Used to generate IDs for stats.
65 // The special value zero means that no track is attached.
66 virtual int AttachmentId() const = 0;
Amit Hilbuch2297d332019-02-19 12:49:22 -080067
68 // Disables the layers identified by the specified RIDs.
69 // If the specified list is empty, this is a no-op.
70 virtual RTCError DisableEncodingLayers(
71 const std::vector<std::string>& rid) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080072};
73
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -080074// Shared implementation for RtpSenderInternal interface.
75class RtpSenderBase : public RtpSenderInternal, public ObserverInterface {
76 public:
Guido Urdaneta1ff16c82019-05-20 19:31:53 +020077 class SetStreamsObserver {
78 public:
79 virtual ~SetStreamsObserver() = default;
80 virtual void OnSetStreams() = 0;
81 };
82
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -080083 // Sets the underlying MediaEngine channel associated with this RtpSender.
84 // A VoiceMediaChannel should be used for audio RtpSenders and
85 // a VideoMediaChannel should be used for video RtpSenders.
86 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
87 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
88
89 bool SetTrack(MediaStreamTrackInterface* track) override;
90 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
91 return track_;
92 }
93
94 RtpParameters GetParameters() const override;
95 RTCError SetParameters(const RtpParameters& parameters) override;
96
Amit Hilbuch619b2942019-02-26 15:55:19 -080097 // |GetParameters| and |SetParameters| operate with a transactional model.
98 // Allow access to get/set parameters without invalidating transaction id.
99 RtpParameters GetParametersInternal() const override;
100 RTCError SetParametersInternal(const RtpParameters& parameters) override;
101
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800102 // Used to set the SSRC of the sender, once a local description has been set.
103 // If |ssrc| is 0, this indiates that the sender should disconnect from the
104 // underlying transport (this occurs if the sender isn't seen in a local
105 // description).
106 void SetSsrc(uint32_t ssrc) override;
107 uint32_t ssrc() const override { return ssrc_; }
108
109 std::vector<std::string> stream_ids() const override { return stream_ids_; }
Henrik Andreasssoncc189172019-05-20 09:01:38 +0000110 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
111 stream_ids_ = stream_ids;
112 }
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200113 void SetStreams(const std::vector<std::string>& stream_ids) override;
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800114
115 std::string id() const override { return id_; }
116
117 void set_init_send_encodings(
118 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
119 init_parameters_.encodings = init_send_encodings;
120 }
121 std::vector<RtpEncodingParameters> init_send_encodings() const override {
122 return init_parameters_.encodings;
123 }
124
125 void set_transport(
126 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
127 dtls_transport_ = dtls_transport;
128 }
129 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
130 return dtls_transport_;
131 }
132
133 void SetFrameEncryptor(
134 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
135
136 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
137 const override {
138 return frame_encryptor_;
139 }
140
141 void Stop() override;
142
143 // Returns an ID that changes every time SetTrack() is called, but
144 // otherwise remains constant. Used to generate IDs for stats.
145 // The special value zero means that no track is attached.
146 int AttachmentId() const override { return attachment_id_; }
147
148 // Disables the layers identified by the specified RIDs.
149 // If the specified list is empty, this is a no-op.
150 RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override;
151
152 protected:
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200153 // If |set_streams_observer| is not null, it is invoked when SetStreams()
154 // is called. |set_streams_observer| is not owned by this object. If not
155 // null, it must be valid at least until this sender becomes stopped.
156 RtpSenderBase(rtc::Thread* worker_thread,
157 const std::string& id,
158 SetStreamsObserver* set_streams_observer);
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800159 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
160 // some other way to test if we have a valid SSRC.
161 bool can_send_track() const { return track_ && ssrc_; }
162
163 virtual std::string track_kind() const = 0;
164
165 // Enable sending on the media channel.
166 virtual void SetSend() = 0;
167 // Disable sending on the media channel.
168 virtual void ClearSend() = 0;
169
170 // Template method pattern to allow subclasses to add custom behavior for
171 // when tracks are attached, detached, and for adding tracks to statistics.
172 virtual void AttachTrack() {}
173 virtual void DetachTrack() {}
174 virtual void AddTrackToStats() {}
175 virtual void RemoveTrackFromStats() {}
176
177 rtc::Thread* worker_thread_;
178 uint32_t ssrc_ = 0;
179 bool stopped_ = false;
180 int attachment_id_ = 0;
181 const std::string id_;
182
183 std::vector<std::string> stream_ids_;
184 RtpParameters init_parameters_;
185
186 cricket::MediaChannel* media_channel_ = nullptr;
187 rtc::scoped_refptr<MediaStreamTrackInterface> track_;
188
189 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
190 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
191 // |last_transaction_id_| is used to verify that |SetParameters| is receiving
192 // the parameters object that was last returned from |GetParameters|.
193 // As such, it is used for internal verification and is not observable by the
194 // the client. It is marked as mutable to enable |GetParameters| to be a
195 // const method.
196 mutable absl::optional<std::string> last_transaction_id_;
197 std::vector<std::string> disabled_rids_;
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200198
199 SetStreamsObserver* set_streams_observer_ = nullptr;
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800200};
201
ossu7bb87ee2017-01-23 04:56:25 -0800202// LocalAudioSinkAdapter receives data callback as a sink to the local
203// AudioTrack, and passes the data to the sink of AudioSource.
204class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
205 public cricket::AudioSource {
206 public:
207 LocalAudioSinkAdapter();
208 virtual ~LocalAudioSinkAdapter();
209
210 private:
211 // AudioSinkInterface implementation.
212 void OnData(const void* audio_data,
213 int bits_per_sample,
214 int sample_rate,
215 size_t number_of_channels,
216 size_t number_of_frames) override;
217
218 // cricket::AudioSource implementation.
219 void SetSink(cricket::AudioSource::Sink* sink) override;
220
221 cricket::AudioSource::Sink* sink_;
222 // Critical section protecting |sink_|.
223 rtc::CriticalSection lock_;
224};
225
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800226class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800227 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700228 // Construct an RtpSender for audio with the given sender ID.
229 // The sender is initialized with no track to send and no associated streams.
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800230 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
231 // at the appropriate times.
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200232 // If |set_streams_observer| is not null, it is invoked when SetStreams()
233 // is called. |set_streams_observer| is not owned by this object. If not
234 // null, it must be valid at least until this sender becomes stopped.
235 static rtc::scoped_refptr<AudioRtpSender> Create(
236 rtc::Thread* worker_thread,
237 const std::string& id,
238 StatsCollector* stats,
239 SetStreamsObserver* set_streams_observer);
ossu7bb87ee2017-01-23 04:56:25 -0800240 virtual ~AudioRtpSender();
241
deadbeef20cb0c12017-02-01 20:27:00 -0800242 // DtmfSenderProvider implementation.
243 bool CanInsertDtmf() override;
244 bool InsertDtmf(int code, int duration) override;
245 sigslot::signal0<>* GetOnDestroyedSignal() override;
246
247 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800248 void OnChanged() override;
249
ossu7bb87ee2017-01-23 04:56:25 -0800250 cricket::MediaType media_type() const override {
251 return cricket::MEDIA_TYPE_AUDIO;
252 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800253 std::string track_kind() const override {
254 return MediaStreamTrackInterface::kAudioKind;
255 }
ossu7bb87ee2017-01-23 04:56:25 -0800256
deadbeef20cb0c12017-02-01 20:27:00 -0800257 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
258
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800259 protected:
260 AudioRtpSender(rtc::Thread* worker_thread,
261 const std::string& id,
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200262 StatsCollector* stats,
263 SetStreamsObserver* set_streams_observer);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700264
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800265 void SetSend() override;
266 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700267
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800268 // Hooks to allow custom logic when tracks are attached and detached.
269 void AttachTrack() override;
270 void DetachTrack() override;
271 void AddTrackToStats() override;
272 void RemoveTrackFromStats() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800273
ossu7bb87ee2017-01-23 04:56:25 -0800274 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800275 cricket::VoiceMediaChannel* voice_media_channel() {
276 return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
277 }
278 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
279 return rtc::scoped_refptr<AudioTrackInterface>(
280 static_cast<AudioTrackInterface*>(track_.get()));
281 }
deadbeef20cb0c12017-02-01 20:27:00 -0800282 sigslot::signal0<> SignalDestroyed;
283
Steve Anton111fdfd2018-06-25 13:03:36 -0700284 StatsCollector* stats_ = nullptr;
deadbeef20cb0c12017-02-01 20:27:00 -0800285 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800286 bool cached_track_enabled_ = false;
ossu7bb87ee2017-01-23 04:56:25 -0800287
288 // Used to pass the data callback from the |track_| to the other end of
289 // cricket::AudioSource.
290 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
291};
292
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800293class VideoRtpSender : public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800294 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700295 // Construct an RtpSender for video with the given sender ID.
296 // The sender is initialized with no track to send and no associated streams.
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200297 // If |set_streams_observer| is not null, it is invoked when SetStreams()
298 // is called. |set_streams_observer| is not owned by this object. If not
299 // null, it must be valid at least until this sender becomes stopped.
300 static rtc::scoped_refptr<VideoRtpSender> Create(
301 rtc::Thread* worker_thread,
302 const std::string& id,
303 SetStreamsObserver* set_streams_observer);
ossu7bb87ee2017-01-23 04:56:25 -0800304 virtual ~VideoRtpSender();
305
306 // ObserverInterface implementation
307 void OnChanged() override;
308
ossu7bb87ee2017-01-23 04:56:25 -0800309 cricket::MediaType media_type() const override {
310 return cricket::MEDIA_TYPE_VIDEO;
311 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800312 std::string track_kind() const override {
313 return MediaStreamTrackInterface::kVideoKind;
Florent Castelli892acf02018-10-01 22:47:20 +0200314 }
ossu7bb87ee2017-01-23 04:56:25 -0800315
deadbeef20cb0c12017-02-01 20:27:00 -0800316 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
317
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800318 protected:
Guido Urdaneta1ff16c82019-05-20 19:31:53 +0200319 VideoRtpSender(rtc::Thread* worker_thread,
320 const std::string& id,
321 SetStreamsObserver* set_streams_observer);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700322
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800323 void SetSend() override;
324 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700325
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800326 // Hook to allow custom logic when tracks are attached.
327 void AttachTrack() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800328
ossu7bb87ee2017-01-23 04:56:25 -0800329 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800330 cricket::VideoMediaChannel* video_media_channel() {
331 return static_cast<cricket::VideoMediaChannel*>(media_channel_);
332 }
333 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
334 return rtc::scoped_refptr<VideoTrackInterface>(
335 static_cast<VideoTrackInterface*>(track_.get()));
336 }
ossu7bb87ee2017-01-23 04:56:25 -0800337
ossu7bb87ee2017-01-23 04:56:25 -0800338 VideoTrackInterface::ContentHint cached_track_content_hint_ =
339 VideoTrackInterface::ContentHint::kNone;
ossu7bb87ee2017-01-23 04:56:25 -0800340};
341
342} // namespace webrtc
343
Steve Anton10542f22019-01-11 09:11:00 -0800344#endif // PC_RTP_SENDER_H_