sip_transport_ice.h 2.62 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2017 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#pragma once

23 24
#include "ip_utils.h"

25 26
#include <pjsip.h>
#include <pj/pool.h>
27 28

#include <functional>
29
#include <memory>
30
#include <type_traits> // std::is_standard_layout
31

Adrien Béraud's avatar
Adrien Béraud committed
32
namespace ring {
Guillaume Roguez's avatar
Guillaume Roguez committed
33

34 35 36 37
class IceTransport;

struct SipIceTransport
{
38 39 40 41 42 43 44 45 46 47 48
        // This structure SHOULD be standard-layout,
        // implies std::is_standard_layout<TransportData>::value
        // SHOULD return true!
        using TransportData = struct {
                pjsip_transport base; // do not move, SHOULD be the fist member
                SipIceTransport* self {nullptr};
        };
        static_assert(std::is_standard_layout<TransportData>::value,
                      "TranportData requires standard-layout");


49
        SipIceTransport(pjsip_endpoint* endpt, pj_pool_t& pool, long t_type,
Guillaume Roguez's avatar
Guillaume Roguez committed
50
                        const std::shared_ptr<IceTransport>& ice,
51
                        int comp_id);
52 53 54
        ~SipIceTransport();

        /**
55
         * To be called on a regular basis to receive packets
56
         */
57
        void loop();
58

59 60
        IpAddr getLocalAddress() const;

Guillaume Roguez's avatar
Guillaume Roguez committed
61
        std::shared_ptr<IceTransport> getIceTransport() const {
62 63 64
            return ice_;
        }

65 66 67
        pjsip_transport* getTransportBase() {
            return &trData_.base;
        }
68 69 70 71

    private:
        std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> pool_;
        std::unique_ptr<pj_pool_t, decltype(pj_pool_release)&> rxPool_;
72

73 74 75
        TransportData trData_;

        pjsip_rx_data rdata_;
76
        bool is_registered_ {false};
Guillaume Roguez's avatar
Guillaume Roguez committed
77
        const std::shared_ptr<IceTransport> ice_;
78 79 80 81 82 83
        const int comp_id_;

        pj_status_t send(pjsip_tx_data *tdata, const pj_sockaddr_t *rem_addr,
                         int addr_len, void *token,
                         pjsip_transport_callback callback);

84
        void onRecv();
85
};
Guillaume Roguez's avatar
Guillaume Roguez committed
86 87

} // namespace ring