summaryrefslogtreecommitdiffstats
path: root/src/dabOutput/dabOutput.h
blob: 4a528bd47463c2362866c7ebc5a501708668f750 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in
   Right of Canada (Communications Research Center Canada)

   Copyright (C) 2016
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://www.opendigitalradio.org

   An object-oriented version of the output channels.
*/
/*
   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/>.
   */

#ifndef __DAB_OUTPUT_H
#define __DAB_OUTPUT_H

#include "UdpSocket.h"
#include "Log.h"
#include "string.h"
#include <stdexcept>
#include <signal.h>
#include <vector>
#include <chrono>
#include <memory>

#include <unistd.h>
#include <sys/time.h>
#ifndef O_BINARY
# define O_BINARY 0
#endif // O_BINARY
#ifdef HAVE_OUTPUT_ZEROMQ
#  include "zmq.hpp"
#endif

/** Configuration for EDI output */

// Can represent both unicast and multicast destinations
struct edi_destination_t {
    std::string dest_addr;
    std::string source_addr;
    unsigned int source_port;
    unsigned int ttl;

    std::shared_ptr<UdpSocket> socket;
};

struct edi_configuration_t {
    edi_configuration_t() :
        verbose(false) {}
    unsigned chunk_len; // RSk, data length of each chunk
    unsigned fec;       // number of fragments that can be recovered
    bool dump;          // dump a file with the EDI packets
    bool verbose;
    bool enable_pft;    // Enable protection and fragmentation
    unsigned int tagpacket_alignment;
    std::vector<edi_destination_t> destinations;
    unsigned int dest_port; // common destination port, because it's encoded in the transport layer

    bool enabled() { return destinations.size() > 0; }
};


// Abstract base class for all outputs
class DabOutput
{
    public:
        virtual int Open(const char* name) = 0;
        int Open(std::string name)
        {
            return Open(name.c_str());
        }
        virtual int Write(void* buffer, int size) = 0;
        virtual int Close() = 0;

        virtual ~DabOutput() {}

        virtual std::string get_info() const = 0;
};

// ----- used in File and Fifo outputs
enum EtiFileType {
    ETI_FILE_TYPE_NONE = 0,
    ETI_FILE_TYPE_RAW,
    ETI_FILE_TYPE_STREAMED,
    ETI_FILE_TYPE_FRAMED
};

// ---------- File output ------------
class DabOutputFile : public DabOutput
{
    public:
        DabOutputFile() {
            nbFrames_ = 0;
            file_ = -1;
            type_ = ETI_FILE_TYPE_FRAMED;
        }

        DabOutputFile(const DabOutputFile& other)
        {
            filename_ = other.filename_;
            file_ = other.file_;
            nbFrames_ = other.nbFrames_;
            type_ = other.type_;
        }

        ~DabOutputFile() {}
        const DabOutputFile& operator=(const DabOutputFile& other) = delete;

        int Open(const char* filename);
        int Write(void* buffer, int size);
        int Close();

        std::string get_info() const {
            return "file://" + filename_;
        }

    protected:
        std::string filename_;
        int file_;
        EtiFileType type_;
        unsigned long nbFrames_;
};

// ---------- FIFO output ------------
// only write is different for the FIFO output
class DabOutputFifo : public DabOutputFile
{
    public:
        DabOutputFifo() : DabOutputFile() {}
        ~DabOutputFifo() {}

        int Write(void* buffer, int size);

        std::string get_info() const {
            return "fifo://" + filename_;
        }

};

// -------------- RAW socket -----------
class DabOutputRaw : public DabOutput
{
    public:
        DabOutputRaw()
        {
            socket_ = -1;
            isCyclades_ = false;
            buffer_ = new unsigned char[6144];
        }

        DabOutputRaw(const DabOutputRaw& other)
        {
            socket_ = other.socket_;
            isCyclades_ = other.isCyclades_;
            buffer_ = new unsigned char[6144];
            memcpy(buffer_, other.buffer_, 6144);
        }

        ~DabOutputRaw() {
            delete[] buffer_;
        }

        const DabOutputRaw operator=(const DabOutputRaw& other);

        int Open(const char* name);
        int Write(void* buffer, int size);
        int Close();

        std::string get_info() const {
            return "raw://" + filename_;
        }
    private:
        std::string filename_;
        int socket_;
        bool isCyclades_;
        unsigned char* buffer_;
};

// -------------- UDP ------------------
class DabOutputUdp : public DabOutput
{
    public:
        DabOutputUdp() {
            packet_ = new UdpPacket(6144);
            socket_ = new UdpSocket();
        }

