summaryrefslogtreecommitdiff
path: root/radio.c
blob: 521c2bf2acb97268053cfc1e5d0e1bb122a8e5f5 (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
 * radio.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <vdr/plugin.h>
#include <vdr/status.h>
#include <vdr/config.h>
#include <vdr/interface.h>
#include <vdr/transfer.h>
#include <string>
#include "getopt.h"
#include "radioaudio.h"
#include "radiotools.h"
#include "radioepg.h"
#include "inforx.h"
#include "service.h"

#if VDRVERSNUM < 10737
    #error This version of radio-plugin requires vdr >= 1.7.37
#endif

#ifndef GIT_REV
static const char *VERSION = "1.1.0";
#else
static const char *VERSION = GIT_REV;
#endif

static const char *DESCRIPTION    = trNOOP("Radio Background-Image/RDS-Text");
static const char *MAINMENUENTRY  = trNOOP("Show RDS-Radiotext");
char *ConfigDir;
char *DataDir;
char *LiveFile;
char *ReplayFile;

// Setup-Params
int S_Activate = false;
int S_StillPic = 1;
int S_HMEntry = false;
int S_RtFunc = 1;
int S_RtOsdTitle = 1;
int S_RtOsdTags = 2;
int S_RtOsdPos = 1;
int S_RtOsdRows = 2;
int S_RtOsdLoop = 0;
int S_RtOsdTO = 60;
int S_RtSkinColor = 1;
int S_RtBgCol = 0;
int S_RtBgTra = 0xA0;
int S_RtFgCol = 1;
int S_RtDispl = 1;
int S_RtMsgItems = 0;
//int S_RtpMemNo = 25;
int S_RassText = 1;
int S_ExtInfo = 0;
uint32_t rt_color[9];
int S_Verbose = 1;
int S_Encrypted = 0;
// Radiotext
char RT_Text[5][RT_MEL];
char RTP_Artist[RT_MEL], RTP_Title[RT_MEL];
int RT_Info, RT_Index, RT_PTY;
time_t RTP_Starttime;
bool RT_OsdTO = false, RTplus_Osd = false, RT_ReOpen = false;
int RT_OsdTOTemp = 0, Radio_CA = 0;
int RT_Charset = 0;    // 0= ISO-8859-1, 1= UTF8, 2= ..
// RadioCheck
const cChannel *chan;
int IsRadioOrReplay;
// Info
bool DoInfoReq = false, InfoRequest = false;
int InfoTimeout = 3;

// --- cRadioCheck -------------------------------------------------------

class cRadioCheck: public cThread {
private:
    static cRadioCheck *RadioCheck;
protected:
    virtual void Action(void);
    void Stop(void);
public:
    cRadioCheck(void);
    virtual ~cRadioCheck();
    static void Init(void);
    static void Exit(void);
};

cRadioCheck *cRadioCheck::RadioCheck = NULL;

cRadioCheck::cRadioCheck(void)
: cThread("radiocheck")
{
    IsRadioOrReplay = 0;
}

cRadioCheck::~cRadioCheck() {
    if (Running()) {
        Stop();
    }
}

void cRadioCheck::Init(void) {
    if (RadioCheck == NULL) {
        RadioCheck = new cRadioCheck;
        RadioCheck->Start();
    }
}

void cRadioCheck::Exit(void) {
    if (RadioCheck != NULL) {
        RadioCheck->Stop();
        DELETENULL(RadioCheck);
    }
}

void cRadioCheck::Stop(void) {
    Cancel(10);
}

void cRadioCheck::Action(void)
{
    if ((S_Verbose & 0x0f) >= 2) {
        printf("vdr-radio: background-checking starts\n");
    }

    while (Running()) {
        cCondWait::SleepMs(2000);

        // check Live-Radio
        if ((IsRadioOrReplay == 1) && (chan != NULL)) {
            if (chan->Vpid()) {
                isyslog("radio: channnel '%s' got Vpid= %d", chan->Name(),
                        chan->Vpid());
                IsRadioOrReplay = 0;
#if VDRVERSNUM >= 20300
                LOCK_CHANNELS_READ
                Channels->SwitchTo(cDevice::CurrentChannel());
#else
                Channels.SwitchTo(cDevice::CurrentChannel());
#endif
                //cDevice::PrimaryDevice()->SwitchChannel(chan, true);
            } else {
                if ((InfoTimeout -= 2) <= 0) {
                    InfoTimeout = 20;
                    int chtid = chan->Tid();
                    // Kanal-EPG PresentEvent
                    if (chan->Apid(0) > 0
                            && (chtid == PREMIERERADIO_TID
                                    || chtid == KDRADIO_TID
                                    || chtid == UMRADIO_TID1
                                    || chtid == UMRADIO_TID2
                                    || chtid == UMRADIO_TID3
                                    || chtid == UMRADIO_TID4
                                    || chtid == UMRADIO_TID5)) {
#if VDRVERSNUM >= 20300
                        LOCK_SCHEDULES_READ
                        static cStateKey SchedulesStateKey;
                        const cSchedules *scheds = cSchedules::GetSchedulesRead(
                                SchedulesStateKey);
#else
                        cSchedulesLock schedLock;
                        const cSchedules *scheds = cSchedules::Schedules(schedLock);
#endif
                        if (scheds != NULL) {
                            const cSchedule *sched = scheds->GetSchedule(
                                    chan->GetChannelID());
                            if (sched != NULL) {
                                const cEvent *present =
                                        sched->GetPresentEvent();
                                if (present != NULL) {
                                    if (chtid == PREMIERERADIO_TID) { // Premiere
                                        InfoTimeout = epg_premiere(
                                                present->Title(),
                                                present->Description(),
                                                present->StartTime(),
                                                present->EndTime());
                                    }
                                    else if (chtid == KDRADIO_TID) {// Kabel Deutschland
                                        InfoTimeout = epg_kdg(
                                                present->Description(),
                                                present->StartTime(),
                                                present->EndTime());
                                    }
                                    else {
                                        // Unity Media Kabel
                                        InfoTimeout = epg_unitymedia(
                                                present->Description(),
                                                present->StartTime(),
                                                present->EndTime());
                                    }
                                    InfoRequest = true;
                                }
                                else {
                                    dsyslog("radio: no event.present (Tid= %d, Apid= %d)",
                                            chtid, chan->Apid(0));
                                }
                            }
                            else {
                                dsyslog("radio: no schedule (Tid= %d, Apid= %d)",
                                        chtid, chan->Apid(0));
                            }
                        }
                    }
                    // Artist/Title with external script?
                    else if (chan->Apid(0) > 0 && DoInfoReq) {
                        InfoTimeout = info_request(chtid, chan->Apid(0));
                        InfoRequest = (InfoTimeout > 0);
                    }
                }
            }
        }

        // temp. OSD-CloseTimeout
        (RT_OsdTOTemp > 0) ? RT_OsdTOTemp -= 2 : RT_OsdTOTemp = 0; // in sec like this cycletime

        // Radiotext-Autodisplay
        if ((S_RtDispl == 2) && (RT_Info >= 0) && !RT_OsdTO
                && (RT_OsdTOTemp == 0) && RT_ReOpen && !Skins.IsOpen()
                && !cOsd::IsOpen()) {
            cRemote::CallPlugin("radio");
        }
    }

    if ((S_Verbose & 0x0f) >= 2) {
        printf("vdr-radio: background-checking ends\n");
    }
}


// --- cMenuSetupRadio -------------------------------------------------------

class cMenuSetupRadio : public cMenuSetupPage {
private:
    int newS_Activate;
    int newS_StillPic;
    int newS_HMEntry;
    int newS_RtFunc;
    int newS_RtOsdTitle;
    int newS_RtOsdTags;
    int newS_RtOsdPos;
    int newS_RtOsdRows;
    int newS_RtOsdLoop;
    int newS_RtOsdTO;
    int newS_RtSkinColor;
    int newS_RtBgCol;
    int newS_RtBgTra;
    int newS_RtFgCol;
    int newS_RtDispl;
    int newS_RtMsgItems;
    //int newS_RtpMemNo;
    int newS_RassText;
    int newS_ExtInfo;
    const char *T_RtFunc[3];
    const char *T_RtOsdTags[3];
    const char *T_RtOsdPos[2];
    const char *T_RtOsdLoop[2];
    const char *T_RtBgColor[9];
    const char *T_RtFgColor[9];
    const char *T_RtDisplay[3];
    const char *T_RtMsgItems[4];
    const char *T_RassText[3];
protected:
    virtual void Store(void);
public:
    cMenuSetupRadio(void);
};

cMenuSetupRadio::cMenuSetupRadio(void)
{
    T_RtFunc[0] = tr("Off");
    T_RtFunc[1] = tr("only Text");
    T_RtFunc[2] = tr("Text+TagInfo");
    T_RtOsdTags[0] = tr("Off");
    T_RtOsdTags[1] = tr("only, if some");
    T_RtOsdTags[2] = tr("always");
    T_RtOsdPos[0] = tr("Top");
    T_RtOsdPos[1] = tr("Bottom");
    T_RtOsdLoop[0] = tr("latest at Top");
    T_RtOsdLoop[1] = tr("latest at Bottom");
    T_RtBgColor[0] = T_RtFgColor[0] = tr ("Black");
    T_RtBgColor[1] = T_RtFgColor[1] = tr ("White");
    T_RtBgColor[2] = T_RtFgColor[2] = tr ("Red");
    T_RtBgColor[3] = T_RtFgColor[3] = tr ("Green");
    T_RtBgColor[4] = T_RtFgColor[4] = tr ("Yellow");
    T_RtBgColor[5] = T_RtFgColor[5] = tr ("Magenta");
    T_RtBgColor[6] = T_RtFgColor[6] = tr ("Blue");
    T_RtBgColor[7] = T_RtFgColor[7] = tr ("Cyan");
    T_RtBgColor[8] = T_RtFgColor[8] = tr ("Transparent");
    T_RtDisplay[0] = tr("Off");
    T_RtDisplay[1] = tr("about MainMenu");
    T_RtDisplay[2] = tr("Automatic");
    T_RtMsgItems[0] = tr("Off");
    T_RtMsgItems[1] = tr("only Taginfo");
    T_RtMsgItems[2] = tr("only Text");
    T_RtMsgItems[3] = tr("Text+TagInfo");
    T_RassText[0] = tr("Off");
    T_RassText[1] = tr("Rass only");
    T_RassText[2] = tr("Rass+Text mixed");

    newS_Activate = S_Activate;
    newS_StillPic = S_StillPic;
    newS_Activate = S_Activate;
    newS_RtFunc = S_RtFunc;
    newS_RtOsdTitle = S_RtOsdTitle;
    newS_RtOsdTags = S_RtOsdTags;
    newS_RtOsdPos = S_RtOsdPos;
    newS_RtOsdRows = S_RtOsdRows;
    newS_RtOsdLoop = S_RtOsdLoop;
    newS_RtOsdTO = S_RtOsdTO;
    newS_RtSkinColor = S_RtSkinColor;
    newS_RtBgCol = S_RtBgCol;
    newS_RtBgTra = S_RtBgTra;
    newS_RtFgCol = S_RtFgCol;
    newS_RtDispl = (S_RtDispl > 2 ? 2 : S_RtDispl);
    newS_RtMsgItems = S_RtMsgItems;
    //newS_RtpMemNo = S_RtpMemNo;
    newS_RassText = S_RassText;
    newS_ExtInfo = S_ExtInfo;

    Add(new cMenuEditBoolItem( tr("Activate"),                      &newS_Activate));
    Add(new cMenuEditBoolItem( tr("Use StillPicture-Function"),     &newS_StillPic));
    Add(new cMenuEditBoolItem( tr("Hide MainMenuEntry"),            &newS_HMEntry));
    Add(new cMenuEditStraItem( tr("RDSText Function"),              &newS_RtFunc, 3, T_RtFunc));
    Add(new cMenuEditStraItem( tr("RDSText OSD-Position"),          &newS_RtOsdPos, 2, T_RtOsdPos));
    Add(new cMenuEditBoolItem( tr("RDSText OSD-Titlerow"),          &newS_RtOsdTitle));
    Add(new cMenuEditIntItem(  tr("RDSText OSD-Rows (1-5)"),        &newS_RtOsdRows, 1, 5));
    Add(new cMenuEditStraItem( tr("RDSText OSD-Scrollmode"),        &newS_RtOsdLoop, 2, T_RtOsdLoop));
    Add(new cMenuEditStraItem( tr("RDSText OSD-Taginfo"),           &newS_RtOsdTags, 3, T_RtOsdTags));
    Add(new cMenuEditBoolItem( tr("RDSText OSD-Skincolors used"),   &newS_RtSkinColor));
    if (newS_RtSkinColor == 0) {
        Add(new cMenuEditStraItem( tr("RDSText OSD-Backgr.Color"),      &newS_RtBgCol, 9, T_RtBgColor));
        Add(new cMenuEditIntItem(  tr("RDSText OSD-Backgr.Transp."),    &newS_RtBgTra, 1, 255));
        Add(new cMenuEditStraItem( tr("RDSText OSD-Foregr.Color"),      &newS_RtFgCol, 8, T_RtFgColor));
        }
    Add(new cMenuEditIntItem(  tr("RDSText OSD-Timeout (0-1440 min)"),  &newS_RtOsdTO, 0, 1440));
    Add(new cMenuEditStraItem( tr("RDSText OSD-Display"),               &newS_RtDispl, 3, T_RtDisplay));
    Add(new cMenuEditStraItem( tr("RDSText StatusMsg (lcdproc & co)"),  &newS_RtMsgItems, 4, T_RtMsgItems));
    //Add(new cMenuEditIntItem(  tr("RDSplus Memorynumber (10-99)"),    &newS_RtpMemNo, 10, 99));
    Add(new cMenuEditStraItem( tr("RDSText Rass-Function"),             &newS_RassText, 3, T_RassText));
    Add(new cMenuEditBoolItem( tr("External Info-Request"),             &newS_ExtInfo));
}

void cMenuSetupRadio::Store(void)
{
    SetupStore("Activate",               S_Activate = newS_Activate);
    SetupStore("UseStillPic",            S_StillPic = newS_StillPic);
    SetupStore("HideMenuEntry",          S_HMEntry = newS_HMEntry);
    SetupStore("RDSText-Function",       S_RtFunc = newS_RtFunc);
    SetupStore("RDSText-OsdTitle",       S_RtOsdTitle = newS_RtOsdTitle);
    SetupStore("RDSText-OsdTags",        S_RtOsdTags = newS_RtOsdTags);
    SetupStore("RDSText-OsdPosition",    S_RtOsdPos = newS_RtOsdPos);
    SetupStore("RDSText-OsdRows",        S_RtOsdRows = newS_RtOsdRows);
    SetupStore("RDSText-OsdLooping",     S_RtOsdLoop = newS_RtOsdLoop);
    SetupStore("RDSText-OsdSkinColor",   S_RtSkinColor = newS_RtSkinColor);
    SetupStore("RDSText-OsdBackgrColor", S_RtBgCol = newS_RtBgCol);
    SetupStore("RDSText-OsdBackgrTrans", S_RtBgTra = newS_RtBgTra);
    SetupStore("RDSText-OsdForegrColor", S_RtFgCol = newS_RtFgCol);
    SetupStore("RDSText-OsdTimeout",     S_RtOsdTO = newS_RtOsdTO);
    SetupStore("RDSText-Display",        S_RtDispl = newS_RtDispl);
    SetupStore("RDSText-MsgItems",       S_RtMsgItems = newS_RtMsgItems);
    //SetupStore("RDSplus-MemNumber",    S_RtpMemNo = newS_RtpMemNo);
    SetupStore("RDSText-Rass",          S_RassText = newS_RassText);
    SetupStore("ExtInfo-Req",           S_ExtInfo = newS_ExtInfo);
}


// --- cPluginRadio -------------------------------------------------------


class cRadioImage;
class cRadioAudio;

class cPluginRadio : public cPlugin, cStatus {
private:
    // Add any member variables or functions you may need here.
    bool ConfigDirParam;
    bool DataDirParam;
    bool LiveFileParam;
    bool ReplayFileParam;
    cRadioImage *radioImage;
    cRadioAudio *radioAudio;
public:
    cPluginRadio(void);
    virtual ~cPluginRadio();
    virtual const char *Version(void) { return VERSION; }
    virtual const char *Description(void) { return tr(DESCRIPTION); }
    virtual const char *CommandLineHelp(void);
    virtual bool ProcessArgs(int argc, char *argv[]);
    virtual bool Start(void);
    virtual void Stop(void);
    virtual void Housekeeping(void);
    virtual void MainThreadHook(void) { }
    virtual cString Active(void) { return NULL; }
    virtual const char *MainMenuEntry(void) { return (S_Activate==0 || S_RtFunc==0 || S_RtDispl==0 || S_HMEntry ? NULL : tr(MAINMENUENTRY)); }
    virtual cOsdObject *MainMenuAction(void);
    virtual cMenuSetupPage *SetupMenu(void);
    virtual bool SetupParse(const char *Name, const char *Value);
    virtual bool Service(const char *Id, void *Data);
    virtual const char **SVDRPHelpPages(void);
    virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
protected:
    virtual void ChannelSwitch(const cDevice *Device, int ChannelNumber, bool LiveView);
    virtual void Replaying(const cControl *Control, const char *Name, const char *FileName, bool On);
};

cPluginRadio::cPluginRadio(void)
{
    // Initialize any member variables here.
    // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
    // VDR OBJECTS TO EXIST OR PROADUCE ANY OUTPUT!

    radioImage = 0;
    radioAudio = 0;

    ConfigDirParam = false;
    DataDirParam = false;
    LiveFileParam = false;
    ReplayFileParam = false;

    rt_color[0] = 0xFF000000;   //Black
    rt_color[1] = 0xFFFCFCFC;   //White
    rt_color[2] = 0xFFFC1414;   //Red
    rt_color[3] = 0xFF24FC24;   //Green
    rt_color[4] = 0xFFFCC024;   //Yellow
    rt_color[5] = 0xFFB000FC;   //Magenta
    rt_color[6] = 0xFF0000FC;   //Blue
    rt_color[7] = 0xFF00FCFC;   //Cyan
    rt_color[8] = 0x00000000;   //Transparent
}

cPluginRadio::~cPluginRadio()
{
    // Clean up after yourself!
    if (ConfigDir)  free(ConfigDir);
    if (DataDir)    free(DataDir);
    if (LiveFile)   free(LiveFile);
    if (ReplayFile) free(ReplayFile);

    cRadioCheck::Exit();
    radioImage->Exit();
}


const char *cPluginRadio::CommandLineHelp(void)
{
    // Return a string that describes all known command line options.
    return "  -f dir,   --files=dir      use dir as image directory (default: <vdrconfig>/plugins/radio)\n"
           "  -d dir,   --data=dir       use dir as temp. data directory (default: <vdrconfig>/plugins/radio)\n"
           "  -l file,  --live=file      use file as default mpegfile in livemode (default: <dir>/radio.mpg)\n"
           "  -r file,  --replay=file    use file as default mpegfile in replaymode (default: <dir>/replay.mpg)\n"
           "  -e 1,     --encrypted=1    use transfermode/backgroundimage @ encrypted radiochannels     \n"
           "  -v level, --verbose=level  set verbose level (default = 1, 0 = Off, 1 = RDS-Text+Tags,    \n"
           "                                                2 = +RDS-Telegram/Debug, 3 = +RawData 0xfd, \n"
           "                                                += 16 = Rass-Info, +=32 = TMC-Info)         \n";
}

bool cPluginRadio::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.

    static struct option long_options[] = {
            { "files",      required_argument, NULL, 'f' },
            { "data",       required_argument, NULL, 'd' },
            { "live",       required_argument, NULL, 'l' },
            { "replay",     required_argument, NULL, 'r' },
            { "encrypted",  required_argument, NULL, 'e' },
            { "verbose",    required_argument, NULL, 'v' },
            { NULL }
    };

    int c;
    while ((c = getopt_long(argc, argv, "f:d:l:r:e:v:", long_options, NULL)) != -1) {
    switch (c) {
        case 'f':
                printf("vdr-radio: arg files-dir = %s\n", optarg);
                ConfigDir = strdup(optarg);
                ConfigDirParam = true;
                break;
        case 'd':
                printf("vdr-radio: arg data-dir = %s\n", optarg);
                DataDir = strdup(optarg);
                DataDirParam = true;
                break;
        case 'l':
                printf("vdr-radio: arg live-mpeg = %s\n", optarg);
                LiveFile = strdup(optarg);
                LiveFileParam = true;
                break;
        case 'r':
                printf("vdr-radio: arg replay-mpeg = %s\n", optarg);
                ReplayFile = strdup(optarg);
                ReplayFileParam = true;
                break;
        case 'v':
                printf("vdr-radio: arg verbose = %s\n", optarg);
                if (isnumber(optarg))
                    S_Verbose = atoi(optarg);
                break;
        case 'e':
                printf("vdr-radio: arg encrypted = %s\n", optarg);
                if (isnumber(optarg))
                    S_Encrypted = atoi(optarg);
                break;
        default:
                printf("vdr-radio: arg char = %c\n", c);
                return false;
        }
    }

    return true;
}

