rtp_transceiver.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright 2017 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. #ifndef PC_RTP_TRANSCEIVER_H_
  11. #define PC_RTP_TRANSCEIVER_H_
  12. #include <string>
  13. #include <vector>
  14. #include "api/rtp_transceiver_interface.h"
  15. #include "pc/channel_interface.h"
  16. #include "pc/channel_manager.h"
  17. #include "pc/rtp_receiver.h"
  18. #include "pc/rtp_sender.h"
  19. namespace webrtc {
  20. // Implementation of the public RtpTransceiverInterface.
  21. //
  22. // The RtpTransceiverInterface is only intended to be used with a PeerConnection
  23. // that enables Unified Plan SDP. Thus, the methods that only need to implement
  24. // public API features and are not used internally can assume exactly one sender
  25. // and receiver.
  26. //
  27. // Since the RtpTransceiver is used internally by PeerConnection for tracking
  28. // RtpSenders, RtpReceivers, and BaseChannels, and PeerConnection needs to be
  29. // backwards compatible with Plan B SDP, this implementation is more flexible
  30. // than that required by the WebRTC specification.
  31. //
  32. // With Plan B SDP, an RtpTransceiver can have any number of senders and
  33. // receivers which map to a=ssrc lines in the m= section.
  34. // With Unified Plan SDP, an RtpTransceiver will have exactly one sender and one
  35. // receiver which are encapsulated by the m= section.
  36. //
  37. // This class manages the RtpSenders, RtpReceivers, and BaseChannel associated
  38. // with this m= section. Since the transceiver, senders, and receivers are
  39. // reference counted and can be referenced from JavaScript (in Chromium), these
  40. // objects must be ready to live for an arbitrary amount of time. The
  41. // BaseChannel is not reference counted and is owned by the ChannelManager, so
  42. // the PeerConnection must take care of creating/deleting the BaseChannel and
  43. // setting the channel reference in the transceiver to null when it has been
  44. // deleted.
  45. //
  46. // The RtpTransceiver is specialized to either audio or video according to the
  47. // MediaType specified in the constructor. Audio RtpTransceivers will have
  48. // AudioRtpSenders, AudioRtpReceivers, and a VoiceChannel. Video RtpTransceivers
  49. // will have VideoRtpSenders, VideoRtpReceivers, and a VideoChannel.
  50. class RtpTransceiver final
  51. : public rtc::RefCountedObject<RtpTransceiverInterface>,
  52. public sigslot::has_slots<> {
  53. public:
  54. // Construct a Plan B-style RtpTransceiver with no senders, receivers, or
  55. // channel set.
  56. // |media_type| specifies the type of RtpTransceiver (and, by transitivity,
  57. // the type of senders, receivers, and channel). Can either by audio or video.
  58. explicit RtpTransceiver(cricket::MediaType media_type);
  59. // Construct a Unified Plan-style RtpTransceiver with the given sender and
  60. // receiver. The media type will be derived from the media types of the sender
  61. // and receiver. The sender and receiver should have the same media type.
  62. // |HeaderExtensionsToOffer| is used for initializing the return value of
  63. // HeaderExtensionsToOffer().
  64. RtpTransceiver(
  65. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
  66. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  67. receiver,
  68. cricket::ChannelManager* channel_manager,
  69. std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer);
  70. ~RtpTransceiver() override;
  71. // Returns the Voice/VideoChannel set for this transceiver. May be null if
  72. // the transceiver is not in the currently set local/remote description.
  73. cricket::ChannelInterface* channel() const { return channel_; }
  74. // Sets the Voice/VideoChannel. The caller must pass in the correct channel
  75. // implementation based on the type of the transceiver.
  76. void SetChannel(cricket::ChannelInterface* channel);
  77. // Adds an RtpSender of the appropriate type to be owned by this transceiver.
  78. // Must not be null.
  79. void AddSender(
  80. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender);
  81. // Removes the given RtpSender. Returns false if the sender is not owned by
  82. // this transceiver.
  83. bool RemoveSender(RtpSenderInterface* sender);
  84. // Returns a vector of the senders owned by this transceiver.
  85. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  86. senders() const {
  87. return senders_;
  88. }
  89. // Adds an RtpReceiver of the appropriate type to be owned by this
  90. // transceiver. Must not be null.
  91. void AddReceiver(
  92. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  93. receiver);
  94. // Removes the given RtpReceiver. Returns false if the sender is not owned by
  95. // this transceiver.
  96. bool RemoveReceiver(RtpReceiverInterface* receiver);
  97. // Returns a vector of the receivers owned by this transceiver.
  98. std::vector<
  99. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  100. receivers() const {
  101. return receivers_;
  102. }
  103. // Returns the backing object for the transceiver's Unified Plan sender.
  104. rtc::scoped_refptr<RtpSenderInternal> sender_internal() const;
  105. // Returns the backing object for the transceiver's Unified Plan receiver.
  106. rtc::scoped_refptr<RtpReceiverInternal> receiver_internal() const;
  107. // RtpTransceivers are not associated until they have a corresponding media
  108. // section set in SetLocalDescription or SetRemoteDescription. Therefore,
  109. // when setting a local offer we need a way to remember which transceiver was
  110. // used to create which media section in the offer. Storing the mline index
  111. // in CreateOffer is specified in JSEP to allow us to do that.
  112. absl::optional<size_t> mline_index() const { return mline_index_; }
  113. void set_mline_index(absl::optional<size_t> mline_index) {
  114. mline_index_ = mline_index;
  115. }
  116. // Sets the MID for this transceiver. If the MID is not null, then the
  117. // transceiver is considered "associated" with the media section that has the
  118. // same MID.
  119. void set_mid(const absl::optional<std::string>& mid) { mid_ = mid; }
  120. // Sets the intended direction for this transceiver. Intended to be used
  121. // internally over SetDirection since this does not trigger a negotiation
  122. // needed callback.
  123. void set_direction(RtpTransceiverDirection direction) {
  124. direction_ = direction;
  125. }
  126. // Sets the current direction for this transceiver as negotiated in an offer/
  127. // answer exchange. The current direction is null before an answer with this
  128. // transceiver has been set.
  129. void set_current_direction(RtpTransceiverDirection direction);
  130. // Sets the fired direction for this transceiver. The fired direction is null
  131. // until SetRemoteDescription is called or an answer is set (either local or
  132. // remote).
  133. void set_fired_direction(RtpTransceiverDirection direction);
  134. // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
  135. // reused only if they were added by AddTrack.
  136. void set_created_by_addtrack(bool created_by_addtrack) {
  137. created_by_addtrack_ = created_by_addtrack;
  138. }
  139. // If AddTrack has been called then transceiver can't be removed during
  140. // rollback.
  141. void set_reused_for_addtrack(bool reused_for_addtrack) {
  142. reused_for_addtrack_ = reused_for_addtrack;
  143. }
  144. bool created_by_addtrack() const { return created_by_addtrack_; }
  145. bool reused_for_addtrack() const { return reused_for_addtrack_; }
  146. // Returns true if this transceiver has ever had the current direction set to
  147. // sendonly or sendrecv.
  148. bool has_ever_been_used_to_send() const {
  149. return has_ever_been_used_to_send_;
  150. }
  151. // Informs the transceiver that its owning
  152. // PeerConnection is closed.
  153. void SetPeerConnectionClosed();
  154. // Executes the "stop the RTCRtpTransceiver" procedure from
  155. // the webrtc-pc specification, described under the stop() method.
  156. void StopTransceiverProcedure();
  157. // Fired when the RtpTransceiver state changes such that negotiation is now
  158. // needed (e.g., in response to a direction change).
  159. sigslot::signal0<> SignalNegotiationNeeded;
  160. // RtpTransceiverInterface implementation.
  161. cricket::MediaType media_type() const override;
  162. absl::optional<std::string> mid() const override;
  163. rtc::scoped_refptr<RtpSenderInterface> sender() const override;
  164. rtc::scoped_refptr<RtpReceiverInterface> receiver() const override;
  165. bool stopped() const override;
  166. bool stopping() const override;
  167. RtpTransceiverDirection direction() const override;
  168. RTCError SetDirectionWithError(
  169. RtpTransceiverDirection new_direction) override;
  170. absl::optional<RtpTransceiverDirection> current_direction() const override;
  171. absl::optional<RtpTransceiverDirection> fired_direction() const override;
  172. RTCError StopStandard() override;
  173. void StopInternal() override;
  174. RTCError SetCodecPreferences(
  175. rtc::ArrayView<RtpCodecCapability> codecs) override;
  176. std::vector<RtpCodecCapability> codec_preferences() const override {
  177. return codec_preferences_;
  178. }
  179. std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer()
  180. const override;
  181. RTCError SetOfferedRtpHeaderExtensions(
  182. rtc::ArrayView<const RtpHeaderExtensionCapability>
  183. header_extensions_to_offer) override;
  184. private:
  185. void OnFirstPacketReceived(cricket::ChannelInterface* channel);
  186. void StopSendingAndReceiving();
  187. // Enforce that this object is created, used and destroyed on one thread.
  188. const TaskQueueBase* thread_;
  189. const bool unified_plan_;
  190. const cricket::MediaType media_type_;
  191. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  192. senders_;
  193. std::vector<
  194. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  195. receivers_;
  196. bool stopped_ = false;
  197. bool stopping_ RTC_GUARDED_BY(thread_) = false;
  198. bool is_pc_closed_ = false;
  199. RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive;
  200. absl::optional<RtpTransceiverDirection> current_direction_;
  201. absl::optional<RtpTransceiverDirection> fired_direction_;
  202. absl::optional<std::string> mid_;
  203. absl::optional<size_t> mline_index_;
  204. bool created_by_addtrack_ = false;
  205. bool reused_for_addtrack_ = false;
  206. bool has_ever_been_used_to_send_ = false;
  207. cricket::ChannelInterface* channel_ = nullptr;
  208. cricket::ChannelManager* channel_manager_ = nullptr;
  209. std::vector<RtpCodecCapability> codec_preferences_;
  210. std::vector<RtpHeaderExtensionCapability> header_extensions_to_offer_;
  211. };
  212. BEGIN_SIGNALING_PROXY_MAP(RtpTransceiver)
  213. PROXY_SIGNALING_THREAD_DESTRUCTOR()
  214. BYPASS_PROXY_CONSTMETHOD0(cricket::MediaType, media_type)
  215. PROXY_CONSTMETHOD0(absl::optional<std::string>, mid)
  216. PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender)
  217. PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver)
  218. PROXY_CONSTMETHOD0(bool, stopped)
  219. PROXY_CONSTMETHOD0(bool, stopping)
  220. PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction)
  221. PROXY_METHOD1(webrtc::RTCError, SetDirectionWithError, RtpTransceiverDirection)
  222. PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, current_direction)
  223. PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, fired_direction)
  224. PROXY_METHOD0(webrtc::RTCError, StopStandard)
  225. PROXY_METHOD0(void, StopInternal)
  226. PROXY_METHOD1(webrtc::RTCError,
  227. SetCodecPreferences,
  228. rtc::ArrayView<RtpCodecCapability>)
  229. PROXY_CONSTMETHOD0(std::vector<RtpCodecCapability>, codec_preferences)
  230. PROXY_CONSTMETHOD0(std::vector<RtpHeaderExtensionCapability>,
  231. HeaderExtensionsToOffer)
  232. PROXY_METHOD1(webrtc::RTCError,
  233. SetOfferedRtpHeaderExtensions,
  234. rtc::ArrayView<const RtpHeaderExtensionCapability>)
  235. END_PROXY_MAP()
  236. } // namespace webrtc
  237. #endif // PC_RTP_TRANSCEIVER_H_