summaryrefslogtreecommitdiffstats
path: root/src/utils.cpp
blob: 071c0e9634dd015cd14302d4db1b2d9636aa7b08 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
   2011, 2012 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)

   Copyright (C) 2013, 2014, 2015 Matthias P. Braendli
   http://mpb.li
*/
/*
   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 <cstring>
#include <iostream>
#include <memory>
#include <boost/algorithm/string/join.hpp>
#include "DabMux.h"
#include "utils.h"

using namespace std;

time_t getDabTime()
{
    static time_t oldTime = 0;
    static int offset = 0;
    if (oldTime == 0) {
        oldTime = time(NULL);
    } else {
        offset+= 24;
        if (offset >= 1000) {
            offset -= 1000;
            ++oldTime;
        }
    }
    return oldTime;
}


/* We use fprintf here because this doesn't have
 * to go to the log.
 * But all information below must go into the log.
 *
 * Do not use \n in the log, it messes presentation and logging
 * up.
 */

void header_message()
{
    fprintf(stderr,
            "Welcome to %s %s, compiled at %s, %s",
            PACKAGE_NAME,
#if defined(GITVERSION)
            GITVERSION,
#else
            PACKAGE_VERSION,
#endif
            __DATE__, __TIME__);
    fprintf(stderr, "\n\n");
    fprintf(stderr,
            "Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012\n");
    fprintf(stderr,
            "Her Majesty the Queen in Right of Canada\n");
    fprintf(stderr,
            "(Communications Research Centre Canada) All rights reserved.\n\n");
    fprintf(stderr,
            "Copyright (C) 2013, 2014, 2015 Matthias P. Braendli\n");
    fprintf(stderr,
            "http://opendigitalradio.org\n\n");

    std::cerr << "Input URLs supported:" << std::endl <<
#if defined(HAVE_INPUT_PRBS)
    " prbs" <<
#endif
#if defined(HAVE_INPUT_TEST)
    " test" <<
#endif
#if defined(HAVE_INPUT_SLIP)
    " slip" <<
#endif
#if defined(HAVE_INPUT_UDP)
    " udp" <<
#endif
#if defined(HAVE_INPUT_FIFO)
    " fifo" <<
#endif
#if defined(HAVE_INPUT_FILE)
    " file" <<
#endif
#if defined(HAVE_INPUT_ZEROMQ)
    " zmq" <<
#endif
    std::endl;

    std::cerr << "Inputs format supported:" << std::endl <<
#if defined(HAVE_FORMAT_RAW)
    " raw" <<
#endif
#if defined(HAVE_FORMAT_BRIDGE)
    " bridge" <<
#endif
#if defined(HAVE_FORMAT_MPEG)
    " mpeg" <<
#endif
#if defined(HAVE_FORMAT_PACKET)
    " packet" <<
#endif
#if defined(HAVE_FORMAT_DMB)
    " dmb" <<
#endif
#if defined(HAVE_FORMAT_EPM)
    " epm" <<
#endif
    std::endl;

    std::cerr << "Output URLs supported:" << std::endl <<
#if defined(HAVE_OUTPUT_FILE)
    " file" <<
#endif
#if defined(HAVE_OUTPUT_FIFO)
    " fifo" <<
#endif
#if defined(HAVE_OUTPUT_UDP)
    " udp" <<
#endif
#if defined(HAVE_OUTPUT_TCP)
    " tcp" <<
#endif
#if defined(HAVE_OUTPUT_RAW)
    " raw" <<
#endif
#if defined(HAVE_OUTPUT_SIMUL)
    " simul" <<
#endif
    std::endl << std::endl;

}