bool cPluginRadio::Start(void)
{
    // Start any background activities the plugin shall perform.
    printf("vdr-radio: Radio-Plugin Backgr.Image/RDS-Text starts...\n");

    radioImage = new cRadioImage;
    if (!radioImage)
        return false;
    radioImage->Init();

    radioAudio = new cRadioAudio;
    if (!radioAudio)
        return false;

    if (!ConfigDirParam)
        ConfigDir = strdup(ConfigDirectory(Name()));
    if (!DataDirParam) {
        DataDir = strdup("/tmp/vdr-radio.XXXXXX");
        mkdtemp(DataDir);
        }
    if (!LiveFileParam) {
        asprintf(&LiveFile, "%s/radio.mpg", ConfigDir);
    }
    if (!ReplayFileParam) {
        asprintf(&ReplayFile, "%s/replay.mpg", ConfigDir);
    }

    cRadioCheck::Init();

    return true;
}

void cPluginRadio::Stop(void)
{
    cRadioCheck::Exit();

    if (IsRadioOrReplay > 0) {
        radioAudio->DisableRadioTextProcessing();
    }
    radioImage->Exit();
}

void cPluginRadio::Housekeeping(void)
{
    // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginRadio::MainMenuAction(void)
{
    // Perform the action when selected from the main VDR menu.
/*  if (!cDevice::PrimaryDevice()->Transferring() && !cDevice::PrimaryDevice()->Replaying()) {
        //cRemote::CallPlugin("radio"); // try again later <-- disabled, looping if activate over menu @ tv in dvb-livemode
        }   */
    if (S_Activate > 0 && S_RtFunc > 0 && S_RtDispl > 0
            && IsRadioOrReplay > 0) {
        if (!RTplus_Osd) {
            cRadioTextOsd *rtosd = new cRadioTextOsd();
            return rtosd;
        } else {
            cRTplusOsd *rtposd = new cRTplusOsd();
            return rtposd;
        }
    }

    return NULL;
}

cMenuSetupPage *cPluginRadio::SetupMenu(void)
{
    // Return a setup menu in case the plugin supports one.
    return new cMenuSetupRadio;
}

bool cPluginRadio::SetupParse(const char *Name, const char *Value)
{
    // Parse your own setup parameters and store their values.
    if      (!strcasecmp(Name, "Activate"))               S_Activate = atoi(Value);
    else if (!strcasecmp(Name, "UseStillPic"))            S_StillPic = atoi(Value);
    else if (!strcasecmp(Name, "HideMenuEntry"))          S_HMEntry = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-Function"))       S_RtFunc = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdTitle"))       S_RtOsdTitle = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdTags"))        S_RtOsdTags = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdPosition"))    S_RtOsdPos = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdRows")) {
       S_RtOsdRows = atoi(Value);
       if (S_RtOsdRows > 5)  S_RtOsdRows = 5;
       }
    else if (!strcasecmp(Name, "RDSText-OsdLooping"))     S_RtOsdLoop = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdSkinColor"))   S_RtSkinColor = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdBackgrColor")) S_RtBgCol = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdBackgrTrans")) S_RtBgTra = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdForegrColor")) S_RtFgCol = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-OsdTimeout")) {
       S_RtOsdTO = atoi(Value);
       if (S_RtOsdTO > 1440)  S_RtOsdTO = 1440;
       }
    else if (!strcasecmp(Name, "RDSText-Display"))        S_RtDispl = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-MsgItems"))       S_RtMsgItems = atoi(Value);
    //else if (!strcasecmp(Name, "RDSplus-MemNumber"))    S_RtpMemNo = atoi(Value);
    else if (!strcasecmp(Name, "RDSText-Rass"))           S_RassText = atoi(Value);
    else if (!strcasecmp(Name, "ExtInfo-Req"))            S_ExtInfo = atoi(Value);
    else
        return false;

    return true;
}

