zstein | d48dbda | 2017-04-05 02:45:57 | [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_H_ |
| 12 | #define PC_RTP_TRANSPORT_H_ |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 13 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Harald Alvestrand | 83894d3 | 2023-09-26 19:38:19 | [diff] [blame] | 20 | #include "api/task_queue/pending_task_safety_flag.h" |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 21 | #include "call/rtp_demuxer.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 22 | #include "call/video_receive_stream.h" |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 23 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 24 | #include "p2p/base/packet_transport_internal.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 25 | #include "pc/rtp_transport_internal.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 26 | #include "pc/session_description.h" |
| 27 | #include "rtc_base/async_packet_socket.h" |
| 28 | #include "rtc_base/copy_on_write_buffer.h" |
| 29 | #include "rtc_base/network/sent_packet.h" |
| 30 | #include "rtc_base/network_route.h" |
| 31 | #include "rtc_base/socket.h" |
zstein | d9ce764 | 2017-04-10 23:17:57 | [diff] [blame] | 32 | |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 33 | namespace rtc { |
| 34 | |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 35 | class CopyOnWriteBuffer; |
| 36 | struct PacketOptions; |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 37 | class PacketTransportInternal; |
| 38 | |
| 39 | } // namespace rtc |
| 40 | |
| 41 | namespace webrtc { |
| 42 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 43 | class RtpTransport : public RtpTransportInternal { |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 44 | public: |
| 45 | RtpTransport(const RtpTransport&) = delete; |
| 46 | RtpTransport& operator=(const RtpTransport&) = delete; |
| 47 | |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 48 | explicit RtpTransport(bool rtcp_mux_enabled) |
| 49 | : rtcp_mux_enabled_(rtcp_mux_enabled) {} |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 50 | |
Zhi Huang | f2d7beb | 2017-11-20 22:35:11 | [diff] [blame] | 51 | bool rtcp_mux_enabled() const override { return rtcp_mux_enabled_; } |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 52 | void SetRtcpMuxEnabled(bool enable) override; |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 53 | |
Bjorn A Mellem | 3a1b927 | 2019-05-24 23:13:08 | [diff] [blame] | 54 | const std::string& transport_name() const override; |
| 55 | |
| 56 | int SetRtpOption(rtc::Socket::Option opt, int value) override; |
| 57 | int SetRtcpOption(rtc::Socket::Option opt, int value) override; |
| 58 | |
| 59 | rtc::PacketTransportInternal* rtp_packet_transport() const { |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 60 | return rtp_packet_transport_; |
| 61 | } |
Bjorn A Mellem | 3a1b927 | 2019-05-24 23:13:08 | [diff] [blame] | 62 | void SetRtpPacketTransport(rtc::PacketTransportInternal* rtp); |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 63 | |
Bjorn A Mellem | 3a1b927 | 2019-05-24 23:13:08 | [diff] [blame] | 64 | rtc::PacketTransportInternal* rtcp_packet_transport() const { |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 65 | return rtcp_packet_transport_; |
| 66 | } |
Bjorn A Mellem | 3a1b927 | 2019-05-24 23:13:08 | [diff] [blame] | 67 | void SetRtcpPacketTransport(rtc::PacketTransportInternal* rtcp); |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 68 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 69 | bool IsReadyToSend() const override { return ready_to_send_; } |
| 70 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 71 | bool IsWritable(bool rtcp) const override; |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 72 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 73 | bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 74 | const rtc::PacketOptions& options, |
| 75 | int flags) override; |
| 76 | |
| 77 | bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 78 | const rtc::PacketOptions& options, |
| 79 | int flags) override; |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 80 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 81 | bool IsSrtpActive() const override { return false; } |
zstein | 3dcf0e9 | 2017-06-01 20:22:42 | [diff] [blame] | 82 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 83 | void UpdateRtpHeaderExtensionMap( |
| 84 | const cricket::RtpHeaderExtensions& header_extensions) override; |
| 85 | |
| 86 | bool RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria, |
| 87 | RtpPacketSinkInterface* sink) override; |
| 88 | |
| 89 | bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) override; |
| 90 | |
zstein | d9ce764 | 2017-04-10 23:17:57 | [diff] [blame] | 91 | protected: |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 92 | // These methods will be used in the subclasses. |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 93 | void DemuxPacket(rtc::CopyOnWriteBuffer packet, int64_t packet_time_us); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 | [diff] [blame] | 94 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 95 | bool SendPacket(bool rtcp, |
| 96 | rtc::CopyOnWriteBuffer* packet, |
| 97 | const rtc::PacketOptions& options, |
| 98 | int flags); |
Philipp Hancke | 977b56c | 2023-11-08 09:20:51 | [diff] [blame] | 99 | flat_set<uint32_t> GetSsrcsForSink(RtpPacketSinkInterface* sink); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 100 | |
| 101 | // Overridden by SrtpTransport. |
| 102 | virtual void OnNetworkRouteChanged( |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 103 | absl::optional<rtc::NetworkRoute> network_route); |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 104 | virtual void OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 105 | int64_t packet_time_us); |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 106 | virtual void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 107 | int64_t packet_time_us); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 108 | // Overridden by SrtpTransport and DtlsSrtpTransport. |
| 109 | virtual void OnWritableState(rtc::PacketTransportInternal* packet_transport); |
| 110 | |
| 111 | private: |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 112 | void OnReadyToSend(rtc::PacketTransportInternal* transport); |
Zhi Huang | cd3fc5d | 2017-11-29 18:41:57 | [diff] [blame] | 113 | void OnSentPacket(rtc::PacketTransportInternal* packet_transport, |
| 114 | const rtc::SentPacket& sent_packet); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 115 | void OnReadPacket(rtc::PacketTransportInternal* transport, |
| 116 | const char* data, |
| 117 | size_t len, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 118 | const int64_t& packet_time_us, |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 119 | int flags); |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 120 | |
| 121 | // Updates "ready to send" for an individual channel and fires |
| 122 | // SignalReadyToSend. |
| 123 | void SetReadyToSend(bool rtcp, bool ready); |
| 124 | |
| 125 | void MaybeSignalReadyToSend(); |
| 126 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 127 | bool IsTransportWritable(); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 | [diff] [blame] | 128 | |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 129 | bool rtcp_mux_enabled_; |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 130 | |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 131 | rtc::PacketTransportInternal* rtp_packet_transport_ = nullptr; |
| 132 | rtc::PacketTransportInternal* rtcp_packet_transport_ = nullptr; |
zstein | d9ce764 | 2017-04-10 23:17:57 | [diff] [blame] | 133 | |
zstein | 56162b9 | 2017-04-24 23:54:35 | [diff] [blame] | 134 | bool ready_to_send_ = false; |
| 135 | bool rtp_ready_to_send_ = false; |
| 136 | bool rtcp_ready_to_send_ = false; |
| 137 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 138 | RtpDemuxer rtp_demuxer_; |
| 139 | |
| 140 | // Used for identifying the MID for RtpDemuxer. |
| 141 | RtpHeaderExtensionMap header_extension_map_; |
Harald Alvestrand | 83894d3 | 2023-09-26 19:38:19 | [diff] [blame] | 142 | // Guard against recursive "ready to send" signals |
| 143 | bool processing_ready_to_send_ = false; |
Harald Alvestrand | 2931ddd | 2023-11-03 15:30:17 | [diff] [blame] | 144 | bool processing_sent_packet_ = false; |
Harald Alvestrand | 83894d3 | 2023-09-26 19:38:19 | [diff] [blame] | 145 | ScopedTaskSafety safety_; |
zstein | d48dbda | 2017-04-05 02:45:57 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // namespace webrtc |
| 149 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 150 | #endif // PC_RTP_TRANSPORT_H_ |