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     int32_t InitializeSendSockets(const char* ipAddr,
     51                                   const uint16_t rtpPort,
     52                                   const uint16_t rtcpPort = 0) override;
     53     int32_t InitializeReceiveSockets(UdpTransportData* const packetCallback,
     54                                      const uint16_t rtpPort,
     55                                      const char* ipAddr = NULL,
     56                                      const char* multicastIpAddr = NULL,
     57                                      const uint16_t rtcpPort = 0) override;
     58     int32_t InitializeSourcePorts(const uint16_t rtpPort,
     59                                   const uint16_t rtcpPort = 0) override;
     60     int32_t SourcePorts(uint16_t& rtpPort, uint16_t& rtcpPort) const override;
     61     int32_t ReceiveSocketInformation(
     62         char ipAddr[kIpAddressVersion6Length],
     63         uint16_t& rtpPort,
     64         uint16_t& rtcpPort,
     65         char multicastIpAddr[kIpAddressVersion6Length]) const override;
     66     int32_t SendSocketInformation(char ipAddr[kIpAddressVersion6Length],
     67                                   uint16_t& rtpPort,
     68                                   uint16_t& rtcpPort) const override;
     69     int32_t RemoteSocketInformation(char ipAddr[kIpAddressVersion6Length],
     70                                     uint16_t& rtpPort,
     71                                     uint16_t& rtcpPort) const override;
     72     int32_t SetQoS(const bool QoS,
     73                    const int32_t serviceType,
     74                    const uint32_t maxBitrate = 0,
     75                    const int32_t overrideDSCP = 0,
     76                    const bool audio = false) override;
     77     int32_t QoS(bool& QoS,
     78                 int32_t& serviceType,
     79                 int32_t& overrideDSCP) const override;
     80     int32_t SetToS(const int32_t DSCP,
     81                    const bool useSetSockOpt = false) override;
     82     int32_t ToS(int32_t& DSCP, bool& useSetSockOpt) const override;
     83     int32_t SetPCP(const int32_t PCP) override;
     84     int32_t PCP(int32_t& PCP) const override;
     85     int32_t EnableIpV6() override;
     86     bool IpV6Enabled() const override;
     87     int32_t SetFilterIP(
     88         const char filterIPAddress[kIpAddressVersion6Length]) override;
     89     int32_t FilterIP(
     90         char filterIPAddress[kIpAddressVersion6Length]) const override;
     91     int32_t SetFilterPorts(const uint16_t rtpFilterPort,
     92                            const uint16_t rtcpFilterPort) override;
     93     int32_t FilterPorts(uint16_t& rtpFilterPort,
     94                         uint16_t& rtcpFilterPort) const override;
     95     int32_t StartReceiving(const uint32_t numberOfSocketBuffers) override;
     96     int32_t StopReceiving() override;
     97     bool Receiving() const override;
     98     bool SendSocketsInitialized() const override;
     99     bool SourcePortsInitialized() const override;
    100     bool ReceiveSocketsInitialized() const override;
    101     int32_t SendRaw(const int8_t* data,
    102                     size_t length,
    103                     int32_t isRTCP,
    104                     uint16_t portnr = 0,
    105                     const char* ip = NULL) override;
    106     int32_t SendRTPPacketTo(const int8_t* data,
    107                             size_t length,
    108                             const SocketAddress& to) override;
    109     int32_t SendRTCPPacketTo(const int8_t* data,
    110                              size_t length,
    111                              const SocketAddress& to) override;
    112     int32_t SendRTPPacketTo(const int8_t* data,
    113                             size_t length,
    114                             uint16_t rtpPort) override;
    115     int32_t SendRTCPPacketTo(const int8_t* data,
    116                              size_t length,
    117                              uint16_t rtcpPort) override;
    118     // Transport functions
    119     bool SendRtp(const uint8_t* data,
    120                  size_t length,
    121                  const PacketOptions& packet_options) override;
    122     bool SendRtcp(const uint8_t* data, size_t length) override;
    123 
    124     // UdpTransport functions continue.
    125     int32_t SetSendIP(const char* ipaddr) override;
    126     int32_t SetSendPorts(const uint16_t rtpPort,
    127                          const uint16_t rtcpPort = 0) override;
    128 
    129     ErrorCode LastError() const override;
    130 
    131     int32_t IPAddressCached(const SocketAddress& address,
    132                             char* ip,
    133                             uint32_t& ipSize,
    134                             uint16_t& sourcePort) override;
    135 
    136     int32_t Id() const {return _id;}
    137 protected:
    138     // IncomingSocketCallback signature functions for receiving callbacks from
    139     // UdpSocketWrapper.
    140     static void IncomingRTPCallback(CallbackObj obj,
    141                                     const int8_t* rtpPacket,
    142                                     size_t rtpPacketLength,
    143                                     const SocketAddress* from);
    144     static void IncomingRTCPCallback(CallbackObj obj,
    145                                      const int8_t* rtcpPacket,
    146                                      size_t rtcpPacketLength,
    147                                      const SocketAddress* from);
    148 
    149     void CloseSendSockets();
    150     void CloseReceiveSockets();
    151 
    152     // Update _remoteRTPAddr according to _destPort and _destIP
    153     void BuildRemoteRTPAddr();
    154     // Update _remoteRTCPAddr according to _destPortRTCP and _destIP
    155     void BuildRemoteRTCPAddr();
    156 
    157     void BuildSockaddrIn(uint16_t portnr, const char* ip,
    158                          SocketAddress& remoteAddr) const;
    159 
    160     ErrorCode BindLocalRTPSocket();
    161     ErrorCode BindLocalRTCPSocket();
    162 
    163     ErrorCode BindRTPSendSocket();
    164     ErrorCode BindRTCPSendSocket();
    165 
    166     void IncomingRTPFunction(const int8_t* rtpPacket,
    167                              size_t rtpPacketLength,
    168                              const SocketAddress* from);
    169     void IncomingRTCPFunction(const int8_t* rtcpPacket,
    170                               size_t rtcpPacketLength,
    171                               const SocketAddress* from);
    172 
    173     bool FilterIPAddress(const SocketAddress* fromAddress);
    174 
    175     bool SetSockOptUsed();
    176 
    177     int32_t EnableQoS(int32_t serviceType, bool audio,
    178                       uint32_t maxBitrate, int32_t overrideDSCP);
    179 
    180     int32_t DisableQoS();
    181 
    182 private:
    183     void GetCachedAddress(char* ip, uint32_t& ipSize,
    184                           uint16_t& sourcePort);
    185 
    186     int32_t _id;
    187     SocketFactoryInterface* _socket_creator;
    188     // Protects the sockets from being re-configured while receiving packets.
    189     CriticalSectionWrapper* _crit;
    190     CriticalSectionWrapper* _critFilter;
    191     // _packetCallback's critical section.
    192     CriticalSectionWrapper* _critPacketCallback;
    193     UdpSocketManager* _mgr;
    194     ErrorCode _lastError;
    195 
    196     // Remote RTP and RTCP ports.
    197     uint16_t _destPort;
    198     uint16_t _destPortRTCP;
    199 
    200     // Local RTP and RTCP ports.
    201     uint16_t _localPort;
    202     uint16_t _localPortRTCP;
    203 
    204     // Local port number when the local port for receiving and local port number
    205     // for sending are not the same.
    206     uint16_t _srcPort;
    207     uint16_t _srcPortRTCP;
    208 
    209     // Remote port from which last received packet was sent.
    210     uint16_t _fromPort;
    211     uint16_t _fromPortRTCP;
    212 
    213     char _fromIP[kIpAddressVersion6Length];
    214     char _destIP[kIpAddressVersion6Length];
    215     char _localIP[kIpAddressVersion6Length];
    216     char _localMulticastIP[kIpAddressVersion6Length];
    217 
    218     UdpSocketWrapper* _ptrRtpSocket;
    219     UdpSocketWrapper* _ptrRtcpSocket;
    220 
    221     // Local port when the local port for receiving and local port for sending
    222     // are not the same.
    223     UdpSocketWrapper* _ptrSendRtpSocket;
    224     UdpSocketWrapper* _ptrSendRtcpSocket;
    225 
    226     SocketAddress _remoteRTPAddr;
    227     SocketAddress _remoteRTCPAddr;
    228 
    229     SocketAddress _localRTPAddr;
    230     SocketAddress _localRTCPAddr;
    231 
    232     int32_t _tos;
    233     bool _receiving;
    234     bool _useSetSockOpt;
    235     bool _qos;
    236     int32_t _pcp;
    237     bool _ipV6Enabled;
    238     int32_t _serviceType;
    239     int32_t _overrideDSCP;
    240     uint32_t _maxBitrate;
    241 
    242     // Cache used by GetCachedAddress(..).
    243     RWLockWrapper* _cachLock;
    244     SocketAddress _previousAddress;
    245     char _previousIP[kIpAddressVersion6Length];
    246     uint32_t _previousIPSize;
    247     uint16_t _previousSourcePort;
    248 
    249     SocketAddress _filterIPAddress;
    250     uint16_t _rtpFilterPort;
    251     uint16_t _rtcpFilterPort;
    252 
    253     UdpTransportData* _packetCallback;
    254 };
    255 
    256 }  // namespace test
    257 }  // namespace webrtc
    258 
    259 #endif  // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_TRANSPORT_IMPL_H_
    260