bool cPluginRadio::Service(const char *Id, void *Data)
{
    static struct tm tm_store;
    if ((strcmp(Id, RADIO_TEXT_SERVICE0) == 0) && (S_Activate > 0) && (S_RtFunc >= 1)) {
        if (Data) {
            RadioTextService_v1_0 *data = (RadioTextService_v1_0*)Data;
            data->rds_pty = RT_PTY;
            data->rds_info = (RT_Info < 0) ? 0 : RT_Info;
            int ind = (RT_Index == 0) ? S_RtOsdRows - 1 : RT_Index - 1;
            data->rds_text = RT_Text[ind];
            data->rds_title = RTP_Title;
            data->rds_artist = RTP_Artist;
            data->title_start = localtime_r(&RTP_Starttime, &tm_store);
        }
        return true;
    }
    else if ((strcmp(Id, RADIO_TEXT_SERVICE1) == 0) && (S_Activate > 0) && (S_RtFunc >= 1)) {
        if (Data) {
            cCharSetConv conf(RT_Charset == 0 ? "ISO-8859-1" : 0);
            RadioTextService_v1_1 *data = (RadioTextService_v1_1*)Data;
            int ind = (RT_Index == 0) ? S_RtOsdRows - 1 : RT_Index - 1;
            data->rds_pty = RT_PTY;
            data->rds_info = RT_Info < 0 ? 0 : RT_Info;
            data->rds_pty_info = RT_PTY > 0 ? ptynr2string(RT_PTY) : "";
            data->rds_text = conf.Convert(RT_Text[ind]);
            data->rds_title = conf.Convert(RTP_Title);
            data->rds_artist = conf.Convert(RTP_Artist);
            data->title_start = RTP_Starttime;
        }
        return true;
    }
    return false;
}

