summaryrefslogtreecommitdiffstats
path: root/src/dabOutput/edi/Transport.cpp
blob: d4332397dff06a9afefc14de3863a7f4297280e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
   Copyright (C) 2019
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://www.opendigitalradio.org

   EDI output,
   UDP and TCP transports and their configuration

   */
/*
   This file is part of ODR-DabMux.

   ODR-DabMux 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.

   ODR-DabMux 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 ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "Transport.h"
#include <iterator>

using namespace std;

namespace edi {

void configuration_t::print() const
{
    etiLog.level(info) << "EDI";
    etiLog.level(info) << " verbose     " << verbose;
    for (auto edi_dest : destinations) {
        if (auto udp_dest = dynamic_pointer_cast<edi::udp_destination_t>(edi_dest)) {
            etiLog.level(info) << " to " << udp_dest->dest_addr << ":" << dest_port;
            if (not udp_dest->source_addr.empty()) {
                etiLog.level(info) << "  source      " << udp_dest->source_addr;
                etiLog.level(info) << "  ttl         " << udp_dest->ttl;
            }
            etiLog.level(info) << "  source port " << udp_dest->source_port;
        }
        else {
            throw std::logic_error("EDI destination not implemented");
        }
    }
    if (interleaver_enabled()) {
        etiLog.level(info) << " interleave     " << latency_frames * 24 << " ms";
    }
}


Sender::Sender(const configuration_t& conf) :
    m_conf(conf),
    edi_pft(m_conf)
{
    if (m_conf.verbose) {
        etiLog.log(info, "Setup EDI");
    }

    for (const auto& edi_dest : m_conf.destinations) {
        if (const auto udp_dest = dynamic_pointer_cast<edi::udp_destination_t>(edi_dest)) {
            auto udp_socket = std::make_shared<UdpSocket>(udp_dest->source_port);

            if (not udp_dest->source_addr.empty()) {
                int err = udp_socket->setMulticastSource(udp_dest->source_addr.c_str());
                if (err) {
                    throw runtime_error("EDI socket set source failed!");
                }
                err = udp_socket->setMulticastTTL(udp_dest->ttl);
                if (err) {
                    throw runtime_error("EDI socket set TTL failed!");
                }
            }

            udp_sockets.emplace(udp_dest.get(), udp_socket);
        }
    }

    if (m_conf.interleaver_enabled()) {
        edi_interleaver.SetLatency(m_conf.latency_frames);
    }

    if (m_conf.dump) {
        edi_debug_file.open("./edi.debug");
    }

    if (m_conf.verbose) {
        etiLog.log(info, "EDI set up");
    }
}

void Sender::write(const TagPacket& tagpacket)
{
    // Assemble into one AF Packet
    edi::AFPacket af_packet = edi_afPacketiser.Assemble(tagpacket);

    if (m_conf.enable_pft) {
        // Apply PFT layer to AF Packet (Reed Solomon FEC and Fragmentation)
        vector<edi::PFTFragment> edi_fragments = edi_pft.Assemble(af_packet);

        if (m_conf.verbose) {
            fprintf(stderr, "EDI number of PFT fragment before interleaver %zu",
                    edi_fragments.size());
        }

        if (m_conf.interleaver_enabled()) {
            edi_fragments = edi_interleaver.Interleave(edi_fragments);
        }

        // Send over ethernet
        for (const auto& edi_frag : edi_fragments) {
            for (auto& dest : m_conf.destinations) {
                if (const auto& udp_dest = dynamic_pointer_cast<edi::udp_destination_t>(dest)) {
                    InetAddress addr;
                    addr.setAddress(udp_dest->dest_addr.c_str());
                    addr.setPort(m_conf.dest_port);

                    udp_sockets.at(udp_dest.get())->send(edi_frag, addr);
                }
                else {
                    throw std::logic_error("EDI destination not implemented");
                }
            }

            if (m_conf.dump) {
                std::ostream_iterator<uint8_t> debug_iterator(edi_debug_file);
                std::copy(edi_frag.begin(), edi_frag.end(), debug_iterator);
            }
        }

        if (m_conf.verbose) {
            fprintf(stderr, "EDI number of PFT fragments %zu",
                    edi_fragments.size());
        }
    }
    else {
        // Send over ethernet
        for (auto& dest : m_conf.destinations) {
            if (const auto& udp_dest = dynamic_pointer_cast<edi::udp_destination_t>(dest)) {
                InetAddress addr;
                addr.setAddress(udp_dest->dest_addr.c_str());
                addr.setPort(m_conf.dest_port);

                udp_sockets.at(udp_dest.get())->send(af_packet, addr);
            }
            else {
                throw std::logic_error("EDI destination not implemented");
            }
        }

        if (m_conf.dump) {
            std::ostream_iterator<uint8_t> debug_iterator(edi_debug_file);
            std::copy(af_packet.begin(), af_packet.end(), debug_iterator);
        }
    }
}

}