blob: c786d906d8bca44590eddffb6734c0f1dfb75b82 [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
Steve Anton8ffb9c32017-08-31 15:45:38 -070050 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:
77 // Sets the underlying MediaEngine channel associated with this RtpSender.
78 // A VoiceMediaChannel should be used for audio RtpSenders and
79 // a VideoMediaChannel should be used for video RtpSenders.
80 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
81 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
82
83 bool SetTrack(MediaStreamTrackInterface* track) override;
84 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
85 return track_;
86 }
87
88 RtpParameters GetParameters() const override;
89 RTCError SetParameters(const RtpParameters& parameters) override;
90
Amit Hilbuch619b2942019-02-26 15:55:19 -080091 // |GetParameters| and |SetParameters| operate with a transactional model.
92 // Allow access to get/set parameters without invalidating transaction id.
93 RtpParameters GetParametersInternal() const override;
94 RTCError SetParametersInternal(const RtpParameters& parameters) override;
95
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -080096 // Used to set the SSRC of the sender, once a local description has been set.
97 // If |ssrc| is 0, this indiates that the sender should disconnect from the
98 // underlying transport (this occurs if the sender isn't seen in a local
99 // description).
100 void SetSsrc(uint32_t ssrc) override;
101 uint32_t ssrc() const override { return ssrc_; }
102
103 std::vector<std::string> stream_ids() const override { return stream_ids_; }
104 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
105 stream_ids_ = stream_ids;
106 }
107
108 std::string id() const override { return id_; }
109
110 void set_init_send_encodings(
111 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
112 init_parameters_.encodings = init_send_encodings;
113 }
114 std::vector<RtpEncodingParameters> init_send_encodings() const override {
115 return init_parameters_.encodings;
116 }
117
118 void set_transport(
119 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
120 dtls_transport_ = dtls_transport;
121 }
122 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
123 return dtls_transport_;
124 }
125
126 void SetFrameEncryptor(
127 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
128
129 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
130 const override {
131 return frame_encryptor_;
132 }
133
134 void Stop() override;
135
136 // Returns an ID that changes every time SetTrack() is called, but
137 // otherwise remains constant. Used to generate IDs for stats.
138 // The special value zero means that no track is attached.
139 int AttachmentId() const override { return attachment_id_; }
140
141 // Disables the layers identified by the specified RIDs.
142 // If the specified list is empty, this is a no-op.
143 RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override;
144
145 protected:
146 RtpSenderBase(rtc::Thread* worker_thread, const std::string& id);
147 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
148 // some other way to test if we have a valid SSRC.
149 bool can_send_track() const { return track_ && ssrc_; }
150
151 virtual std::string track_kind() const = 0;
152
153 // Enable sending on the media channel.
154 virtual void SetSend() = 0;
155 // Disable sending on the media channel.
156 virtual void ClearSend() = 0;
157
158 // Template method pattern to allow subclasses to add custom behavior for
159 // when tracks are attached, detached, and for adding tracks to statistics.
160 virtual void AttachTrack() {}
161 virtual void DetachTrack() {}
162 virtual void AddTrackToStats() {}
163 virtual void RemoveTrackFromStats() {}
164
165 rtc::Thread* worker_thread_;
166 uint32_t ssrc_ = 0;
167 bool stopped_ = false;
168 int attachment_id_ = 0;
169 const std::string id_;
170
171 std::vector<std::string> stream_ids_;
172 RtpParameters init_parameters_;
173
174 cricket::MediaChannel* media_channel_ = nullptr;
175 rtc::scoped_refptr<MediaStreamTrackInterface> track_;
176
177 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
178 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
179 // |last_transaction_id_| is used to verify that |SetParameters| is receiving
180 // the parameters object that was last returned from |GetParameters|.
181 // As such, it is used for internal verification and is not observable by the
182 // the client. It is marked as mutable to enable |GetParameters| to be a
183 // const method.
184 mutable absl::optional<std::string> last_transaction_id_;
185 std::vector<std::string> disabled_rids_;
186};
187
ossu7bb87ee2017-01-23 04:56:25 -0800188// LocalAudioSinkAdapter receives data callback as a sink to the local
189// AudioTrack, and passes the data to the sink of AudioSource.
190class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
191 public cricket::AudioSource {
192 public:
193 LocalAudioSinkAdapter();
194 virtual ~LocalAudioSinkAdapter();
195
196 private:
197 // AudioSinkInterface implementation.
198 void OnData(const void* audio_data,
199 int bits_per_sample,
200 int sample_rate,
201 size_t number_of_channels,
202 size_t number_of_frames) override;
203
204 // cricket::AudioSource implementation.
205 void SetSink(cricket::AudioSource::Sink* sink) override;
206
207 cricket::AudioSource::Sink* sink_;
208 // Critical section protecting |sink_|.
209 rtc::CriticalSection lock_;
210};
211
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800212class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800213 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700214 // Construct an RtpSender for audio with the given sender ID.
215 // The sender is initialized with no track to send and no associated streams.
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800216 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
217 // at the appropriate times.
218 static rtc::scoped_refptr<AudioRtpSender> Create(rtc::Thread* worker_thread,
219 const std::string& id,
220 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800221 virtual ~AudioRtpSender();
222
deadbeef20cb0c12017-02-01 20:27:00 -0800223 // DtmfSenderProvider implementation.
224 bool CanInsertDtmf() override;
225 bool InsertDtmf(int code, int duration) override;
226 sigslot::signal0<>* GetOnDestroyedSignal() override;
227
228 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800229 void OnChanged() override;
230
ossu7bb87ee2017-01-23 04:56:25 -0800231 cricket::MediaType media_type() const override {
232 return cricket::MEDIA_TYPE_AUDIO;
233 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800234 std::string track_kind() const override {
235 return MediaStreamTrackInterface::kAudioKind;
236 }
ossu7bb87ee2017-01-23 04:56:25 -0800237
deadbeef20cb0c12017-02-01 20:27:00 -0800238 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
239
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800240 protected:
241 AudioRtpSender(rtc::Thread* worker_thread,
242 const std::string& id,
243 StatsCollector* stats);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700244
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800245 void SetSend() override;
246 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700247
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800248 // Hooks to allow custom logic when tracks are attached and detached.
249 void AttachTrack() override;
250 void DetachTrack() override;
251 void AddTrackToStats() override;
252 void RemoveTrackFromStats() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800253
ossu7bb87ee2017-01-23 04:56:25 -0800254 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800255 cricket::VoiceMediaChannel* voice_media_channel() {
256 return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
257 }
258 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
259 return rtc::scoped_refptr<AudioTrackInterface>(
260 static_cast<AudioTrackInterface*>(track_.get()));
261 }
deadbeef20cb0c12017-02-01 20:27:00 -0800262 sigslot::signal0<> SignalDestroyed;
263
Steve Anton111fdfd2018-06-25 13:03:36 -0700264 StatsCollector* stats_ = nullptr;
deadbeef20cb0c12017-02-01 20:27:00 -0800265 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800266 bool cached_track_enabled_ = false;
ossu7bb87ee2017-01-23 04:56:25 -0800267
268 // Used to pass the data callback from the |track_| to the other end of
269 // cricket::AudioSource.
270 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
271};
272
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800273class VideoRtpSender : public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800274 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700275 // Construct an RtpSender for video with the given sender ID.
276 // The sender is initialized with no track to send and no associated streams.
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800277 static rtc::scoped_refptr<VideoRtpSender> Create(rtc::Thread* worker_thread,
278 const std::string& id);
ossu7bb87ee2017-01-23 04:56:25 -0800279 virtual ~VideoRtpSender();
280
281 // ObserverInterface implementation
282 void OnChanged() override;
283
ossu7bb87ee2017-01-23 04:56:25 -0800284 cricket::MediaType media_type() const override {
285 return cricket::MEDIA_TYPE_VIDEO;
286 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800287 std::string track_kind() const override {
288 return MediaStreamTrackInterface::kVideoKind;
Florent Castelli892acf02018-10-01 22:47:20 +0200289 }
ossu7bb87ee2017-01-23 04:56:25 -0800290
deadbeef20cb0c12017-02-01 20:27:00 -0800291 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
292
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800293 protected:
294 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700295
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800296 void SetSend() override;
297 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700298
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800299 // Hook to allow custom logic when tracks are attached.
300 void AttachTrack() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800301
ossu7bb87ee2017-01-23 04:56:25 -0800302 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800303 cricket::VideoMediaChannel* video_media_channel() {
304 return static_cast<cricket::VideoMediaChannel*>(media_channel_);
305 }
306 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
307 return rtc::scoped_refptr<VideoTrackInterface>(
308 static_cast<VideoTrackInterface*>(track_.get()));
309 }
ossu7bb87ee2017-01-23 04:56:25 -0800310
ossu7bb87ee2017-01-23 04:56:25 -0800311 VideoTrackInterface::ContentHint cached_track_content_hint_ =
312 VideoTrackInterface::ContentHint::kNone;
ossu7bb87ee2017-01-23 04:56:25 -0800313};
314
315} // namespace webrtc
316
Steve Anton10542f22019-01-11 09:11:00 -0800317#endif // PC_RTP_SENDER_H_