const char **cPluginRadio::SVDRPHelpPages(void)
{
    static const char *HelpPages[] = {
        "RTINFO\n"
        "  Print the radiotext information.",
        "RTCLOSE\n"
        "  Close the radiotext-osd,\n"
        "  Reopen can only be done over menu or channelswitch.",
        "RTTCLOSE\n"
        "  Close the radiotext-osd temporarily,\n"
        "  Reopen will be done after osd-messagetimeout.",
        NULL
        };

    return HelpPages;
}

cString cPluginRadio::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
    if (strcasecmp(Command, "RTINFO") == 0) {
        // we use the default reply code here
        if (RT_Info == 2) {
            int ind = (RT_Index == 0) ? S_RtOsdRows - 1 : RT_Index - 1;
            return cString::sprintf(
                    " Radiotext: %s\n RT-Title : %s\n RT-Artist: %s\n",
                    RT_Text[ind], RTP_Title, RTP_Artist);
        } else if (RT_Info == 1) {
            int ind = (RT_Index == 0) ? S_RtOsdRows - 1 : RT_Index - 1;
            return cString::sprintf(" Radiotext: %s\n", RT_Text[ind]);
        } else
            return cString::sprintf(" Radiotext not available (yet)\n");
    } else if (strcasecmp(Command, "RTCLOSE") == 0) {
        // we use the default reply code here
        if (RT_OsdTO)
            return cString::sprintf("RT-OSD already closed");
        else {
            RT_OsdTO = true;
            return cString::sprintf("RT-OSD will be closed now");
        }
    } else if (strcasecmp(Command, "RTTCLOSE") == 0) {
        // we use the default reply code here
        RT_OsdTOTemp = 2 * Setup.OSDMessageTime;
        return cString::sprintf("RT-OSD will be temporarily closed");
    }

    return NULL;
}

