blob: ca59afb806f603e0b516a640ca5e4365efc2a539 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCPACKETSOCKET_H_
12#define RTC_BASE_ASYNCPACKETSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/constructormagic.h"
15#include "rtc_base/dscp.h"
16#include "rtc_base/sigslot.h"
17#include "rtc_base/socket.h"
18#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020namespace rtc {
21
22// This structure holds the info needed to update the packet send time header
23// extension, including the information needed to update the authentication tag
24// after changing the value.
25struct PacketTimeUpdateParams {
26 PacketTimeUpdateParams();
Qingsi Wang6e641e62018-04-11 20:14:17 -070027 PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028 ~PacketTimeUpdateParams();
29
Qingsi Wang6e641e62018-04-11 20:14:17 -070030 int rtp_sendtime_extension_id = -1; // extension header id present in packet.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 std::vector<char> srtp_auth_key; // Authentication key.
Qingsi Wang6e641e62018-04-11 20:14:17 -070032 int srtp_auth_tag_len = -1; // Authentication tag length.
33 int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034};
35
36// This structure holds meta information for the packet which is about to send
37// over network.
38struct PacketOptions {
Qingsi Wang6e641e62018-04-11 20:14:17 -070039 PacketOptions();
40 explicit PacketOptions(DiffServCodePoint dscp);
41 PacketOptions(const PacketOptions& other);
42 ~PacketOptions();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
Qingsi Wang6e641e62018-04-11 20:14:17 -070044 DiffServCodePoint dscp = DSCP_NO_CHANGE;
Bjorn Mellem3a9c46d2018-04-25 13:24:48 -070045 // When used with RTP packets (for example, webrtc::PacketOptions), the value
46 // should be 16 bits. A value of -1 represents "not set".
47 int64_t packet_id = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048 PacketTimeUpdateParams packet_time_params;
Qingsi Wang6e641e62018-04-11 20:14:17 -070049 // PacketInfo is passed to SentPacket when signaling this packet is sent.
50 PacketInfo info_signaled_after_sent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051};
52
53// This structure will have the information about when packet is actually
54// received by socket.
55struct PacketTime {
56 PacketTime() : timestamp(-1), not_before(-1) {}
57 PacketTime(int64_t timestamp, int64_t not_before)
58 : timestamp(timestamp), not_before(not_before) {}
59
60 int64_t timestamp; // Receive time after socket delivers the data.
61
62 // Earliest possible time the data could have arrived, indicating the
63 // potential error in the |timestamp| value, in case the system, is busy. For
64 // example, the time of the last select() call.
65 // If unknown, this value will be set to zero.
66 int64_t not_before;
67};
68
69inline PacketTime CreatePacketTime(int64_t not_before) {
70 return PacketTime(TimeMicros(), not_before);
71}
72
73// Provides the ability to receive packets asynchronously. Sends are not
74// buffered since it is acceptable to drop packets under high load.
75class AsyncPacketSocket : public sigslot::has_slots<> {
76 public:
77 enum State {
78 STATE_CLOSED,
79 STATE_BINDING,
80 STATE_BOUND,
81 STATE_CONNECTING,
82 STATE_CONNECTED
83 };
84
85 AsyncPacketSocket();
86 ~AsyncPacketSocket() override;
87
88 // Returns current local address. Address may be set to null if the
89 // socket is not bound yet (GetState() returns STATE_BINDING).
90 virtual SocketAddress GetLocalAddress() const = 0;
91
92 // Returns remote address. Returns zeroes if this is not a client TCP socket.
93 virtual SocketAddress GetRemoteAddress() const = 0;
94
95 // Send a packet.
96 virtual int Send(const void *pv, size_t cb, const PacketOptions& options) = 0;
97 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
98 const PacketOptions& options) = 0;
99
100 // Close the socket.
101 virtual int Close() = 0;
102
103 // Returns current state of the socket.
104 virtual State GetState() const = 0;
105
106 // Get/set options.
107 virtual int GetOption(Socket::Option opt, int* value) = 0;
108 virtual int SetOption(Socket::Option opt, int value) = 0;
109
110 // Get/Set current error.
111 // TODO: Remove SetError().
112 virtual int GetError() const = 0;
113 virtual void SetError(int error) = 0;
114
115 // Emitted each time a packet is read. Used only for UDP and
116 // connected TCP sockets.
117 sigslot::signal5<AsyncPacketSocket*, const char*, size_t,
118 const SocketAddress&,
119 const PacketTime&> SignalReadPacket;
120
121 // Emitted each time a packet is sent.
122 sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
123
124 // Emitted when the socket is currently able to send.
125 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
126
127 // Emitted after address for the socket is allocated, i.e. binding
128 // is finished. State of the socket is changed from BINDING to BOUND
129 // (for UDP and server TCP sockets) or CONNECTING (for client TCP
130 // sockets).
131 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
132
133 // Emitted for client TCP sockets when state is changed from
134 // CONNECTING to CONNECTED.
135 sigslot::signal1<AsyncPacketSocket*> SignalConnect;
136
137 // Emitted for client TCP sockets when state is changed from
138 // CONNECTED to CLOSED.
139 sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
140
141 // Used only for listening TCP sockets.
142 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
143
144 private:
145 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
146};
147
Qingsi Wang6e641e62018-04-11 20:14:17 -0700148void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
149 const AsyncPacketSocket& socket_from,
Qingsi Wang4ea53b32018-04-16 18:22:31 -0700150 bool is_connectionless,
Qingsi Wang6e641e62018-04-11 20:14:17 -0700151 rtc::PacketInfo* info);
152
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200155#endif // RTC_BASE_ASYNCPACKETSOCKET_H_