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