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