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