• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
15 #ifndef PC_RTP_SENDER_H_
16 #define PC_RTP_SENDER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #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/synchronization/mutex.h"
28 
29 namespace webrtc {
30 
31 class StatsCollector;
32 
33 bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters);
34 
35 // Internal interface used by PeerConnection.
36 class RtpSenderInternal : public RtpSenderInterface {
37  public:
38   // Sets the underlying MediaEngine channel associated with this RtpSender.
39   // 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;
43 
44   // 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 
50   virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
51   virtual void set_init_send_encodings(
52       const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
53   virtual void set_transport(
54       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) = 0;
55 
56   virtual void Stop() = 0;
57 
58   // |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 
63   // 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;
67 
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;
72 };
73 
74 // Shared implementation for RtpSenderInternal interface.
75 class RtpSenderBase : public RtpSenderInternal, public ObserverInterface {
76  public:
77   class SetStreamsObserver {
78    public:
79     virtual ~SetStreamsObserver() = default;
80     virtual void OnSetStreams() = 0;
81   };
82 
83   // 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;
track()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 
97   // |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 
102   // 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;
ssrc()107   uint32_t ssrc() const override { return ssrc_; }
108 
stream_ids()109   std::vector<std::string> stream_ids() const override { return stream_ids_; }
set_stream_ids(const std::vector<std::string> & stream_ids)110   void set_stream_ids(const std::vector<std::string>& stream_ids) override {
111     stream_ids_ = stream_ids;
112   }
113   void SetStreams(const std::vector<std::string>& stream_ids) override;
114 
id()115   std::string id() const override { return id_; }
116 
set_init_send_encodings(const std::vector<RtpEncodingParameters> & init_send_encodings)117   void set_init_send_encodings(
118       const std::vector<RtpEncodingParameters>& init_send_encodings) override {
119     init_parameters_.encodings = init_send_encodings;
120   }
init_send_encodings()121   std::vector<RtpEncodingParameters> init_send_encodings() const override {
122     return init_parameters_.encodings;
123   }
124 
set_transport(rtc::scoped_refptr<DtlsTransportInterface> dtls_transport)125   void set_transport(
126       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
127     dtls_transport_ = dtls_transport;
128   }
dtls_transport()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 
GetFrameEncryptor()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.
AttachmentId()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   void SetEncoderToPacketizerFrameTransformer(
153       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
154 
155  protected:
156   // If |set_streams_observer| is not null, it is invoked when SetStreams()
157   // is called. |set_streams_observer| is not owned by this object. If not
158   // null, it must be valid at least until this sender becomes stopped.
159   RtpSenderBase(rtc::Thread* worker_thread,
160                 const std::string& id,
161                 SetStreamsObserver* set_streams_observer);
162   // TODO(nisse): Since SSRC == 0 is technically valid, figure out
163   // some other way to test if we have a valid SSRC.
can_send_track()164   bool can_send_track() const { return track_ && ssrc_; }
165 
166   virtual std::string track_kind() const = 0;
167 
168   // Enable sending on the media channel.
169   virtual void SetSend() = 0;
170   // Disable sending on the media channel.
171   virtual void ClearSend() = 0;
172 
173   // Template method pattern to allow subclasses to add custom behavior for
174   // when tracks are attached, detached, and for adding tracks to statistics.
AttachTrack()175   virtual void AttachTrack() {}
DetachTrack()176   virtual void DetachTrack() {}
AddTrackToStats()177   virtual void AddTrackToStats() {}
RemoveTrackFromStats()178   virtual void RemoveTrackFromStats() {}
179 
180   rtc::Thread* worker_thread_;
181   uint32_t ssrc_ = 0;
182   bool stopped_ = false;
183   int attachment_id_ = 0;
184   const std::string id_;
185 
186   std::vector<std::string> stream_ids_;
187   RtpParameters init_parameters_;
188 
189   cricket::MediaChannel* media_channel_ = nullptr;
190   rtc::scoped_refptr<MediaStreamTrackInterface> track_;
191 
192   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
193   rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
194   // |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_;
200   std::vector<std::string> disabled_rids_;
201 
202   SetStreamsObserver* set_streams_observer_ = nullptr;
203 
204   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
205 };
206 
207 // LocalAudioSinkAdapter receives data callback as a sink to the local
208 // AudioTrack, and passes the data to the sink of AudioSource.
209 class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
210                               public cricket::AudioSource {
211  public:
212   LocalAudioSinkAdapter();
213   virtual ~LocalAudioSinkAdapter();
214 
215  private:
216   // AudioSinkInterface implementation.
217   void OnData(const void* audio_data,
218               int bits_per_sample,
219               int sample_rate,
220               size_t number_of_channels,
221               size_t number_of_frames,
222               absl::optional<int64_t> absolute_capture_timestamp_ms) override;
223 
224   // AudioSinkInterface implementation.
OnData(const void * audio_data,int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames)225   void OnData(const void* audio_data,
226               int bits_per_sample,
227               int sample_rate,
228               size_t number_of_channels,
229               size_t number_of_frames) override {
230     OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
231            number_of_frames,
232            /*absolute_capture_timestamp_ms=*/absl::nullopt);
233   }
234 
235   // cricket::AudioSource implementation.
236   void SetSink(cricket::AudioSource::Sink* sink) override;
237 
238   cricket::AudioSource::Sink* sink_;
239   // Critical section protecting |sink_|.
240   Mutex lock_;
241 };
242 
243 class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
244  public:
245   // Construct an RtpSender for audio with the given sender ID.
246   // The sender is initialized with no track to send and no associated streams.
247   // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
248   // at the appropriate times.
249   // If |set_streams_observer| is not null, it is invoked when SetStreams()
250   // is called. |set_streams_observer| is not owned by this object. If not
251   // null, it must be valid at least until this sender becomes stopped.
252   static rtc::scoped_refptr<AudioRtpSender> Create(
253       rtc::Thread* worker_thread,
254       const std::string& id,
255       StatsCollector* stats,
256       SetStreamsObserver* set_streams_observer);
257   virtual ~AudioRtpSender();
258 
259   // DtmfSenderProvider implementation.
260   bool CanInsertDtmf() override;
261   bool InsertDtmf(int code, int duration) override;
262   sigslot::signal0<>* GetOnDestroyedSignal() override;
263 
264   // ObserverInterface implementation.
265   void OnChanged() override;
266 
media_type()267   cricket::MediaType media_type() const override {
268     return cricket::MEDIA_TYPE_AUDIO;
269   }
track_kind()270   std::string track_kind() const override {
271     return MediaStreamTrackInterface::kAudioKind;
272   }
273 
274   rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
275 
276  protected:
277   AudioRtpSender(rtc::Thread* worker_thread,
278                  const std::string& id,
279                  StatsCollector* stats,
280                  SetStreamsObserver* set_streams_observer);
281 
282   void SetSend() override;
283   void ClearSend() override;
284 
285   // Hooks to allow custom logic when tracks are attached and detached.
286   void AttachTrack() override;
287   void DetachTrack() override;
288   void AddTrackToStats() override;
289   void RemoveTrackFromStats() override;
290 
291  private:
voice_media_channel()292   cricket::VoiceMediaChannel* voice_media_channel() {
293     return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
294   }
audio_track()295   rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
296     return rtc::scoped_refptr<AudioTrackInterface>(
297         static_cast<AudioTrackInterface*>(track_.get()));
298   }
299   sigslot::signal0<> SignalDestroyed;
300 
301   StatsCollector* stats_ = nullptr;
302   rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
303   bool cached_track_enabled_ = false;
304 
305   // Used to pass the data callback from the |track_| to the other end of
306   // cricket::AudioSource.
307   std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
308 };
309 
310 class VideoRtpSender : public RtpSenderBase {
311  public:
312   // Construct an RtpSender for video with the given sender ID.
313   // The sender is initialized with no track to send and no associated streams.
314   // If |set_streams_observer| is not null, it is invoked when SetStreams()
315   // is called. |set_streams_observer| is not owned by this object. If not
316   // null, it must be valid at least until this sender becomes stopped.
317   static rtc::scoped_refptr<VideoRtpSender> Create(
318       rtc::Thread* worker_thread,
319       const std::string& id,
320       SetStreamsObserver* set_streams_observer);
321   virtual ~VideoRtpSender();
322 
323   // ObserverInterface implementation
324   void OnChanged() override;
325 
media_type()326   cricket::MediaType media_type() const override {
327     return cricket::MEDIA_TYPE_VIDEO;
328   }
track_kind()329   std::string track_kind() const override {
330     return MediaStreamTrackInterface::kVideoKind;
331   }
332 
333   rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
334 
335  protected:
336   VideoRtpSender(rtc::Thread* worker_thread,
337                  const std::string& id,
338                  SetStreamsObserver* set_streams_observer);
339 
340   void SetSend() override;
341   void ClearSend() override;
342 
343   // Hook to allow custom logic when tracks are attached.
344   void AttachTrack() override;
345 
346  private:
video_media_channel()347   cricket::VideoMediaChannel* video_media_channel() {
348     return static_cast<cricket::VideoMediaChannel*>(media_channel_);
349   }
video_track()350   rtc::scoped_refptr<VideoTrackInterface> video_track() const {
351     return rtc::scoped_refptr<VideoTrackInterface>(
352         static_cast<VideoTrackInterface*>(track_.get()));
353   }
354 
355   VideoTrackInterface::ContentHint cached_track_content_hint_ =
356       VideoTrackInterface::ContentHint::kNone;
357 };
358 
359 }  // namespace webrtc
360 
361 #endif  // PC_RTP_SENDER_H_
362