You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. // This file contains classes that implement RtpSenderInterface.
  11. // An RtpSender associates a MediaStreamTrackInterface with an underlying
  12. // transport (provided by AudioProviderInterface/VideoProviderInterface)
  13. #ifndef PC_RTP_SENDER_H_
  14. #define PC_RTP_SENDER_H_
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "api/media_stream_interface.h"
  19. #include "api/rtp_sender_interface.h"
  20. #include "media/base/audio_source.h"
  21. #include "media/base/media_channel.h"
  22. #include "pc/dtmf_sender.h"
  23. #include "rtc_base/critical_section.h"
  24. namespace webrtc {
  25. class StatsCollector;
  26. bool UnimplementedRtpParameterHasValue(const RtpParameters& parameters);
  27. // Internal interface used by PeerConnection.
  28. class RtpSenderInternal : public RtpSenderInterface {
  29. public:
  30. // Sets the underlying MediaEngine channel associated with this RtpSender.
  31. // A VoiceMediaChannel should be used for audio RtpSenders and
  32. // a VideoMediaChannel should be used for video RtpSenders.
  33. // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
  34. virtual void SetMediaChannel(cricket::MediaChannel* media_channel) = 0;
  35. // Used to set the SSRC of the sender, once a local description has been set.
  36. // If |ssrc| is 0, this indiates that the sender should disconnect from the
  37. // underlying transport (this occurs if the sender isn't seen in a local
  38. // description).
  39. virtual void SetSsrc(uint32_t ssrc) = 0;
  40. virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
  41. virtual void set_init_send_encodings(
  42. const std::vector<RtpEncodingParameters>& init_send_encodings) = 0;
  43. virtual void set_transport(
  44. rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) = 0;
  45. virtual void Stop() = 0;
  46. // |GetParameters| and |SetParameters| operate with a transactional model.
  47. // Allow access to get/set parameters without invalidating transaction id.
  48. virtual RtpParameters GetParametersInternal() const = 0;
  49. virtual RTCError SetParametersInternal(const RtpParameters& parameters) = 0;
  50. // Returns an ID that changes every time SetTrack() is called, but
  51. // otherwise remains constant. Used to generate IDs for stats.
  52. // The special value zero means that no track is attached.
  53. virtual int AttachmentId() const = 0;
  54. // Disables the layers identified by the specified RIDs.
  55. // If the specified list is empty, this is a no-op.
  56. virtual RTCError DisableEncodingLayers(
  57. const std::vector<std::string>& rid) = 0;
  58. };
  59. // Shared implementation for RtpSenderInternal interface.
  60. class RtpSenderBase : public RtpSenderInternal, public ObserverInterface {
  61. public:
  62. class SetStreamsObserver {
  63. public:
  64. virtual ~SetStreamsObserver() = default;
  65. virtual void OnSetStreams() = 0;
  66. };
  67. // Sets the underlying MediaEngine channel associated with this RtpSender.
  68. // A VoiceMediaChannel should be used for audio RtpSenders and
  69. // a VideoMediaChannel should be used for video RtpSenders.
  70. // Must call SetMediaChannel(nullptr) before the media channel is destroyed.
  71. void SetMediaChannel(cricket::MediaChannel* media_channel) override;
  72. bool SetTrack(MediaStreamTrackInterface* track) override;
  73. rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
  74. return track_;
  75. }
  76. RtpParameters GetParameters() const override;
  77. RTCError SetParameters(const RtpParameters& parameters) override;
  78. // |GetParameters| and |SetParameters| operate with a transactional model.
  79. // Allow access to get/set parameters without invalidating transaction id.
  80. RtpParameters GetParametersInternal() const override;
  81. RTCError SetParametersInternal(const RtpParameters& parameters) override;
  82. // Used to set the SSRC of the sender, once a local description has been set.
  83. // If |ssrc| is 0, this indiates that the sender should disconnect from the
  84. // underlying transport (this occurs if the sender isn't seen in a local
  85. // description).
  86. void SetSsrc(uint32_t ssrc) override;
  87. uint32_t ssrc() const override { return ssrc_; }
  88. std::vector<std::string> stream_ids() const override { return stream_ids_; }
  89. void set_stream_ids(const std::vector<std::string>& stream_ids) override {
  90. stream_ids_ = stream_ids;
  91. }
  92. void SetStreams(const std::vector<std::string>& stream_ids) override;
  93. std::string id() const override { return id_; }
  94. void set_init_send_encodings(
  95. const std::vector<RtpEncodingParameters>& init_send_encodings) override {
  96. init_parameters_.encodings = init_send_encodings;
  97. }
  98. std::vector<RtpEncodingParameters> init_send_encodings() const override {
  99. return init_parameters_.encodings;
  100. }
  101. void set_transport(
  102. rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override {
  103. dtls_transport_ = dtls_transport;
  104. }
  105. rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override {
  106. return dtls_transport_;
  107. }
  108. void SetFrameEncryptor(
  109. rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
  110. rtc::scoped_refptr<FrameEncryptorInterface> GetFrameEncryptor()
  111. const override {
  112. return frame_encryptor_;
  113. }
  114. void Stop() override;
  115. // Returns an ID that changes every time SetTrack() is called, but
  116. // otherwise remains constant. Used to generate IDs for stats.
  117. // The special value zero means that no track is attached.
  118. int AttachmentId() const override { return attachment_id_; }
  119. // Disables the layers identified by the specified RIDs.
  120. // If the specified list is empty, this is a no-op.
  121. RTCError DisableEncodingLayers(const std::vector<std::string>& rid) override;
  122. protected:
  123. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  124. // is called. |set_streams_observer| is not owned by this object. If not
  125. // null, it must be valid at least until this sender becomes stopped.
  126. RtpSenderBase(rtc::Thread* worker_thread,
  127. const std::string& id,
  128. SetStreamsObserver* set_streams_observer);
  129. // TODO(nisse): Since SSRC == 0 is technically valid, figure out
  130. // some other way to test if we have a valid SSRC.
  131. bool can_send_track() const { return track_ && ssrc_; }
  132. virtual std::string track_kind() const = 0;
  133. // Enable sending on the media channel.
  134. virtual void SetSend() = 0;
  135. // Disable sending on the media channel.
  136. virtual void ClearSend() = 0;
  137. // Template method pattern to allow subclasses to add custom behavior for
  138. // when tracks are attached, detached, and for adding tracks to statistics.
  139. virtual void AttachTrack() {}
  140. virtual void DetachTrack() {}
  141. virtual void AddTrackToStats() {}
  142. virtual void RemoveTrackFromStats() {}
  143. rtc::Thread* worker_thread_;
  144. uint32_t ssrc_ = 0;
  145. bool stopped_ = false;
  146. int attachment_id_ = 0;
  147. const std::string id_;
  148. std::vector<std::string> stream_ids_;
  149. RtpParameters init_parameters_;
  150. cricket::MediaChannel* media_channel_ = nullptr;
  151. rtc::scoped_refptr<MediaStreamTrackInterface> track_;
  152. rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
  153. rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
  154. // |last_transaction_id_| is used to verify that |SetParameters| is receiving
  155. // the parameters object that was last returned from |GetParameters|.
  156. // As such, it is used for internal verification and is not observable by the
  157. // the client. It is marked as mutable to enable |GetParameters| to be a
  158. // const method.
  159. mutable absl::optional<std::string> last_transaction_id_;
  160. std::vector<std::string> disabled_rids_;
  161. SetStreamsObserver* set_streams_observer_ = nullptr;
  162. };
  163. // LocalAudioSinkAdapter receives data callback as a sink to the local
  164. // AudioTrack, and passes the data to the sink of AudioSource.
  165. class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
  166. public cricket::AudioSource {
  167. public:
  168. LocalAudioSinkAdapter();
  169. virtual ~LocalAudioSinkAdapter();
  170. private:
  171. // AudioSinkInterface implementation.
  172. void OnData(const void* audio_data,
  173. int bits_per_sample,
  174. int sample_rate,
  175. size_t number_of_channels,
  176. size_t number_of_frames) override;
  177. // cricket::AudioSource implementation.
  178. void SetSink(cricket::AudioSource::Sink* sink) override;
  179. cricket::AudioSource::Sink* sink_;
  180. // Critical section protecting |sink_|.
  181. rtc::CriticalSection lock_;
  182. };
  183. class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
  184. public:
  185. // Construct an RtpSender for audio with the given sender ID.
  186. // The sender is initialized with no track to send and no associated streams.
  187. // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
  188. // at the appropriate times.
  189. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  190. // is called. |set_streams_observer| is not owned by this object. If not
  191. // null, it must be valid at least until this sender becomes stopped.
  192. static rtc::scoped_refptr<AudioRtpSender> Create(
  193. rtc::Thread* worker_thread,
  194. const std::string& id,
  195. StatsCollector* stats,
  196. SetStreamsObserver* set_streams_observer);
  197. virtual ~AudioRtpSender();
  198. // DtmfSenderProvider implementation.
  199. bool CanInsertDtmf() override;
  200. bool InsertDtmf(int code, int duration) override;
  201. sigslot::signal0<>* GetOnDestroyedSignal() override;
  202. // ObserverInterface implementation.
  203. void OnChanged() override;
  204. cricket::MediaType media_type() const override {
  205. return cricket::MEDIA_TYPE_AUDIO;
  206. }
  207. std::string track_kind() const override {
  208. return MediaStreamTrackInterface::kAudioKind;
  209. }
  210. rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
  211. protected:
  212. AudioRtpSender(rtc::Thread* worker_thread,
  213. const std::string& id,
  214. StatsCollector* stats,
  215. SetStreamsObserver* set_streams_observer);
  216. void SetSend() override;
  217. void ClearSend() override;
  218. // Hooks to allow custom logic when tracks are attached and detached.
  219. void AttachTrack() override;
  220. void DetachTrack() override;
  221. void AddTrackToStats() override;
  222. void RemoveTrackFromStats() override;
  223. private:
  224. cricket::VoiceMediaChannel* voice_media_channel() {
  225. return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
  226. }
  227. rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
  228. return rtc::scoped_refptr<AudioTrackInterface>(
  229. static_cast<AudioTrackInterface*>(track_.get()));
  230. }
  231. sigslot::signal0<> SignalDestroyed;
  232. StatsCollector* stats_ = nullptr;
  233. rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
  234. bool cached_track_enabled_ = false;
  235. // Used to pass the data callback from the |track_| to the other end of
  236. // cricket::AudioSource.
  237. std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
  238. };
  239. class VideoRtpSender : public RtpSenderBase {
  240. public:
  241. // Construct an RtpSender for video with the given sender ID.
  242. // The sender is initialized with no track to send and no associated streams.
  243. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  244. // is called. |set_streams_observer| is not owned by this object. If not
  245. // null, it must be valid at least until this sender becomes stopped.
  246. static rtc::scoped_refptr<VideoRtpSender> Create(
  247. rtc::Thread* worker_thread,
  248. const std::string& id,
  249. SetStreamsObserver* set_streams_observer);
  250. virtual ~VideoRtpSender();
  251. // ObserverInterface implementation
  252. void OnChanged() override;
  253. cricket::MediaType media_type() const override {
  254. return cricket::MEDIA_TYPE_VIDEO;
  255. }
  256. std::string track_kind() const override {
  257. return MediaStreamTrackInterface::kVideoKind;
  258. }
  259. rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
  260. protected:
  261. VideoRtpSender(rtc::Thread* worker_thread,
  262. const std::string& id,
  263. SetStreamsObserver* set_streams_observer);
  264. void SetSend() override;
  265. void ClearSend() override;
  266. // Hook to allow custom logic when tracks are attached.
  267. void AttachTrack() override;
  268. private:
  269. cricket::VideoMediaChannel* video_media_channel() {
  270. return static_cast<cricket::VideoMediaChannel*>(media_channel_);
  271. }
  272. rtc::scoped_refptr<VideoTrackInterface> video_track() const {
  273. return rtc::scoped_refptr<VideoTrackInterface>(
  274. static_cast<VideoTrackInterface*>(track_.get()));
  275. }
  276. VideoTrackInterface::ContentHint cached_track_content_hint_ =
  277. VideoTrackInterface::ContentHint::kNone;
  278. };
  279. } // namespace webrtc
  280. #endif // PC_RTP_SENDER_H_