rtp_sender.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. void SetEncoderToPacketizerFrameTransformer(
  123. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
  124. protected:
  125. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  126. // is called. |set_streams_observer| is not owned by this object. If not
  127. // null, it must be valid at least until this sender becomes stopped.
  128. RtpSenderBase(rtc::Thread* worker_thread,
  129. const std::string& id,
  130. SetStreamsObserver* set_streams_observer);
  131. // TODO(nisse): Since SSRC == 0 is technically valid, figure out
  132. // some other way to test if we have a valid SSRC.
  133. bool can_send_track() const { return track_ && ssrc_; }
  134. virtual std::string track_kind() const = 0;
  135. // Enable sending on the media channel.
  136. virtual void SetSend() = 0;
  137. // Disable sending on the media channel.
  138. virtual void ClearSend() = 0;
  139. // Template method pattern to allow subclasses to add custom behavior for
  140. // when tracks are attached, detached, and for adding tracks to statistics.
  141. virtual void AttachTrack() {}
  142. virtual void DetachTrack() {}
  143. virtual void AddTrackToStats() {}
  144. virtual void RemoveTrackFromStats() {}
  145. rtc::Thread* worker_thread_;
  146. uint32_t ssrc_ = 0;
  147. bool stopped_ = false;
  148. int attachment_id_ = 0;
  149. const std::string id_;
  150. std::vector<std::string> stream_ids_;
  151. RtpParameters init_parameters_;
  152. cricket::MediaChannel* media_channel_ = nullptr;
  153. rtc::scoped_refptr<MediaStreamTrackInterface> track_;
  154. rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_;
  155. rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
  156. // |last_transaction_id_| is used to verify that |SetParameters| is receiving
  157. // the parameters object that was last returned from |GetParameters|.
  158. // As such, it is used for internal verification and is not observable by the
  159. // the client. It is marked as mutable to enable |GetParameters| to be a
  160. // const method.
  161. mutable absl::optional<std::string> last_transaction_id_;
  162. std::vector<std::string> disabled_rids_;
  163. SetStreamsObserver* set_streams_observer_ = nullptr;
  164. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_;
  165. };
  166. // LocalAudioSinkAdapter receives data callback as a sink to the local
  167. // AudioTrack, and passes the data to the sink of AudioSource.
  168. class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
  169. public cricket::AudioSource {
  170. public:
  171. LocalAudioSinkAdapter();
  172. virtual ~LocalAudioSinkAdapter();
  173. private:
  174. // AudioSinkInterface implementation.
  175. void OnData(const void* audio_data,
  176. int bits_per_sample,
  177. int sample_rate,
  178. size_t number_of_channels,
  179. size_t number_of_frames,
  180. absl::optional<int64_t> absolute_capture_timestamp_ms) override;
  181. // AudioSinkInterface implementation.
  182. void OnData(const void* audio_data,
  183. int bits_per_sample,
  184. int sample_rate,
  185. size_t number_of_channels,
  186. size_t number_of_frames) override {
  187. OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
  188. number_of_frames,
  189. /*absolute_capture_timestamp_ms=*/absl::nullopt);
  190. }
  191. // cricket::AudioSource implementation.
  192. void SetSink(cricket::AudioSource::Sink* sink) override;
  193. cricket::AudioSource::Sink* sink_;
  194. // Critical section protecting |sink_|.
  195. rtc::CriticalSection lock_;
  196. };
  197. class AudioRtpSender : public DtmfProviderInterface, public RtpSenderBase {
  198. public:
  199. // Construct an RtpSender for audio with the given sender ID.
  200. // The sender is initialized with no track to send and no associated streams.
  201. // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
  202. // at the appropriate times.
  203. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  204. // is called. |set_streams_observer| is not owned by this object. If not
  205. // null, it must be valid at least until this sender becomes stopped.
  206. static rtc::scoped_refptr<AudioRtpSender> Create(
  207. rtc::Thread* worker_thread,
  208. const std::string& id,
  209. StatsCollector* stats,
  210. SetStreamsObserver* set_streams_observer);
  211. virtual ~AudioRtpSender();
  212. // DtmfSenderProvider implementation.
  213. bool CanInsertDtmf() override;
  214. bool InsertDtmf(int code, int duration) override;
  215. sigslot::signal0<>* GetOnDestroyedSignal() override;
  216. // ObserverInterface implementation.
  217. void OnChanged() override;
  218. cricket::MediaType media_type() const override {
  219. return cricket::MEDIA_TYPE_AUDIO;
  220. }
  221. std::string track_kind() const override {
  222. return MediaStreamTrackInterface::kAudioKind;
  223. }
  224. rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
  225. protected:
  226. AudioRtpSender(rtc::Thread* worker_thread,
  227. const std::string& id,
  228. StatsCollector* stats,
  229. SetStreamsObserver* set_streams_observer);
  230. void SetSend() override;
  231. void ClearSend() override;
  232. // Hooks to allow custom logic when tracks are attached and detached.
  233. void AttachTrack() override;
  234. void DetachTrack() override;
  235. void AddTrackToStats() override;
  236. void RemoveTrackFromStats() override;
  237. private:
  238. cricket::VoiceMediaChannel* voice_media_channel() {
  239. return static_cast<cricket::VoiceMediaChannel*>(media_channel_);
  240. }
  241. rtc::scoped_refptr<AudioTrackInterface> audio_track() const {
  242. return rtc::scoped_refptr<AudioTrackInterface>(
  243. static_cast<AudioTrackInterface*>(track_.get()));
  244. }
  245. sigslot::signal0<> SignalDestroyed;
  246. StatsCollector* stats_ = nullptr;
  247. rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
  248. bool cached_track_enabled_ = false;
  249. // Used to pass the data callback from the |track_| to the other end of
  250. // cricket::AudioSource.
  251. std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
  252. };
  253. class VideoRtpSender : public RtpSenderBase {
  254. public:
  255. // Construct an RtpSender for video with the given sender ID.
  256. // The sender is initialized with no track to send and no associated streams.
  257. // If |set_streams_observer| is not null, it is invoked when SetStreams()
  258. // is called. |set_streams_observer| is not owned by this object. If not
  259. // null, it must be valid at least until this sender becomes stopped.
  260. static rtc::scoped_refptr<VideoRtpSender> Create(
  261. rtc::Thread* worker_thread,
  262. const std::string& id,
  263. SetStreamsObserver* set_streams_observer);
  264. virtual ~VideoRtpSender();
  265. // ObserverInterface implementation
  266. void OnChanged() override;
  267. cricket::MediaType media_type() const override {
  268. return cricket::MEDIA_TYPE_VIDEO;
  269. }
  270. std::string track_kind() const override {
  271. return MediaStreamTrackInterface::kVideoKind;
  272. }
  273. rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
  274. protected:
  275. VideoRtpSender(rtc::Thread* worker_thread,
  276. const std::string& id,
  277. SetStreamsObserver* set_streams_observer);
  278. void SetSend() override;
  279. void ClearSend() override;
  280. // Hook to allow custom logic when tracks are attached.
  281. void AttachTrack() override;
  282. private:
  283. cricket::VideoMediaChannel* video_media_channel() {
  284. return static_cast<cricket::VideoMediaChannel*>(media_channel_);
  285. }
  286. rtc::scoped_refptr<VideoTrackInterface> video_track() const {
  287. return rtc::scoped_refptr<VideoTrackInterface>(
  288. static_cast<VideoTrackInterface*>(track_.get()));
  289. }
  290. VideoTrackInterface::ContentHint cached_track_content_hint_ =
  291. VideoTrackInterface::ContentHint::kNone;
  292. };
  293. } // namespace webrtc
  294. #endif // PC_RTP_SENDER_H_