rtp_sender.h 13 KB

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