blob: ac498c99d8d94b974ba709689802228b4390bed5 [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
69// LocalAudioSinkAdapter receives data callback as a sink to the local
70// AudioTrack, and passes the data to the sink of AudioSource.
71class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
72 public cricket::AudioSource {
73 public:
74 LocalAudioSinkAdapter();
75 virtual ~LocalAudioSinkAdapter();
76
77 private:
78 // AudioSinkInterface implementation.
79 void OnData(const void* audio_data,
80 int bits_per_sample,
81 int sample_rate,
82 size_t number_of_channels,
83 size_t number_of_frames) override;
84
85 // cricket::AudioSource implementation.
86 void SetSink(cricket::AudioSource::Sink* sink) override;
87
88 cricket::AudioSource::Sink* sink_;
89 // Critical section protecting |sink_|.
90 rtc::CriticalSection lock_;
91};
92
deadbeef20cb0c12017-02-01 20:27:00 -080093class AudioRtpSender : public DtmfProviderInterface,
94 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080095 public rtc::RefCountedObject<RtpSenderInternal> {
96 public:
97 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
98 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080099
Steve Anton111fdfd2018-06-25 13:03:36 -0700100 // Construct an RtpSender for audio with the given sender ID.
101 // The sender is initialized with no track to send and no associated streams.
Steve Anton47136dd2018-01-12 10:49:35 -0800102 AudioRtpSender(rtc::Thread* worker_thread,
Steve Anton111fdfd2018-06-25 13:03:36 -0700103 const std::string& id,
Steve Anton02ee47c2018-01-10 16:26:06 -0800104 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800105
106 virtual ~AudioRtpSender();
107
deadbeef20cb0c12017-02-01 20:27:00 -0800108 // DtmfSenderProvider implementation.
109 bool CanInsertDtmf() override;
110 bool InsertDtmf(int code, int duration) override;
111 sigslot::signal0<>* GetOnDestroyedSignal() override;
112
113 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800114 void OnChanged() override;
115
deadbeef20cb0c12017-02-01 20:27:00 -0800116 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800117 bool SetTrack(MediaStreamTrackInterface* track) override;
118 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
119 return track_;
120 }
121
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100122 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
123 return dtls_transport_;
124 }
125
ossu7bb87ee2017-01-23 04:56:25 -0800126 uint32_t ssrc() const override { return ssrc_; }
127
128 cricket::MediaType media_type() const override {
129 return cricket::MEDIA_TYPE_AUDIO;
130 }
131
132 std::string id() const override { return id_; }
133
Steve Anton8ffb9c32017-08-31 15:45:38 -0700134 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800135
Amit Hilbuch2297d332019-02-19 12:49:22 -0800136 RtpParameters GetParameters() const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800137 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800138
deadbeef20cb0c12017-02-01 20:27:00 -0800139 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
140
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700141 void SetFrameEncryptor(
142 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
143
144 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
145 const override;
146
ossu7bb87ee2017-01-23 04:56:25 -0800147 // RtpSenderInternal implementation.
148 void SetSsrc(uint32_t ssrc) override;
149
Steve Anton8ffb9c32017-08-31 15:45:38 -0700150 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
151 stream_ids_ = stream_ids;
152 }
ossu7bb87ee2017-01-23 04:56:25 -0800153
Florent Castelli892acf02018-10-01 22:47:20 +0200154 void set_init_send_encodings(
155 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
156 init_parameters_.encodings = init_send_encodings;
157 }
158 std::vector<RtpEncodingParameters> init_send_encodings() const override {
159 return init_parameters_.encodings;
160 }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100161 void set_transport(
162 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
163 dtls_transport_ = dtls_transport;
164 }
ossu7bb87ee2017-01-23 04:56:25 -0800165 void Stop() override;
166
Harald Alvestrandc72af932018-01-11 17:18:19 +0100167 int AttachmentId() const override { return attachment_id_; }
168
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800169 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
ossu7bb87ee2017-01-23 04:56:25 -0800170
Amit Hilbuch2297d332019-02-19 12:49:22 -0800171 RTCError DisableEncodingLayers(const std::vector<std::string>& rids) override;
172
ossu7bb87ee2017-01-23 04:56:25 -0800173 private:
174 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
175 // some other way to test if we have a valid SSRC.
176 bool can_send_track() const { return track_ && ssrc_; }
177 // Helper function to construct options for
178 // AudioProviderInterface::SetAudioSend.
179 void SetAudioSend();
180 // Helper function to call SetAudioSend with "stop sending" parameters.
181 void ClearAudioSend();
182
deadbeef20cb0c12017-02-01 20:27:00 -0800183 sigslot::signal0<> SignalDestroyed;
184
Steve Anton47136dd2018-01-12 10:49:35 -0800185 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800186 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700187 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200188 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800189 cricket::VoiceMediaChannel* media_channel_ = nullptr;
Steve Anton111fdfd2018-06-25 13:03:36 -0700190 StatsCollector* stats_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800191 rtc::scoped_refptr<AudioTrackInterface> track_;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100192 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
deadbeef20cb0c12017-02-01 20:27:00 -0800193 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800194 // |last_transaction_id_| is used to verify that |SetParameters| is receiving
195 // the parameters object that was last returned from |GetParameters|.
196 // As such, it is used for internal verification and is not observable by the
197 // the client. It is marked as mutable to enable |GetParameters| to be a
198 // const method.
199 mutable absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800200 uint32_t ssrc_ = 0;
201 bool cached_track_enabled_ = false;
202 bool stopped_ = false;
203
204 // Used to pass the data callback from the |track_| to the other end of
205 // cricket::AudioSource.
206 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100207 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700208 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
ossu7bb87ee2017-01-23 04:56:25 -0800209};
210
211class VideoRtpSender : public ObserverInterface,
212 public rtc::RefCountedObject<RtpSenderInternal> {
213 public:
Steve Anton111fdfd2018-06-25 13:03:36 -0700214 // Construct an RtpSender for video with the given sender ID.
215 // The sender is initialized with no track to send and no associated streams.
216 VideoRtpSender(rtc::Thread* worker_thread, const std::string& id);
ossu7bb87ee2017-01-23 04:56:25 -0800217
218 virtual ~VideoRtpSender();
219
220 // ObserverInterface implementation
221 void OnChanged() override;
222
223 // RtpSenderInterface implementation
224 bool SetTrack(MediaStreamTrackInterface* track) override;
225 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
226 return track_;
227 }
228
229 uint32_t ssrc() const override { return ssrc_; }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100230 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
231 return dtls_transport_;
232 }
ossu7bb87ee2017-01-23 04:56:25 -0800233
234 cricket::MediaType media_type() const override {
235 return cricket::MEDIA_TYPE_VIDEO;
236 }
237
238 std::string id() const override { return id_; }
239
Steve Anton8ffb9c32017-08-31 15:45:38 -0700240 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800241
Florent Castelli892acf02018-10-01 22:47:20 +0200242 void set_init_send_encodings(
243 const std::vector<RtpEncodingParameters>& init_send_encodings) override {
244 init_parameters_.encodings = init_send_encodings;
245 }
246 std::vector<RtpEncodingParameters> init_send_encodings() const override {
247 return init_parameters_.encodings;
248 }
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100249 void set_transport(
250 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
251 dtls_transport_ = dtls_transport;
252 }
Florent Castelli892acf02018-10-01 22:47:20 +0200253
Amit Hilbuch2297d332019-02-19 12:49:22 -0800254 RtpParameters GetParameters() const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800255 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800256
deadbeef20cb0c12017-02-01 20:27:00 -0800257 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
258
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700259 void SetFrameEncryptor(
260 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
261
262 rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
263 const override;
264
ossu7bb87ee2017-01-23 04:56:25 -0800265 // RtpSenderInternal implementation.
266 void SetSsrc(uint32_t ssrc) override;
267
Steve Anton8ffb9c32017-08-31 15:45:38 -0700268 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
269 stream_ids_ = stream_ids;
270 }
ossu7bb87ee2017-01-23 04:56:25 -0800271
272 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100273 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800274
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800275 void SetMediaChannel(cricket::MediaChannel* media_channel) override;
ossu7bb87ee2017-01-23 04:56:25 -0800276
Amit Hilbuch2297d332019-02-19 12:49:22 -0800277 RTCError DisableEncodingLayers(const std::vector<std::string>& rids) override;
278
ossu7bb87ee2017-01-23 04:56:25 -0800279 private:
280 bool can_send_track() const { return track_ && ssrc_; }
281 // Helper function to construct options for
282 // VideoProviderInterface::SetVideoSend.
283 void SetVideoSend();
284 // Helper function to call SetVideoSend with "stop sending" parameters.
285 void ClearVideoSend();
286
Steve Anton47136dd2018-01-12 10:49:35 -0800287 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800288 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700289 std::vector<std::string> stream_ids_;
Florent Castelli892acf02018-10-01 22:47:20 +0200290 RtpParameters init_parameters_;
Steve Anton47136dd2018-01-12 10:49:35 -0800291 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800292 rtc::scoped_refptr<VideoTrackInterface> track_;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800293 // |last_transaction_id_| is used to verify that |SetParameters| is receiving
294 // the parameters object that was last returned from |GetParameters|.
295 // As such, it is used for internal verification and is not observable by the
296 // the client. It is marked as mutable to enable |GetParameters| to be a
297 // const method.
298 mutable absl::optional<std::string> last_transaction_id_;
ossu7bb87ee2017-01-23 04:56:25 -0800299 uint32_t ssrc_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800300 VideoTrackInterface::ContentHint cached_track_content_hint_ =
301 VideoTrackInterface::ContentHint::kNone;
302 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100303 int attachment_id_ = 0;
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700304 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100305 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
Amit Hilbuch2297d332019-02-19 12:49:22 -0800306 std::vector<std::string> disabled_rids_;
ossu7bb87ee2017-01-23 04:56:25 -0800307};
308
309} // namespace webrtc
310
Steve Anton10542f22019-01-11 09:11:00 -0800311#endif // PC_RTP_SENDER_H_