zstein | 398c3fd | 2017-07-19 20:38:02 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef PC_RTP_TRANSPORT_INTERNAL_H_ |
| 12 | #define PC_RTP_TRANSPORT_INTERNAL_H_ |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 13 | |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 14 | #include <string> |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 15 | #include <utility> |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 16 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 17 | #include "call/rtp_demuxer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "p2p/base/ice_transport_internal.h" |
| 19 | #include "pc/session_description.h" |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 20 | #include "rtc_base/callback_list.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/network_route.h" |
| 22 | #include "rtc_base/ssl_stream_adapter.h" |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | class CopyOnWriteBuffer; |
| 26 | struct PacketOptions; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 27 | } // namespace rtc |
| 28 | |
| 29 | namespace webrtc { |
| 30 | |
Yaowen Guo | fe91129 | 2022-06-01 10:57:04 | [diff] [blame] | 31 | // This class is an internal interface; it is not accessible to API consumers |
| 32 | // but is accessible to internal classes in order to send and receive RTP and |
| 33 | // RTCP packets belonging to a single RTP session. Additional convenience and |
| 34 | // configuration methods are also provided. |
Bjorn A Mellem | 34cd485 | 2019-05-24 17:13:10 | [diff] [blame] | 35 | class RtpTransportInternal : public sigslot::has_slots<> { |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 36 | public: |
Bjorn A Mellem | 34cd485 | 2019-05-24 17:13:10 | [diff] [blame] | 37 | virtual ~RtpTransportInternal() = default; |
| 38 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 39 | virtual void SetRtcpMuxEnabled(bool enable) = 0; |
| 40 | |
Bjorn A Mellem | 3a1b927 | 2019-05-24 23:13:08 | [diff] [blame] | 41 | virtual const std::string& transport_name() const = 0; |
| 42 | |
| 43 | // Sets socket options on the underlying RTP or RTCP transports. |
| 44 | virtual int SetRtpOption(rtc::Socket::Option opt, int value) = 0; |
| 45 | virtual int SetRtcpOption(rtc::Socket::Option opt, int value) = 0; |
| 46 | |
Zhi Huang | f2d7beb | 2017-11-20 22:35:11 | [diff] [blame] | 47 | virtual bool rtcp_mux_enabled() const = 0; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 48 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 49 | virtual bool IsReadyToSend() const = 0; |
| 50 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 51 | // Called whenever a transport's ready-to-send state changes. The argument |
| 52 | // is true if all used transports are ready to send. This is more specific |
| 53 | // than just "writable"; it means the last send didn't return ENOTCONN. |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 54 | void SubscribeReadyToSend(const void* tag, |
| 55 | absl::AnyInvocable<void(bool)> callback) { |
| 56 | callback_list_ready_to_send_.AddReceiver(tag, std::move(callback)); |
| 57 | } |
| 58 | void UnsubscribeReadyToSend(const void* tag) { |
| 59 | callback_list_ready_to_send_.RemoveReceivers(tag); |
| 60 | } |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 61 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 62 | // Called whenever an RTCP packet is received. There is no equivalent signal |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 63 | // for demuxable RTP packets because they would be forwarded to the |
| 64 | // BaseChannel through the RtpDemuxer callback. |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 65 | void SubscribeRtcpPacketReceived( |
| 66 | const void* tag, |
| 67 | absl::AnyInvocable<void(rtc::CopyOnWriteBuffer*, int64_t)> callback) { |
| 68 | callback_list_rtcp_packet_received_.AddReceiver(tag, std::move(callback)); |
| 69 | } |
| 70 | // There doesn't seem to be a need to unsubscribe from this signal. |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 71 | |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 72 | // Called whenever a RTP packet that can not be demuxed by the transport is |
| 73 | // received. |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 74 | void SetUnDemuxableRtpPacketReceivedHandler( |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 75 | absl::AnyInvocable<void(RtpPacketReceived&)> callback) { |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 76 | callback_undemuxable_rtp_packet_received_ = std::move(callback); |
| 77 | } |
Per K | e1e94ad | 2023-03-30 14:53:59 | [diff] [blame] | 78 | |
Zhi Huang | cd3fc5d | 2017-11-29 18:41:57 | [diff] [blame] | 79 | // Called whenever the network route of the P2P layer transport changes. |
| 80 | // The argument is an optional network route. |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 81 | void SubscribeNetworkRouteChanged( |
| 82 | const void* tag, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 83 | absl::AnyInvocable<void(std::optional<rtc::NetworkRoute>)> callback) { |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 84 | callback_list_network_route_changed_.AddReceiver(tag, std::move(callback)); |
| 85 | } |
| 86 | void UnsubscribeNetworkRouteChanged(const void* tag) { |
| 87 | callback_list_network_route_changed_.RemoveReceivers(tag); |
| 88 | } |
Zhi Huang | cd3fc5d | 2017-11-29 18:41:57 | [diff] [blame] | 89 | |
Zhi Huang | f2d7beb | 2017-11-20 22:35:11 | [diff] [blame] | 90 | // Called whenever a transport's writable state might change. The argument is |
| 91 | // true if the transport is writable, otherwise it is false. |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 92 | void SubscribeWritableState(const void* tag, |
| 93 | absl::AnyInvocable<void(bool)> callback) { |
| 94 | callback_list_writable_state_.AddReceiver(tag, std::move(callback)); |
| 95 | } |
| 96 | void UnsubscribeWritableState(const void* tag) { |
| 97 | callback_list_writable_state_.RemoveReceivers(tag); |
| 98 | } |
| 99 | void SubscribeSentPacket( |
| 100 | const void* tag, |
| 101 | absl::AnyInvocable<void(const rtc::SentPacket&)> callback) { |
| 102 | callback_list_sent_packet_.AddReceiver(tag, std::move(callback)); |
| 103 | } |
| 104 | void UnsubscribeSentPacket(const void* tag) { |
| 105 | callback_list_sent_packet_.RemoveReceivers(tag); |
| 106 | } |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 107 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 108 | virtual bool IsWritable(bool rtcp) const = 0; |
| 109 | |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 110 | // TODO(zhihuang): Pass the `packet` by copy so that the original data |
Zhi Huang | f2d7beb | 2017-11-20 22:35:11 | [diff] [blame] | 111 | // wouldn't be modified. |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 112 | virtual bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 113 | const rtc::PacketOptions& options, |
| 114 | int flags) = 0; |
| 115 | |
| 116 | virtual bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 117 | const rtc::PacketOptions& options, |
| 118 | int flags) = 0; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 119 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 120 | // This method updates the RTP header extension map so that the RTP transport |
| 121 | // can parse the received packets and identify the MID. This is called by the |
| 122 | // BaseChannel when setting the content description. |
| 123 | // |
| 124 | // TODO(zhihuang): Merging and replacing following methods handling header |
| 125 | // extensions with SetParameters: |
| 126 | // UpdateRtpHeaderExtensionMap, |
| 127 | // UpdateSendEncryptedHeaderExtensionIds, |
| 128 | // UpdateRecvEncryptedHeaderExtensionIds, |
| 129 | // CacheRtpAbsSendTimeHeaderExtension, |
| 130 | virtual void UpdateRtpHeaderExtensionMap( |
| 131 | const cricket::RtpHeaderExtensions& header_extensions) = 0; |
| 132 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 133 | virtual bool IsSrtpActive() const = 0; |
Steve Anton | db67ba1 | 2018-03-20 00:41:42 | [diff] [blame] | 134 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 135 | virtual bool RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria, |
| 136 | RtpPacketSinkInterface* sink) = 0; |
| 137 | |
| 138 | virtual bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) = 0; |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 139 | |
| 140 | protected: |
| 141 | void SendReadyToSend(bool arg) { callback_list_ready_to_send_.Send(arg); } |
| 142 | void SendRtcpPacketReceived(rtc::CopyOnWriteBuffer* buffer, |
| 143 | int64_t packet_time_us) { |
| 144 | callback_list_rtcp_packet_received_.Send(buffer, packet_time_us); |
| 145 | } |
| 146 | void NotifyUnDemuxableRtpPacketReceived(RtpPacketReceived& packet) { |
| 147 | callback_undemuxable_rtp_packet_received_(packet); |
| 148 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 149 | void SendNetworkRouteChanged(std::optional<rtc::NetworkRoute> route) { |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 150 | callback_list_network_route_changed_.Send(route); |
| 151 | } |
| 152 | void SendWritableState(bool state) { |
| 153 | callback_list_writable_state_.Send(state); |
| 154 | } |
| 155 | void SendSentPacket(const rtc::SentPacket& packet) { |
| 156 | callback_list_sent_packet_.Send(packet); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | CallbackList<bool> callback_list_ready_to_send_; |
| 161 | CallbackList<rtc::CopyOnWriteBuffer*, int64_t> |
| 162 | callback_list_rtcp_packet_received_; |
Harald Alvestrand | a654437 | 2023-11-13 09:33:56 | [diff] [blame] | 163 | absl::AnyInvocable<void(RtpPacketReceived&)> |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 164 | callback_undemuxable_rtp_packet_received_ = |
| 165 | [](RtpPacketReceived& packet) {}; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 166 | CallbackList<std::optional<rtc::NetworkRoute>> |
Harald Alvestrand | ff281aa | 2023-09-05 09:49:32 | [diff] [blame] | 167 | callback_list_network_route_changed_; |
| 168 | CallbackList<bool> callback_list_writable_state_; |
| 169 | CallbackList<const rtc::SentPacket&> callback_list_sent_packet_; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // namespace webrtc |
| 173 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 174 | #endif // PC_RTP_TRANSPORT_INTERNAL_H_ |