blob: 42a877ecc262a0973a0a5475536769dc85a94ff8 [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
58 // Returns an ID that changes every time SetTrack() is called, but
59 // otherwise remains constant. Used to generate IDs for stats.
60 // The special value zero means that no track is attached.
61 virtual int AttachmentId() const = 0;
Amit Hilbuch2297d332019-02-19 12:49:22 -080062
63 // Disables the layers identified by the specified RIDs.
64 // If the specified list is empty, this is a no-op.
65 virtual RTCError DisableEncodingLayers(
66 const std::vector<std::string>& rid) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080067};
68
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -080069// Shared implementation for RtpSenderInternal interface.
70class RtpSenderBase : public RtpSenderInternal, public ObserverInterface {
71 public:
72 // Sets the underlying MediaEngine channel associated with this RtpSender.
73 // A VoiceMediaChannel should be used for audio RtpSenders and
74 // a VideoMediaChannel should be used for video RtpSenders.
75 // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
76 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
77
78 bool SetTrack(MediaStreamTrackInterface* track) override;
79 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
80 return track_;
81 }
82
83 RtpParameters GetParameters() const override;
84 RTCError SetParameters(const RtpParameters& parameters) override;
85
86 // Used to set the SSRC of the sender, once a local description has been set.
87 // If |ssrc| is 0, this indiates that the sender should disconnect from the
88 // underlying transport (this occurs if the sender isn't seen in a local
89 // description).
90 void SetSsrc(uint32_t ssrc) override;
91 uint32_t ssrc() const override { return ssrc_; }
92
93 std::vector<std::string> stream_ids() const override { return stream_ids_; }
94 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
95 stream_ids_ = stream_ids;
96 }
97
98 std::string id() const override { return id_; }
99
100 void set_init_send_encodings(
101 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
102 init_parameters_.encodings = init_send_encodings;
103 }
104 std::vector<RtpEncodingParameters> init_send_encodings() const override {
105 return init_parameters_.encodings;
106 }
107
108 void set_transport(
109 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
110 dtls_transport_ = dtls_transport;
111 }
112 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
113 return dtls_transport_;
114 }
115
116 void SetFrameEncryptor(
117 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
118
119 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
120 const override {
121 return frame_encryptor_;
122 }
123
124 void Stop() override;
125
126 // Returns an ID that changes every time SetTrack() is called, but
127 // otherwise remains constant. Used to generate IDs for stats.
128 // The special value zero means that no track is attached.
129 int AttachmentId() const override { return attachment_id_; }
130
131 // Disables the layers identified by the specified RIDs.
132 // If the specified list is empty, this is a no-op.
133 RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override;
134
135 protected:
136 RtpSenderBase(rtc::Thread* worker_thread, const std::string& id);
137 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
138 // some other way to test if we have a valid SSRC.
139 bool can_send_track() const { return track_ && ssrc_; }
140
141 virtual std::string track_kind() const = 0;
142
143 // Enable sending on the media channel.
144 virtual void SetSend() = 0;
145 // Disable sending on the media channel.
146 virtual void ClearSend() = 0;
147
148 // Template method pattern to allow subclasses to add custom behavior for
149 // when tracks are attached, detached, and for adding tracks to statistics.
150 virtual void AttachTrack() {}
151 virtual void DetachTrack() {}
152 virtual void AddTrackToStats() {}
153 virtual void RemoveTrackFromStats() {}
154
155 rtc::Thread* worker_thread_;
156 uint32_t ssrc_ = 0;
157 bool stopped_ = false;
158 int attachment_id_ = 0;
159 const std::string id_;
160
161 std::vector<std::string> stream_ids_;
162 RtpParameters init_parameters_;
163
164 cricket::MediaChannel* media_channel_ = nullptr;
165 rtc::scoped_refptr<MediaStreamTrackInterface> track_;
166
167 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
168 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
169 // |last_transaction_id_| is used to verify that |SetParameters| is receiving
170 // the parameters object that was last returned from |GetParameters|.
171 // As such, it is used for internal verification and is not observable by the
172 // the client. It is marked as mutable to enable |GetParameters| to be a
173 // const method.
174 mutable absl::optional<std::string> last_transaction_id_;
175 std::vector<std::string> disabled_rids_;
176};
177
ossu7bb87ee2017-01-23 04:56:25 -0800178// LocalAudioSinkAdapter receives data callback as a sink to the local
179// AudioTrack, and passes the data to the sink of AudioSource.
180class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
181 public cricket::AudioSource {
182 public:
183 LocalAudioSinkAdapter();
184 virtual ~LocalAudioSinkAdapter();
185
186 private:
187 // AudioSinkInterface implementation.
188 void OnData(const void* audio_data,
189 int bits_per_sample,
190 int sample_rate,
191 size_t number_of_channels,
192 size_t number_of_frames) override;
193
194 // cricket::AudioSource implementation.
195 void SetSink(cricket::AudioSource::Sink* sink) override;
196
197 cricket::AudioSource::Sink* sink_;
198 // Critical section protecting |sink_|.
199 rtc::CriticalSection lock_;
200};
201
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800202class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800203 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700204 // Construct an RtpSender for audio with the given sender ID.
205 // The sender is initialized with no track to send and no associated streams.
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800206 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
207 // at the appropriate times.
208 static rtc::scoped_refptr<AudioRtpSender> Create(rtc::Thread* worker_thread,
209 const std::string& id,
210 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800211 virtual ~AudioRtpSender();
212
deadbeef20cb0c12017-02-01 20:27:00 -0800213 // DtmfSenderProvider implementation.
214 bool CanInsertDtmf() override;
215 bool InsertDtmf(int code, int duration) override;
216 sigslot::signal0<>* GetOnDestroyedSignal() override;
217
218 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800219 void OnChanged() override;
220
ossu7bb87ee2017-01-23 04:56:25 -0800221 cricket::MediaType media_type() const override {
222 return cricket::MEDIA_TYPE_AUDIO;
223 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800224 std::string track_kind() const override {
225 return MediaStreamTrackInterface::kAudioKind;
226 }
ossu7bb87ee2017-01-23 04:56:25 -0800227
deadbeef20cb0c12017-02-01 20:27:00 -0800228 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
229
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800230 protected:
231 AudioRtpSender(rtc::Thread* worker_thread,
232 const std::string& id,
233 StatsCollector* stats);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700234
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800235 void SetSend() override;
236 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700237
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800238 // Hooks to allow custom logic when tracks are attached and detached.
239 void AttachTrack() override;
240 void DetachTrack() override;
241 void AddTrackToStats() override;
242 void RemoveTrackFromStats() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800243
ossu7bb87ee2017-01-23 04:56:25 -0800244 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800245 cricket::VoiceMediaChannel* voice_media_channel() {
246 return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
247 }
248 rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
249 return rtc::scoped_refptr<AudioTrackInterface>(
250 static_cast<AudioTrackInterface*>(track_.get()));
251 }
deadbeef20cb0c12017-02-01 20:27:00 -0800252 sigslot::signal0<> SignalDestroyed;
253
Steve Anton111fdfd2018-06-25 13:03:36 -0700254 StatsCollector* stats_ = nullptr;
deadbeef20cb0c12017-02-01 20:27:00 -0800255 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800256 bool cached_track_enabled_ = false;
ossu7bb87ee2017-01-23 04:56:25 -0800257
258 // Used to pass the data callback from the |track_| to the other end of
259 // cricket::AudioSource.
260 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
261};
262
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800263class VideoRtpSender : public RtpSenderBase {
ossu7bb87ee2017-01-23 04:56:25 -0800264 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700265 // Construct an RtpSender for video with the given sender ID.
266 // The sender is initialized with no track to send and no associated streams.
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800267 static rtc::scoped_refptr<VideoRtpSender> Create(rtc::Thread* worker_thread,
268 const std::string& id);
ossu7bb87ee2017-01-23 04:56:25 -0800269 virtual ~VideoRtpSender();
270
271 // ObserverInterface implementation
272 void OnChanged() override;
273
ossu7bb87ee2017-01-23 04:56:25 -0800274 cricket::MediaType media_type() const override {
275 return cricket::MEDIA_TYPE_VIDEO;
276 }
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800277 std::string track_kind() const override {
278 return MediaStreamTrackInterface::kVideoKind;
Florent Castelli892acf02018-10-01 22:47:20 +0200279 }
ossu7bb87ee2017-01-23 04:56:25 -0800280
deadbeef20cb0c12017-02-01 20:27:00 -0800281 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
282
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800283 protected:
284 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700285
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800286 void SetSend() override;
287 void ClearSend() override;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700288
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800289 // Hook to allow custom logic when tracks are attached.
290 void AttachTrack() override;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800291
ossu7bb87ee2017-01-23 04:56:25 -0800292 private:
Amit Hilbuchea7ef2a2019-02-19 15:20:21 -0800293 cricket::VideoMediaChannel* video_media_channel() {
294 return static_cast<cricket::VideoMediaChannel*>(media_channel_);
295 }
296 rtc::scoped_refptr<VideoTrackInterface> video_track() const {
297 return rtc::scoped_refptr<VideoTrackInterface>(
298 static_cast<VideoTrackInterface*>(track_.get()));
299 }
ossu7bb87ee2017-01-23 04:56:25 -0800300
ossu7bb87ee2017-01-23 04:56:25 -0800301 VideoTrackInterface::ContentHint cached_track_content_hint_ =
302 VideoTrackInterface::ContentHint::kNone;
ossu7bb87ee2017-01-23 04:56:25 -0800303};
304
305} // namespace webrtc
306
Steve Anton10542f22019-01-11 09:11:00 -0800307#endif // PC_RTP_SENDER_H_