#if ENABLE_CMDLINE_OPTIONS
void printUsage(char *name, FILE* out)
{
    fprintf(out, "NAME\n");
    fprintf(out, "  %s - A software DAB multiplexer\n", name);
    fprintf(out, "\nSYNOPSYS\n");
    fprintf(out, "    You can use either a configuration file, or the command line arguments\n");
    fprintf(out, "    With external configuration file:\n");
    fprintf(out, "  %s [-e] configuration.mux\n", name);
    fprintf(out, "    See doc/example.config for an example format for the configuration file\n");
    fprintf(out, "    With command line arguments:\n");
    fprintf(out, "  %s"
            " [ensemble]"
            " [subchannel1 subchannel2 ...]"
            " [service1 component1 [component2 ...] service2 ...]"
            " [output1 ...]"
            " [-h]"
            " [-m mode]"
            " [-n nbFrames]"
            " [-o]"
            " [-s]"
            " [-V]"
            " [-z]"
            "\n",
            name);
    fprintf(out, "\n  Where ensemble ="
            " [-i ensembleId]"
            " [-L label]"
            " [-l sLabel]"
            "\n");
    fprintf(out, "\n  Where subchannel ="
            " -(A | B | D | E | F | M | P | T) inputName"
            " [-b bitrate]"
            " [-i subchannelId]"
            " [-k]"
            " [-p protection]"
            "\n");
    fprintf(out, "\n  Where service ="
            " -S"
            " [-g language]"
            " [-i serviceId]"
            " [-L label]"
            " [-l sLabel]"
            " [-y PTy]"
            "\n");
    fprintf(out, "\n  Where component ="
            " -C"
            " [-a address]"
            " [-d]"
            " [-f figType]"
            " [-i subchannelId]"
            " [-L label]"
            " [-l sLabel]"
            " [-t type]"
            "\n");
    fprintf(out, "\nDESCRIPTION\n");
    fprintf(out,
            "  %s  is a software multiplexer that generates an ETI stream from\n"
            "  audio and data streams. Because of  its  software  based  architecture,\n"
            "  many  typical DAB services can be generated and multiplexed on a single\n"
            "  PC platform with live or pre-recorded sources.\n"
            "\n"
            "  A DAB multiplex configuration is composed of one ensemble. An  ensemble\n"
            "  is  the entity that receivers tune to and process. An ensemble contains\n"
            "  several services. A service is  the  listener-selectable  output.  Each\n"
            "  service  contains  one mandatory service component which is called pri-\n"
            "  mary component. An audio primary component  define  a  program  service\n"
            "  while  a data primary component define a data service. Service can con-\n"
            "  tain additional components which are called secondary components. Maxi-\n"
            "  mum  total  number  of components is 12 for program services and 11 for\n"
            "  data services. A service component is a link to one subchannel (of Fast\n"
            "  Information  Data  Channel).  A  subchannel  is the physical space used\n"
            "  within the common interleaved frame.\n"
            "\n"
            "   __________________________________________________\n"
            "  |                     Ensemble                     |  ENSEMBLE\n"
            "  |__________________________________________________|\n"
            "          |                 |                 |\n"
            "          |                 |                 |\n"
            "   _______V______    _______V______    _______V______\n"
            "  |   Service 1  |  |   Service 2  |  |   Service 3  |  SERVICES\n"
            "  |______________|  |______________|  |______________|\n"
            "     |        |        |        | |______         |\n"
            "     |        |        |        |        |        |\n"
            "   __V__    __V__    __V__    __V__    __V__    __V__\n"
            "  | SC1 |  | SC2 |  | SC3 |  | SC4 |  | SC5 |  | SC6 |  SERVICE\n"
            "  |_____|  |_____|  |_____|  |_____|  |_____|  |_____|  COMPONENTS\n"
            "     |        |   _____|        |        |    ____|\n"
            "     |        |  |              |        |   |\n"
            "   __V________V__V______________V________V___V_______   COMMON\n"
            "  | SubCh1 | SubCh9 |  ...  | SubCh3 | SubCh60 | ... |  INTERLEAVED\n"
            "  |________|________|_______|________|_________|_____|  FRAME\n"
            "  Figure 1: An example of a DAB multiplex configuration\n",
        name);

    fprintf(out, "\nGENERAL OPTIONS\n");
    fprintf(out, "  -h                   : get this help\n");
    fprintf(out, "  -m                   : DAB mode (default: 2)\n");
    fprintf(out, "  -n nbFrames          : number of frames to produce\n");
    fprintf(out, "  -o                   : enable logging to syslog\n");
    fprintf(out, "  -r                   : throttle the output rate to one ETI frame every 24ms\n");
    fprintf(out, "  -V                   : print version information and "
            "exit\n");
    fprintf(out, "  -z                   : write SCCA field for Factum ETI"
            " analyzer\n");
    fprintf(out, "\nINPUT OPTIONS\n");
    fprintf(out, "  -A                   : set audio service\n");
    fprintf(out, "  -B                   : set CRC-Bridge data service\n");
    fprintf(out, "  -D                   : set data service\n");
    fprintf(out, "  -E                   : set enhanced packet service\n");
    fprintf(out, "  -F                   : set DAB+ service\n");
    fprintf(out, "  -M                   : set DMB service\n");
    fprintf(out, "  -P                   : set packet service\n");
    fprintf(out, "  -T                   : set test service\n");
    fprintf(out, "  inputName<n>         : name of file for audio and "
            "packet service and Udp input address for data service "
            "([address]:port)\n");
    fprintf(out, "  -a                   : packet address (default: 0x%x "
            "(%i))\n", DEFAULT_PACKET_ADDRESS, DEFAULT_PACKET_ADDRESS);
    fprintf(out, "  -b bitrate<n>        : bitrate (in kbits/s) of the "
            "subchannel (default: audio 1st frame, data %i, packet %i)\n",
            DEFAULT_DATA_BITRATE, DEFAULT_PACKET_BITRATE);
    fprintf(out, "  -c                   : set the extendend country code ECC "
            "(default: %u (0x%2x)\n", DEFAULT_ENSEMBLE_ECC, DEFAULT_ENSEMBLE_ECC);
    fprintf(out, "  -d                   : turn on datagroups in packet "
            "mode\n");
    fprintf(out, "  -f figType           : user application type in FIG "
            "0/13 for packet mode\n");
    fprintf(out, "  -g language          : Primary service component "
            "language: english=9, french=15\n");
    fprintf(out, "  -i id<n>             : service|subchannel|"
            "serviceComponent id <n> (default: <n>)\n");
    fprintf(out, "  -k                   : set non-blocking file input "
            "(audio and packet only)\n");
    fprintf(out, "  -L label<n>          : label of service <n>"
            " (default: CRC-Audio<n>)\n");
    fprintf(out, "  -l sLabel<n>         : short label flag of service <n>"
            " (default: 0xf040)\n");
    fprintf(out, "  -p protection<n>     : protection level (default: 3)\n");
    fprintf(out, "  -s                   : enable TIST, synchronized on 1PPS at level 2.\n");
    fprintf(out, "                         This also transmits time using the MNSC.\n");
    fprintf(out, "  -t type              : audio/data service component type"
            " (default: 0)\n");
    fprintf(out, "                         audio: foreground=0, "
            "background=1, multi-channel=2\n");
    fprintf(out, "                         data: unspecified=0, TMC=1, "
            "EWS=2, ITTS=3, paging=4, TDC=5, DMB=24, IP=59, MOT=60, "
            "proprietary=61\n");
    fprintf(out, "  -y PTy               : Primary service component program"
            " type international code\n");
    fprintf(out, "\nOUTPUT OPTIONS\n");
    fprintf(out, "  -O output            : name of the output in format "
            "scheme://[address][:port][/name]\n"
            "                         where scheme is (raw|udp|tcp|file|fifo|simul)\n"
           );
}
#else // no command line options
void printUsage(char *name, FILE* out)
{
    fprintf(out, "NAME\n");
    fprintf(out, "  %s - A software DAB multiplexer\n", name);
    fprintf(out, "\nSYNOPSYS\n");
    fprintf(out, "    This software requires a configuration file:\n");
    fprintf(out, "  %s configuration.mux\n", name);
    fprintf(out, "    See doc/example.config for an example format for the configuration file\n");
    fprintf(out, "\nDESCRIPTION\n");
    fprintf(out,
            "  %s  is a software multiplexer that generates an ETI stream from\n"
            "  audio and data streams. Because of  its  software  based  architecture,\n"
            "  many  typical DAB services can be generated and multiplexed on a single\n"
            "  PC platform with live or pre-recorded sources.\n"
            "\n"
            "  A DAB multiplex configuration is composed of one ensemble. An  ensemble\n"
            "  is  the entity that receivers tune to and process. An ensemble contains\n"
            "  several services. A service is  the  listener-selectable  output.  Each\n"
            "  service  contains  one mandatory service component which is called pri-\n"
            "  mary component. An audio primary component  define  a  program  service\n"
            "  while  a data primary component define a data service. Service can con-\n"
            "  tain additional components which are called secondary components. Maxi-\n"
            "  mum  total  number  of components is 12 for program services and 11 for\n"
            "  data services. A service component is a link to one subchannel (of Fast\n"
            "  Information  Data  Channel).  A  subchannel  is the physical space used\n"
            "  within the common interleaved frame.\n"
            "\n"
            "   __________________________________________________\n"
            "  |                     Ensemble                     |  ENSEMBLE\n"
            "  |__________________________________________________|\n"
            "          |                 |                 |\n"
            "          |                 |                 |\n"
            "   _______V______    _______V______    _______V______\n"
            "  |   Service 1  |  |   Service 2  |  |   Service 3  |  SERVICES\n"
            "  |______________|  |______________|  |______________|\n"
            "     |        |        |        | |______         |\n"
            "     |        |        |        |        |        |\n"
            "   __V__    __V__    __V__    __V__    __V__    __V__\n"
            "  | SC1 |  | SC2 |  | SC3 |  | SC4 |  | SC5 |  | SC6 |  SERVICE\n"
            "  |_____|  |_____|  |_____|  |_____|  |_____|  |_____|  COMPONENTS\n"
            "     |        |   _____|        |        |    ____|\n"
            "     |        |  |              |        |   |\n"
            "   __V________V__V______________V________V___V_______   COMMON\n"
            "  | SubCh1 | SubCh9 |  ...  | SubCh3 | SubCh60 | ... |  INTERLEAVED\n"
            "  |________|________|_______|________|_________|_____|  FRAME\n"
            "  Figure 1: An example of a DAB multiplex configuration\n",
        name);
}
#endif