void cPluginRadio::ChannelSwitch(const cDevice *Device, int ChannelNumber,
                                 bool LiveView) {
    if (Device != cDevice::PrimaryDevice()) {
        return;
    }

    IsRadioOrReplay = Radio_CA = 0;
    radioAudio->DisableRadioTextProcessing();
    InfoTimeout = 3;

    if (S_Activate == false) {
        return;
    }

    char *image;
    if (cDevice::CurrentChannel() == ChannelNumber) {
#if VDRVERSNUM >= 20300
        LOCK_CHANNELS_READ
        chan = ChannelNumber ? Channels->GetByNumber(ChannelNumber) : NULL;
#else
        chan = ChannelNumber ? Channels.GetByNumber(ChannelNumber) : NULL;
#endif
        if (chan != NULL && chan->Vpid() == 0 && chan->Apid(0) > 0) {
            asprintf(&image, "%s/%s.mpg", ConfigDir, chan->Name());
            if (!file_exists(image)) {
                dsyslog("radio: channel-image not found '%s' (Channelname= %s)",
                        image, chan->Name());
                free(image);
                asprintf(&image, "%s", LiveFile);
                if (!file_exists(image)) {
                    dsyslog("radio: live-image not found '%s' (Channelname= %s)",
                            image, chan->Name());
                }
            }
            dsyslog("radio: [ChannelSwitch # Apid= %d, Ca= %d] channelname '%s', use image '%s'",
                    chan->Apid(0), chan->Ca(0), chan->Name(), image);
            if ((Radio_CA = chan->Ca(0)) == 0 || S_Encrypted == 1) {
                cDevice::PrimaryDevice()->ForceTransferMode();
            }
            radioImage->SetBackgroundImage(image);
            radioAudio->EnableRadioTextProcessing(chan->Name(), chan->Apid(0), false);
            free(image);
            IsRadioOrReplay = 1;
            DoInfoReq = (S_ExtInfo > 0);
        }
    }
}

