2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
12 #define WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
14 #include "webrtc/test/channel_transport/udp_socket_wrapper.h"
15 #include "webrtc/test/channel_transport/udp_transport.h"
19 class CriticalSectionWrapper;
24 class UdpSocketManager;
26 class UdpTransportImpl : public UdpTransport
29 // A factory that returns a wrapped UDP socket or equivalent.
30 class SocketFactoryInterface {
32 virtual ~SocketFactoryInterface() {}
33 virtual UdpSocketWrapper* CreateSocket(const int32_t id,
34 UdpSocketManager* mgr,
36 IncomingSocketCallback cb,
38 bool disableGQOS) = 0;
41 // Constructor, only called by UdpTransport::Create and tests.
42 // The constructor takes ownership of the "maker".
43 // The constructor does not take ownership of socket_manager.
44 UdpTransportImpl(const int32_t id,
45 SocketFactoryInterface* maker,
46 UdpSocketManager* socket_manager);
47 virtual ~UdpTransportImpl();
49 // UdpTransport functions
50 virtual int32_t InitializeSendSockets(
52 const uint16_t rtpPort,
53 const uint16_t rtcpPort = 0) OVERRIDE;
54 virtual int32_t InitializeReceiveSockets(
55 UdpTransportData* const packetCallback,
56 const uint16_t rtpPort,
57 const char* ipAddr = NULL,
58 const char* multicastIpAddr = NULL,
59 const uint16_t rtcpPort = 0) OVERRIDE;
60 virtual int32_t InitializeSourcePorts(
61 const uint16_t rtpPort,
62 const uint16_t rtcpPort = 0) OVERRIDE;
63 virtual int32_t SourcePorts(uint16_t& rtpPort,
64 uint16_t& rtcpPort) const OVERRIDE;
65 virtual int32_t ReceiveSocketInformation(
66 char ipAddr[kIpAddressVersion6Length],
69 char multicastIpAddr[kIpAddressVersion6Length]) const OVERRIDE;
70 virtual int32_t SendSocketInformation(
71 char ipAddr[kIpAddressVersion6Length],
73 uint16_t& rtcpPort) const OVERRIDE;
74 virtual int32_t RemoteSocketInformation(
75 char ipAddr[kIpAddressVersion6Length],
77 uint16_t& rtcpPort) const OVERRIDE;
78 virtual int32_t SetQoS(const bool QoS,
79 const int32_t serviceType,
80 const uint32_t maxBitrate = 0,
81 const int32_t overrideDSCP = 0,
82 const bool audio = false) OVERRIDE;
83 virtual int32_t QoS(bool& QoS, int32_t& serviceType,
84 int32_t& overrideDSCP) const OVERRIDE;
85 virtual int32_t SetToS(const int32_t DSCP,
86 const bool useSetSockOpt = false) OVERRIDE;
87 virtual int32_t ToS(int32_t& DSCP, bool& useSetSockOpt) const OVERRIDE;
88 virtual int32_t SetPCP(const int32_t PCP) OVERRIDE;
89 virtual int32_t PCP(int32_t& PCP) const OVERRIDE;
90 virtual int32_t EnableIpV6() OVERRIDE;
91 virtual bool IpV6Enabled() const OVERRIDE;
92 virtual int32_t SetFilterIP(
93 const char filterIPAddress[kIpAddressVersion6Length]) OVERRIDE;
94 virtual int32_t FilterIP(
95 char filterIPAddress[kIpAddressVersion6Length]) const OVERRIDE;
96 virtual int32_t SetFilterPorts(const uint16_t rtpFilterPort,
97 const uint16_t rtcpFilterPort) OVERRIDE;
98 virtual int32_t FilterPorts(uint16_t& rtpFilterPort,
99 uint16_t& rtcpFilterPort) const OVERRIDE;
100 virtual int32_t StartReceiving(
101 const uint32_t numberOfSocketBuffers) OVERRIDE;
102 virtual int32_t StopReceiving() OVERRIDE;
103 virtual bool Receiving() const OVERRIDE;
104 virtual bool SendSocketsInitialized() const OVERRIDE;
105 virtual bool SourcePortsInitialized() const OVERRIDE;
106 virtual bool ReceiveSocketsInitialized() const OVERRIDE;
107 virtual int32_t SendRaw(const int8_t* data,
108 uint32_t length, int32_t isRTCP,
110 const char* ip = NULL) OVERRIDE;
111 virtual int32_t SendRTPPacketTo(const int8_t *data,
113 const SocketAddress& to) OVERRIDE;
114 virtual int32_t SendRTCPPacketTo(const int8_t *data,
116 const SocketAddress& to) OVERRIDE;
117 virtual int32_t SendRTPPacketTo(const int8_t *data,
119 uint16_t rtpPort) OVERRIDE;
120 virtual int32_t SendRTCPPacketTo(const int8_t *data,
122 uint16_t rtcpPort) OVERRIDE;
123 // Transport functions
124 virtual int SendPacket(int channel, const void* data, int length) OVERRIDE;
125 virtual int SendRTCPPacket(int channel,
127 int length) OVERRIDE;
129 // UdpTransport functions continue.
130 virtual int32_t SetSendIP(const char* ipaddr) OVERRIDE;
131 virtual int32_t SetSendPorts(const uint16_t rtpPort,
132 const uint16_t rtcpPort = 0) OVERRIDE;
134 virtual ErrorCode LastError() const OVERRIDE;
136 virtual int32_t IPAddressCached(const SocketAddress& address,
139 uint16_t& sourcePort) OVERRIDE;
141 int32_t Id() const {return _id;}
143 // IncomingSocketCallback signature functions for receiving callbacks from
145 static void IncomingRTPCallback(CallbackObj obj,
146 const int8_t* rtpPacket,
147 int32_t rtpPacketLength,
148 const SocketAddress* from);
149 static void IncomingRTCPCallback(CallbackObj obj,
150 const int8_t* rtcpPacket,
151 int32_t rtcpPacketLength,
152 const SocketAddress* from);
154 void CloseSendSockets();
155 void CloseReceiveSockets();
157 // Update _remoteRTPAddr according to _destPort and _destIP
158 void BuildRemoteRTPAddr();
159 // Update _remoteRTCPAddr according to _destPortRTCP and _destIP
160 void BuildRemoteRTCPAddr();
162 void BuildSockaddrIn(uint16_t portnr, const char* ip,
163 SocketAddress& remoteAddr) const;
165 ErrorCode BindLocalRTPSocket();
166 ErrorCode BindLocalRTCPSocket();
168 ErrorCode BindRTPSendSocket();
169 ErrorCode BindRTCPSendSocket();
171 void IncomingRTPFunction(const int8_t* rtpPacket,
172 int32_t rtpPacketLength,
173 const SocketAddress* from);
174 void IncomingRTCPFunction(const int8_t* rtcpPacket,
175 int32_t rtcpPacketLength,
176 const SocketAddress* from);
178 bool FilterIPAddress(const SocketAddress* fromAddress);
180 bool SetSockOptUsed();
182 int32_t EnableQoS(int32_t serviceType, bool audio,
183 uint32_t maxBitrate, int32_t overrideDSCP);
185 int32_t DisableQoS();
188 void GetCachedAddress(char* ip, uint32_t& ipSize,
189 uint16_t& sourcePort);
192 SocketFactoryInterface* _socket_creator;
193 // Protects the sockets from being re-configured while receiving packets.
194 CriticalSectionWrapper* _crit;
195 CriticalSectionWrapper* _critFilter;
196 // _packetCallback's critical section.
197 CriticalSectionWrapper* _critPacketCallback;
198 UdpSocketManager* _mgr;
199 ErrorCode _lastError;
201 // Remote RTP and RTCP ports.
203 uint16_t _destPortRTCP;
205 // Local RTP and RTCP ports.
207 uint16_t _localPortRTCP;
209 // Local port number when the local port for receiving and local port number
210 // for sending are not the same.
212 uint16_t _srcPortRTCP;
214 // Remote port from which last received packet was sent.
216 uint16_t _fromPortRTCP;
218 char _fromIP[kIpAddressVersion6Length];
219 char _destIP[kIpAddressVersion6Length];
220 char _localIP[kIpAddressVersion6Length];
221 char _localMulticastIP[kIpAddressVersion6Length];
223 UdpSocketWrapper* _ptrRtpSocket;
224 UdpSocketWrapper* _ptrRtcpSocket;
226 // Local port when the local port for receiving and local port for sending
228 UdpSocketWrapper* _ptrSendRtpSocket;
229 UdpSocketWrapper* _ptrSendRtcpSocket;
231 SocketAddress _remoteRTPAddr;
232 SocketAddress _remoteRTCPAddr;
234 SocketAddress _localRTPAddr;
235 SocketAddress _localRTCPAddr;
243 int32_t _serviceType;
244 int32_t _overrideDSCP;
245 uint32_t _maxBitrate;
247 // Cache used by GetCachedAddress(..).
248 RWLockWrapper* _cachLock;
249 SocketAddress _previousAddress;
250 char _previousIP[kIpAddressVersion6Length];
251 uint32_t _previousIPSize;
252 uint16_t _previousSourcePort;
254 SocketAddress _filterIPAddress;
255 uint16_t _rtpFilterPort;
256 uint16_t _rtcpFilterPort;
258 UdpTransportData* _packetCallback;
262 } // namespace webrtc
264 #endif // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_