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.

231 lines
9.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. RtpTransceiver(
  63. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
  64. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  65. receiver,
  66. cricket::ChannelManager* channel_manager);
  67. ~RtpTransceiver() override;
  68. // Returns the Voice/VideoChannel set for this transceiver. May be null if
  69. // the transceiver is not in the currently set local/remote description.
  70. cricket::ChannelInterface* channel() const { return channel_; }
  71. // Sets the Voice/VideoChannel. The caller must pass in the correct channel
  72. // implementation based on the type of the transceiver.
  73. void SetChannel(cricket::ChannelInterface* channel);
  74. // Adds an RtpSender of the appropriate type to be owned by this transceiver.
  75. // Must not be null.
  76. void AddSender(
  77. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender);
  78. // Removes the given RtpSender. Returns false if the sender is not owned by
  79. // this transceiver.
  80. bool RemoveSender(RtpSenderInterface* sender);
  81. // Returns a vector of the senders owned by this transceiver.
  82. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  83. senders() const {
  84. return senders_;
  85. }
  86. // Adds an RtpReceiver of the appropriate type to be owned by this
  87. // transceiver. Must not be null.
  88. void AddReceiver(
  89. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  90. receiver);
  91. // Removes the given RtpReceiver. Returns false if the sender is not owned by
  92. // this transceiver.
  93. bool RemoveReceiver(RtpReceiverInterface* receiver);
  94. // Returns a vector of the receivers owned by this transceiver.
  95. std::vector<
  96. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  97. receivers() const {
  98. return receivers_;
  99. }
  100. // Returns the backing object for the transceiver's Unified Plan sender.
  101. rtc::scoped_refptr<RtpSenderInternal> sender_internal() const;
  102. // Returns the backing object for the transceiver's Unified Plan receiver.
  103. rtc::scoped_refptr<RtpReceiverInternal> receiver_internal() const;
  104. // RtpTransceivers are not associated until they have a corresponding media
  105. // section set in SetLocalDescription or SetRemoteDescription. Therefore,
  106. // when setting a local offer we need a way to remember which transceiver was
  107. // used to create which media section in the offer. Storing the mline index
  108. // in CreateOffer is specified in JSEP to allow us to do that.
  109. absl::optional<size_t> mline_index() const { return mline_index_; }
  110. void set_mline_index(absl::optional<size_t> mline_index) {
  111. mline_index_ = mline_index;
  112. }
  113. // Sets the MID for this transceiver. If the MID is not null, then the
  114. // transceiver is considered "associated" with the media section that has the
  115. // same MID.
  116. void set_mid(const absl::optional<std::string>& mid) { mid_ = mid; }
  117. // Sets the intended direction for this transceiver. Intended to be used
  118. // internally over SetDirection since this does not trigger a negotiation
  119. // needed callback.
  120. void set_direction(RtpTransceiverDirection direction) {
  121. direction_ = direction;
  122. }
  123. // Sets the current direction for this transceiver as negotiated in an offer/
  124. // answer exchange. The current direction is null before an answer with this
  125. // transceiver has been set.
  126. void set_current_direction(RtpTransceiverDirection direction);
  127. // Sets the fired direction for this transceiver. The fired direction is null
  128. // until SetRemoteDescription is called or an answer is set (either local or
  129. // remote).
  130. void set_fired_direction(RtpTransceiverDirection direction);
  131. // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
  132. // reused only if they were added by AddTrack.
  133. void set_created_by_addtrack(bool created_by_addtrack) {
  134. created_by_addtrack_ = created_by_addtrack;
  135. }
  136. bool created_by_addtrack() const { return created_by_addtrack_; }
  137. // Returns true if this transceiver has ever had the current direction set to
  138. // sendonly or sendrecv.
  139. bool has_ever_been_used_to_send() const {
  140. return has_ever_been_used_to_send_;
  141. }
  142. // Fired when the RtpTransceiver state changes such that negotiation is now
  143. // needed (e.g., in response to a direction change).
  144. sigslot::signal0<> SignalNegotiationNeeded;
  145. // RtpTransceiverInterface implementation.
  146. cricket::MediaType media_type() const override;
  147. absl::optional<std::string> mid() const override;
  148. rtc::scoped_refptr<RtpSenderInterface> sender() const override;
  149. rtc::scoped_refptr<RtpReceiverInterface> receiver() const override;
  150. bool stopped() const override;
  151. RtpTransceiverDirection direction() const override;
  152. void SetDirection(RtpTransceiverDirection new_direction) override;
  153. absl::optional<RtpTransceiverDirection> current_direction() const override;
  154. absl::optional<RtpTransceiverDirection> fired_direction() const override;
  155. void Stop() override;
  156. RTCError SetCodecPreferences(
  157. rtc::ArrayView<RtpCodecCapability> codecs) override;
  158. std::vector<RtpCodecCapability> codec_preferences() const override {
  159. return codec_preferences_;
  160. }
  161. private:
  162. void OnFirstPacketReceived(cricket::ChannelInterface* channel);
  163. const bool unified_plan_;
  164. const cricket::MediaType media_type_;
  165. std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
  166. senders_;
  167. std::vector<
  168. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
  169. receivers_;
  170. bool stopped_ = false;
  171. RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive;
  172. absl::optional<RtpTransceiverDirection> current_direction_;
  173. absl::optional<RtpTransceiverDirection> fired_direction_;
  174. absl::optional<std::string> mid_;
  175. absl::optional<size_t> mline_index_;
  176. bool created_by_addtrack_ = false;
  177. bool has_ever_been_used_to_send_ = false;
  178. cricket::ChannelInterface* channel_ = nullptr;
  179. cricket::ChannelManager* channel_manager_ = nullptr;
  180. std::vector<RtpCodecCapability> codec_preferences_;
  181. };
  182. BEGIN_SIGNALING_PROXY_MAP(RtpTransceiver)
  183. PROXY_SIGNALING_THREAD_DESTRUCTOR()
  184. PROXY_CONSTMETHOD0(cricket::MediaType, media_type)
  185. PROXY_CONSTMETHOD0(absl::optional<std::string>, mid)
  186. PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender)
  187. PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver)
  188. PROXY_CONSTMETHOD0(bool, stopped)
  189. PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction)
  190. PROXY_METHOD1(void, SetDirection, RtpTransceiverDirection)
  191. PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, current_direction)
  192. PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, fired_direction)
  193. PROXY_METHOD0(void, Stop)
  194. PROXY_METHOD1(webrtc::RTCError,
  195. SetCodecPreferences,
  196. rtc::ArrayView<RtpCodecCapability>)
  197. PROXY_CONSTMETHOD0(std::vector<RtpCodecCapability>, codec_preferences)
  198. END_PROXY_MAP()
  199. } // namespace webrtc
  200. #endif // PC_RTP_TRANSCEIVER_H_