void printOutputs(vector<shared_ptr<DabOutput> >& outputs)
{
    int index = 0;

    for (auto output : outputs) {
        etiLog.log(info, "Output      %i", index);
        etiLog.level(info) << "  URI: " <<
            output->get_info();

        ++index;
    }
}

void printServices(const vector<shared_ptr<DabService> >& services)
{
    int index = 0;

    for (auto service : services) {

        etiLog.level(info) << "Service       " << service->get_rc_name();
        etiLog.level(info) << " label:       " <<
                service->label.long_label();
        etiLog.level(info) << " short label: " <<
                service->label.short_label();

        etiLog.log(info, " (0x%x)", service->label.flag());
        etiLog.log(info, " id:            0x%lx (%lu)", service->id,
                service->id);

        etiLog.log(info, " pty:           0x%x (%u)", service->pty,
                service->pty);

        etiLog.log(info, " language:      0x%x (%u)",
                service->language, service->language);
        etiLog.log(info, " announcements: 0x%x",
                service->ASu);

        std::vector<std::string> clusters_s;
        for (const auto& cluster : service->clusters) {
            clusters_s.push_back(std::to_string(cluster));
        }
        etiLog.level(info) << " clusters: " << boost::join(clusters_s, ",");
        ++index;
    }
}

void printComponents(vector<DabComponent*>& components)
{
    vector<DabComponent*>::const_iterator current;
    unsigned int index = 0;

    for (current = components.begin(); current != components.end(); ++current) {
        etiLog.level(info) << "Component     " << (*current)->get_rc_name();
        printComponent(*current);
        ++index;
    }
}