        ~DabOutputUdp() {
            delete socket_;
            delete packet_;
        }

        int Open(const char* name);
        int Write(void* buffer, int size);
        int Close() { return 0; }

        std::string get_info() const {
            return "udp://" + uri_;
        }
    private:
        // make sure we don't copy this output around
        // the UdpPacket and UdpSocket do not support
        // copying either
        DabOutputUdp(const DabOutputUdp& other) = delete;
        DabOutputUdp operator=(const DabOutputUdp& other) = delete;

        std::string uri_;
        UdpSocket* socket_;
        UdpPacket* packet_;
};

// -------------- TCP ------------------
class TCPDataDispatcher;
class DabOutputTcp : public DabOutput
{
    public:
        DabOutputTcp() {}
        DabOutputTcp(const DabOutputTcp& other) = delete;
        const DabOutputTcp& operator=(const DabOutputTcp& other) = delete;
        ~DabOutputTcp();

        int Open(const char* name);
        int Write(void* buffer, int size);
        int Close();

        std::string get_info() const {
            return "tcp://" + uri_;
        }

    private:
        std::string uri_;

        TCPDataDispatcher* dispatcher_;
};

// -------------- Simul ------------------
class DabOutputSimul : public DabOutput
{
    public:
        DabOutputSimul() {}

        DabOutputSimul(const DabOutputSimul& other)
        {
            startTime_ = other.startTime_;
        }

        ~DabOutputSimul() { }

        int Open(const char* name);
        int Write(void* buffer, int size);
        int Close() { return 0; }

        std::string get_info() const {
            return "simul://" + name_;
        }
    private:
        std::string name_;
        std::chrono::steady_clock::time_point startTime_;
};

#if defined(HAVE_OUTPUT_ZEROMQ)

#define NUM_FRAMES_PER_ZMQ_MESSAGE 4
/* A concatenation of four ETI frames,
 * whose maximal size is 6144.
 *
 * If we transmit four frames in one zmq message,
 * we do not risk breaking ETI vs. transmission frame
 * phase.
 *
 * The frames are concatenated in buf, and
 * their sizes is given in the buflen array.
 *
 * Most of the time, the buf will not be completely
 * filled
 */
struct zmq_dab_message_t
{
    zmq_dab_message_t()
    {
        /* set buf lengths to invalid */
        buflen[0] = -1;
        buflen[1] = -1;
        buflen[2] = -1;
        buflen[3] = -1;

        version = 1;
    }
    uint32_t version;
    int16_t buflen[NUM_FRAMES_PER_ZMQ_MESSAGE];
    /* The head stops here. Use the macro below to calculate
     * the head size.
     */

    uint8_t  buf[NUM_FRAMES_PER_ZMQ_MESSAGE*6144];
};

#define ZMQ_DAB_MESSAGE_HEAD_LENGTH (4 + NUM_FRAMES_PER_ZMQ_MESSAGE*2)

// -------------- ZeroMQ message queue ------------------
class DabOutputZMQ : public DabOutput
{
    public:
        DabOutputZMQ() :
            endpoint_(""),
            zmq_proto_(""), zmq_context_(1),
            zmq_pub_sock_(zmq_context_, ZMQ_PUB),
            zmq_message_ix(0)
        {
            throw std::runtime_error("ERROR: No ZMQ protocol specified !");
        }

        DabOutputZMQ(std::string zmq_proto) :
            endpoint_(""),
            zmq_proto_(zmq_proto), zmq_context_(1),
            zmq_pub_sock_(zmq_context_, ZMQ_PUB),
            zmq_message_ix(0)
        { }


        ~DabOutputZMQ()
        {
            zmq_pub_sock_.close();
        }

        std::string get_info() const {
            return "zmq: " + zmq_proto_ + "://" + endpoint_;
        }

        int Open(const char* endpoint);
        int Write(void* buffer, int size);
        int Close();
    private:
        DabOutputZMQ(const DabOutputZMQ& other) :
            zmq_proto_(other.zmq_proto_), zmq_context_(1),
            zmq_pub_sock_(zmq_context_, ZMQ_PUB)
        {
            /* Forbid copy constructor */
        }

        std::string endpoint_;
        std::string zmq_proto_;
        zmq::context_t zmq_context_; // handle for the zmq context
        zmq::socket_t zmq_pub_sock_; // handle for the zmq publisher socket

        zmq_dab_message_t zmq_message;
        int zmq_message_ix;
};

#endif

#endif