Home | History | Annotate | Download | only in channel_transport
      1 /*
      2  *  Copyright (c) 2012 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 
     11 #ifndef WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
     12 #define WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
     13 
     14 #include "webrtc/test/channel_transport/udp_socket_wrapper.h"
     15 #include "webrtc/test/channel_transport/udp_transport.h"
     16 
     17 namespace webrtc {
     18 
     19 class CriticalSectionWrapper;
     20 class RWLockWrapper;
     21 
     22 namespace test {
     23 
     24 class UdpSocketManager;
     25 
     26 class UdpTransportImpl : public UdpTransport
     27 {
     28 public:
     29     // A factory that returns a wrapped UDP socket or equivalent.
     30     class SocketFactoryInterface {
     31     public:
     32         virtual ~SocketFactoryInterface() {}
     33         virtual UdpSocketWrapper* CreateSocket(const int32_t id,
     34                                                UdpSocketManager* mgr,
     35                                                CallbackObj obj,
     36                                                IncomingSocketCallback cb,
     37                                                bool ipV6Enable,
     38                                                bool disableGQOS) = 0;
     39     };
     40 
     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();
     48 
     49     // UdpTransport functions
     50     virtual int32_t InitializeSendSockets(
     51         const char* ipAddr,
     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],
     67         uint16_t& rtpPort,
     68         uint16_t& rtcpPort,
     69         char multicastIpAddr[kIpAddressVersion6Length]) const OVERRIDE;
     70     virtual int32_t SendSocketInformation(
     71         char ipAddr[kIpAddressVersion6Length],
     72         uint16_t& rtpPort,
     73         uint16_t& rtcpPort) const OVERRIDE;
     74     virtual int32_t RemoteSocketInformation(
     75         char ipAddr[kIpAddressVersion6Length],
     76         uint16_t& rtpPort,
     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,
    109                             uint16_t portnr = 0,
    110                             const char* ip = NULL) OVERRIDE;
    111     virtual int32_t SendRTPPacketTo(const int8_t *data,
    112                                     uint32_t length,
    113                                     const SocketAddress& to) OVERRIDE;
    114     virtual int32_t SendRTCPPacketTo(const int8_t *data,
    115                                      uint32_t length,
    116                                      const SocketAddress& to) OVERRIDE;
    117     virtual int32_t SendRTPPacketTo(const int8_t *data,
    118                                     uint32_t length,
    119                                     uint16_t rtpPort) OVERRIDE;
    120     virtual int32_t SendRTCPPacketTo(const int8_t *data,
    121                                      uint32_t length,
    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,
    126                                const void* data,
    127                                int length) OVERRIDE;
    128 
    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;
    133 
    134     virtual ErrorCode LastError() const OVERRIDE;
    135 
    136     virtual int32_t IPAddressCached(const SocketAddress& address,
    137                                     char* ip,
    138                                     uint32_t& ipSize,
    139                                     uint16_t& sourcePort) OVERRIDE;
    140 
    141     int32_t Id() const {return _id;}
    142 protected:
    143     // IncomingSocketCallback signature functions for receiving callbacks from
    144     // UdpSocketWrapper.
    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);
    153 
    154     void CloseSendSockets();
    155     void CloseReceiveSockets();
    156 
    157     // Update _remoteRTPAddr according to _destPort and _destIP
    158     void BuildRemoteRTPAddr();
    159     // Update _remoteRTCPAddr according to _destPortRTCP and _destIP
    160     void BuildRemoteRTCPAddr();
    161 
    162     void BuildSockaddrIn(uint16_t portnr, const char* ip,
    163                          SocketAddress& remoteAddr) const;
    164 
    165     ErrorCode BindLocalRTPSocket();
    166     ErrorCode BindLocalRTCPSocket();
    167 
    168     ErrorCode BindRTPSendSocket();
    169     ErrorCode BindRTCPSendSocket();
    170 
    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);
    177 
    178     bool FilterIPAddress(const SocketAddress* fromAddress);
    179 
    180     bool SetSockOptUsed();
    181 
    182     int32_t EnableQoS(int32_t serviceType, bool audio,
    183                       uint32_t maxBitrate, int32_t overrideDSCP);
    184 
    185     int32_t DisableQoS();
    186 
    187 private:
    188     void GetCachedAddress(char* ip, uint32_t& ipSize,
    189                           uint16_t& sourcePort);
    190 
    191     int32_t _id;
    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;
    200 
    201     // Remote RTP and RTCP ports.
    202     uint16_t _destPort;
    203     uint16_t _destPortRTCP;
    204 
    205     // Local RTP and RTCP ports.
    206     uint16_t _localPort;
    207     uint16_t _localPortRTCP;
    208 
    209     // Local port number when the local port for receiving and local port number
    210     // for sending are not the same.
    211     uint16_t _srcPort;
    212     uint16_t _srcPortRTCP;
    213 
    214     // Remote port from which last received packet was sent.
    215     uint16_t _fromPort;
    216     uint16_t _fromPortRTCP;
    217 
    218     char _fromIP[kIpAddressVersion6Length];
    219     char _destIP[kIpAddressVersion6Length];
    220     char _localIP[kIpAddressVersion6Length];
    221     char _localMulticastIP[kIpAddressVersion6Length];
    222 
    223     UdpSocketWrapper* _ptrRtpSocket;
    224     UdpSocketWrapper* _ptrRtcpSocket;
    225 
    226     // Local port when the local port for receiving and local port for sending
    227     // are not the same.
    228     UdpSocketWrapper* _ptrSendRtpSocket;
    229     UdpSocketWrapper* _ptrSendRtcpSocket;
    230 
    231     SocketAddress _remoteRTPAddr;
    232     SocketAddress _remoteRTCPAddr;
    233 
    234     SocketAddress _localRTPAddr;
    235     SocketAddress _localRTCPAddr;
    236 
    237     int32_t _tos;
    238     bool _receiving;
    239     bool _useSetSockOpt;
    240     bool _qos;
    241     int32_t _pcp;
    242     bool _ipV6Enabled;
    243     int32_t _serviceType;
    244     int32_t _overrideDSCP;
    245     uint32_t _maxBitrate;
    246 
    247     // Cache used by GetCachedAddress(..).
    248     RWLockWrapper* _cachLock;
    249     SocketAddress _previousAddress;
    250     char _previousIP[kIpAddressVersion6Length];
    251     uint32_t _previousIPSize;
    252     uint16_t _previousSourcePort;
    253 
    254     SocketAddress _filterIPAddress;
    255     uint16_t _rtpFilterPort;
    256     uint16_t _rtcpFilterPort;
    257 
    258     UdpTransportData* _packetCallback;
    259 };
    260 
    261 }  // namespace test
    262 }  // namespace webrtc
    263 
    264 #endif  // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
    265