void printComponent(DabComponent* component)
{
    etiLog.log(info, " service id:             0x%x (%u)",
            component->serviceId, component->serviceId);
    etiLog.log(info, " subchannel id:          0x%x (%u)",
            component->subchId, component->subchId);
    etiLog.level(info) << " label:                  " <<
            component->label.long_label();
    etiLog.level(info) << " short label:            " <<
            component->label.short_label();

    etiLog.log(info, " (0x%x)", component->label.flag());
    etiLog.log(info, " service component type: 0x%x (%u)", component->type,
            component->type);

    if (component->packet.appType != 0xFFFF) {
        etiLog.log(info, " (packet) id:            0x%x (%u)",
                component->packet.id, component->packet.id);
        etiLog.log(info, " (packet) address:       %u",
                component->packet.address);

        etiLog.log(info, " (packet) app type:      %u",
                component->packet.appType);

        etiLog.log(info, " (packet) datagroup:     %u",
                component->packet.datagroup);
    }
    else if (component->audio.uaType != 0xFFFF) {
        stringstream ss;
        ss <<            " (audio) app type:       ";
        switch (component->audio.uaType) {
            case FIG0_13_APPTYPE_SLIDESHOW:
                ss << "MOT Slideshow";
                break;
            case FIG0_13_APPTYPE_WEBSITE:
                ss << "MOT Broadcast Website";
                break;
            case FIG0_13_APPTYPE_TPEG:
                ss << "TPEG";
                break;
            case FIG0_13_APPTYPE_DGPS:
                ss << "DGPS";
                break;
            case FIG0_13_APPTYPE_TMC:
                ss << "TMC";
                break;
            case FIG0_13_APPTYPE_EPG:
                ss << "EPG";
                break;
            case FIG0_13_APPTYPE_DABJAVA:
                ss << "DAB Java";
                break;
            case FIG0_13_APPTYPE_JOURNALINE:
                ss << "Journaline";
                break;
            default:
                ss << "Unknown: " << component->audio.uaType;
                break;
        }

        etiLog.level(info) << ss.str();
    }
    else {
        etiLog.level(info) << " No app type defined";
    }
}