void cPluginRadio::Replaying(const cControl *Control, const char *Name,
        const char *FileName, bool On) {
    IsRadioOrReplay = 0;
    radioAudio->DisableRadioTextProcessing();

    if (S_Activate == false) {
        return;
    }

    bool isRadio = false;

    if (On && FileName != NULL) {
        char *vdrfile;
        // check VDR PES-Recordings
        asprintf(&vdrfile, "%s/001.vdr", FileName);
        if (file_exists(vdrfile)) {
            cFileName fn(FileName, false, true, true);
            cUnbufferedFile *f = fn.Open();
            if (f) {
                uchar b[4] = { 0x00, 0x00, 0x00, 0x00 };
                ReadFrame(f, b, sizeof(b), sizeof(b));
                fn.Close();
                isRadio = (b[0] == 0x00) && (b[1] == 0x00) && (b[2] == 0x01)
                        && (0xc0 <= b[3] && b[3] <= 0xdf);
            }
        }
        // check VDR TS-Recordings
        asprintf(&vdrfile, "%s/info", FileName);
        if (file_exists(vdrfile)) {
            cRecordingInfo rfi(FileName);
            if (rfi.Read()) {
                if (rfi.FramesPerSecond() > 0 && rfi.FramesPerSecond() < 18) {// max. seen 13.88 @ ARD-RadioTP 320k
                    isRadio = true;
                }
            }
        }
        free(vdrfile);
    }

    if (isRadio) {
        if (!file_exists(ReplayFile)) {
            dsyslog("radio: replay-image not found '%s'", ReplayFile);
        }
        else {
            radioImage->SetBackgroundImage(ReplayFile);
        }
        radioAudio->EnableRadioTextProcessing(Name, 0, true);
        IsRadioOrReplay = 2;
    }
}


VDRPLUGINCREATOR(cPluginRadio); // Don't touch this!