summaryrefslogtreecommitdiff
path: root/audiorecorder.c
blob: 41d2c9092778ee07d785a71e7097739c7fb3338e (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
/*
 * audiorecorder.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */


#include "audiorecorder.h"
#include "setup.h"
#include "mainmenu.h"
#include "service.h"

#include <vdr/plugin.h>

#include <getopt.h>
#include <sys/stat.h>
#include <unistd.h>

#include <iostream>

#if defined(APIVERSNUM) && APIVERSNUM < 20000
#error "VDR-2.0.0 API version or greater is required!"
#endif

extern "C" {
#include <libavcodec/avcodec.h>
}

// extern cChannels Channels;

using namespace std;

/* --- cPluginAudiorecorder ------------------------------------------------- */

const char *cPluginAudiorecorder::DESCRIPTION = tr("floods your disc with music");
const char *cPluginAudiorecorder::VERSION = "2.0.0_rc6";

string cPluginAudiorecorder::recdir;
int cPluginAudiorecorder::debug = 0;
string cPluginAudiorecorder::cfg;
string cPluginAudiorecorder::pscript;

cPluginAudiorecorder::cPluginAudiorecorder(void)
{
        dispatcher = NULL;
        postproc = NULL;
}


cPluginAudiorecorder::~cPluginAudiorecorder()
{
        delete dispatcher;
        delete postproc;
}


const char *cPluginAudiorecorder::CommandLineHelp(void)
{
        return "  -r dir,   --recdir=dir    set recording-directory to 'dir'.\n"
               "  -d level, --debug=level   set debugging level (default: 0),\n"
               "                            all stuff is written to stdout.\n"
               "                            0 = off\n"
               "                            1 = only errors\n"
               "                            2 = errors and further infos\n"
               "  -p script, --postproc=script set script (default: none)\n"
               "                            for postprocessing.\n";

}


bool cPluginAudiorecorder::ProcessArgs(int argc, char *argv[])
{
        int c, option_index = 0;
//        struct stat dir;

        static struct option long_options[] = {
                { "recdir", required_argument, NULL, 'r' },
                { "debug", required_argument, NULL, 'd' },
                { "postproc", required_argument, NULL, 'p' },
                { NULL }
        };

        c = getopt_long(argc, argv, "r:v:d:p:", long_options, &option_index);

        while (c != -1) {
                switch (c) {
                case 'r':
                        recdir = optarg;
                        break;
                case 'd':
                        if (atoi(optarg) > 0)
                                debug = atoi(optarg);
                        break;
                case 'p':
                        pscript = optarg;
                        break;
                default:
                        return false;
                }

                c = getopt_long(argc, argv, "r:v:d:p:", long_options,
                        &option_index);
        }

        if (recdir.empty()) {
                cerr << endl << "audiorecorder: missing parameter --recdir|-r "
                     << endl;
                dsyslog("audiorecorder: missing parameter --recdir|-r");
                return false;
        }

        if (recdir[recdir.length() - 1] != '/')
                recdir.append("/");
//LT
/*
        stat(recdir.c_str(), &dir);

        if (! (dir.st_mode & S_IFDIR)) {
                cerr << endl << "audiorecorder: " << recdir << " (given with "
                        "the parameter --recdir|-r) isn't a directory" << endl;
                dsyslog("[audiorecorder]: %s (given with the parameter "
                        " --recdir|-r) isn't a directory", recdir.c_str());
                return false;
        }

        if (access(recdir.c_str(), R_OK | W_OK) != 0) {
                cerr << endl << "audiorecorder: can't access " << recdir <<
                        " (given with the parameter --recdir|-r)" << endl;
                dsyslog("[audiorecorder]: can't access %s (given with the "
                        "parameter --recdir|-r)", recdir.c_str());
                return false;
        }
*/
//
        dsyslog("[audiorecorder]: external path-script  : %s (%s, %s())", pscript.c_str(), __FILE__, __func__);



        return true;
}


bool cPluginAudiorecorder::Initialize(void)
{
        /* Initialize any background activities the plugin shall perform. */

        cfg.append(strdup(ConfigDirectory(PLUGIN_NAME_I18N)));
        cfg.append("/audiorecorder.conf");

        audio_codecs[0]   = "mp2";
        audio_codecs[1]   = "libmp3lame";
//        audio_codecs[2]   = "mp3"; // not needed, used

        audio_codecs_translated[0]   = tr("mp2");
        audio_codecs_translated[1]   = tr("mp3");
//        audio_codecs_translated[2]   = tr("mp3"); // not needed, used

        fade_types[0]     = tr("off");
        fade_types[1]     = tr("linear");
        fade_types[2]     = tr("exponential");

        views[0]          = tr("all");
        views[1]          = tr("by artist");
        views[2]          = tr("by channel");
        views[3]          = tr("by date");

        file_patterns[0]  = tr("Artist/Title");
        file_patterns[1]  = tr("Artist - Title");
        file_patterns[2]  = tr("Channel/Artist/Title");
        file_patterns[3]  = tr("Channel - Artist - Title");

        return true;
}


bool cPluginAudiorecorder::Start(void)
{
        /* initialize libavcodec */
        avcodec_register_all();

        probe_audio_codecs();

        Cache.load();

        dispatcher = new cDispatcher();
        postproc = new cPostproc();

        return true;
}


void cPluginAudiorecorder::Stop(void)
{
        /* Stop any background activities the plugin shall perform. */
}


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


const char *cPluginAudiorecorder::MainMenuEntry(void)
{
        main_menu.str("");
        main_menu.clear();

        main_menu << tr("Audiorecorder") << " ("
                << (dispatcher->is_active() ? tr("on") : tr("off"))
                << ") " << dispatcher->get_recording_receivers()
                << "/" << dispatcher->get_attached_receivers(-1)
                << "/" << SetupValues.max_receivers
                << ", " << postproc->get_num_queued();

        return main_menu.str().c_str();
}


cOsdObject *cPluginAudiorecorder::MainMenuAction(void)
{
        return new cMainmenu(dispatcher);
}


cString cPluginAudiorecorder::Active(void)
{
        /*
	if (postproc->get_num_queued() != 0)
		return "active postprocessings";
        */
	return NULL;
}


cMenuSetupPage *cPluginAudiorecorder::SetupMenu(void)
{
        return new cAudiorecorderSetup();
}


bool cPluginAudiorecorder::SetupParse(const char *name, const char *value)
{
        if (strcmp(name, "start_type") == 0)
                SetupValues.start_type = atoi(value);
        else if (strcmp(name, "max_receivers") == 0)
                SetupValues.max_receivers = atoi(value);
        else if (strcmp(name, "min_free_space") == 0)
                SetupValues.min_free_space = atoi(value);
        else if (strcmp(name, "pause") == 0)
                SetupValues.pause = atoi(value);
        else if (strcmp(name, "max_postproc") == 0)
                SetupValues.max_postproc = atoi(value);
        else if (strcmp(name, "default_view") == 0)
                SetupValues.default_view = atoi(value);
        else if (strcmp(name, "fade_in") == 0)
                SetupValues.fade_in = atoi(value);
        else if (strcmp(name, "fade_in_mode") == 0)
                SetupValues.fade_in_mode = atoi(value);
        else if (strcmp(name, "fade_out") == 0)
                SetupValues.fade_out = atoi(value);
        else if (strcmp(name, "fade_out_mode") == 0)
                SetupValues.fade_out_mode = atoi(value);
        else if (strcmp(name, "audio_codec") == 0) {
                int c;

                for (c = 0; c < SetupValues.num_audio_codecs; ++c) {
                        if (strcmp(value, audio_codecs_translated[c]) == 0) {
                                SetupValues.audio_codec = c;
                                break;
                        }
                }
        }
        else if (strcmp(name, "bit_rate") == 0)
                SetupValues.bit_rate = atoi(value);
        else if (strcmp(name, "file_pattern") == 0)
                SetupValues.file_pattern = atoi(value);
        else if (strcmp(name, "upper") == 0)
                SetupValues.upper = atoi(value);
        else if (strcmp(name, "copies") == 0)
                SetupValues.copies = atoi(value);
        else
                return false;

        return true;
}


bool cPluginAudiorecorder::Service(const char *id, void *data)
{
        if (! id)
                return false;

        if (strcmp(id, "Audiorecorder-StatusRtpChannel-v1.0") == 0) {
                if (data) {
                        struct Audiorecorder_StatusRtpChannel_v1_0 *d;
                        d = (struct Audiorecorder_StatusRtpChannel_v1_0 *)data;
                        d->status =
                                dispatcher->get_recording_status(d->channel);
                }
                return true;
        }

        return false;
}

const char **cPluginAudiorecorder::SVDRPHelpPages(void)
{
        /* Return help text for SVDRP commands this plugin implements */
        return NULL;
}


cString cPluginAudiorecorder::SVDRPCommand(const char *command,
        const char *option, int &reply_code)
{
        /* Process SVDRP commands this plugin implements */
        return NULL;
}


void cPluginAudiorecorder::probe_audio_codecs() {
        int c;
        const char *tmp;
        AVCodec *codec = NULL;

        for (c = 1; c < SetupValues.num_audio_codecs; ++c) {
                codec = avcodec_find_encoder_by_name(audio_codecs[c]);
                if (codec)
                        continue;

                dsyslog("[audiorecorder]: your version of libavcodec (ffmpeg) "
                        "is not compiled with %s support (%s, %s())", audio_codecs[c], __FILE__, __func__);

                tmp = audio_codecs[c];
                audio_codecs[c] =
                        audio_codecs[SetupValues.num_audio_codecs - 1];
                audio_codecs[SetupValues.num_audio_codecs - 1] = tmp;
                --SetupValues.num_audio_codecs;
        }
}


VDRPLUGINCREATOR(cPluginAudiorecorder); /* Don't touch this! */