void printSubchannels(vector<DabSubchannel*>& subchannels)
{
    vector<DabSubchannel*>::iterator subchannel;
    int index = 0;

    for (subchannel = subchannels.begin(); subchannel != subchannels.end();
            ++subchannel) {
        dabProtection* protection = &(*subchannel)->protection;
        etiLog.level(info) << "Subchannel   " << (*subchannel)->uid;
        etiLog.log(info, " input");
        etiLog.level(info) << "   URI:     " << (*subchannel)->inputUri;
        switch ((*subchannel)->type) {
            case subchannel_type_t::Audio:
                etiLog.log(info, " type:       audio");
                break;
            case subchannel_type_t::DataDmb:
                etiLog.log(info, " type:       data");
                break;
            case subchannel_type_t::Fidc:
                etiLog.log(info, " type:       fidc");
                break;
            case subchannel_type_t::Packet:
                etiLog.log(info, " type:       packet");
                break;
            default:
                etiLog.log(info, " type:       unknown");
                break;
        }
        etiLog.log(info, " id:         0x%x (%u)",
                (*subchannel)->id, (*subchannel)->id);
        etiLog.log(info, " bitrate:    %i",
                (*subchannel)->bitrate);
        if (protection->form == UEP) {
            etiLog.log(info, " protection: UEP %i", protection->level + 1);
            etiLog.log(info, "  index:     %i",
                    protection->uep.tableIndex);
        }
        else {
            etiLog.log(info, " protection: EEP %i-%c",
                    protection->level + 1,
                    protection->eep.profile == EEP_A ? 'A' : 'B');
            etiLog.log(info, "  option:    %i",
                    protection->eep.GetOption());
            etiLog.log(info, "  level:     %i",
                    (*subchannel)->protection.level);
        }
        etiLog.log(info, " SAD:        %u",
                (*subchannel)->startAddress);
        etiLog.log(info, " size (CU):  %i",
                (*subchannel)->getSizeCu());
        ++index;
    }
}

void printEnsemble(const shared_ptr<dabEnsemble> ensemble)
{
    etiLog.log(info, "Ensemble");
    etiLog.log(info, " id:          0x%lx (%lu)", ensemble->id, ensemble->id);
    etiLog.log(info, " ecc:         0x%x (%u)", ensemble->ecc, ensemble->ecc);
    etiLog.level(info) << " label:       " <<
            ensemble->label.long_label();
    etiLog.level(info) << " short label: " <<
            ensemble->label.short_label();

    etiLog.log(info, " (0x%x)", ensemble->label.flag());
    etiLog.log(info, " mode:        %u", ensemble->mode);

    if (ensemble->lto_auto) {
        time_t now = time(NULL);
        struct tm* ltime = localtime(&now);
        time_t now2 = timegm(ltime);
        etiLog.log(info, " lto:         %2.1f hours", 0.5 * (now2 - now) / 1800);
    }
    else {
        etiLog.log(info, " lto:         %2.1f hours", 0.5 * ensemble->lto);
    }
    etiLog.log(info, " intl. table. %d", ensemble->international_table);

    if (ensemble->clusters.empty()) {
        etiLog.level(info) << " No announcement clusters defined";
    }
    else {
        for (const auto& cluster : ensemble->clusters) {
            etiLog.level(info) << cluster->tostring();
        }
    }
}