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