~audio-recorder/audio-recorder/trunk

22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
1
/*
1424 by Osmo Antero
Version 3.0.0
2
 * Copyright (c) 2011- Osmo Antero.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Library General Public
6
 * License as published by the Free Software Foundation; either
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
7
 * version 3 of the License (GPL3), or any later version.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
463 by Osmo Antero
Updated README and INSTALL files.
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
12
 * See the GNU Library General Public License 3 for more details.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
13
 *
14
 * You should have received a copy of the GNU Library General Public
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
15
 * License 3 along with this program; if not, see /usr/share/common-licenses/GPL file
1012.1.5 by David Rabel
GPL boilerplate updated in source files.
16
 * or <http://www.gnu.org/licenses/>.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
17
*/
18
#include <string.h>
19
#include <glib.h>
20
#include <math.h>
21
#include "timer.h"
22
23
#include "utility.h"
24
#include "support.h"
69 by Osmo Antero
Ported this application to GTK/GDK 3. Using now GSettings (and dconf) instead of GConf2
25
#include "dconf.h"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
26
#include "log.h"
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
27
#include "gst-vad.h"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
28
#include "audio-sources.h"
29
#include "rec-manager.h"
30
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
31
/*
32
  Sample commands:
33
34
  Clock time:
35
36
  start at 09:30 pm
37
  start at 21:30
38
  start at 21:30:00
39
40
  -- During runtime:
41
       * Compare clock time to given timer value (clock time hh:mm:ss).
42
       * Start recorder (pipeline) if condition is TRUE.
43
       * User must stop recording manually (or by other command).
44
  -----------------------------------------------------
45
46
  Clock time or duration of recording:
47
48
  stop at 11:30 am
49
  stop after 1h 20m
50
  stop after 1 hour 20 min
51
  pause after 20 min
52
53
  -- During runtime:
54
       * Compare clock time to the given timer value.
55
       * Compare duration to the given timer limit.
56
       * Stop or pause recorder (pipeline) if condition is TRUE.
57
  -----------------------------------------------------
58
59
  Managing "silence".
60
61
  stop if silence
62
  stop if silence 5s
63
  stop if silence 5s 0.4
64
  stop if silence 5s 40%
65
66
  -- During runtime:
67
       * Make sure VAD is running.
68
       * Send threshold signals to this module (timer.c)
69
       * Recorder will stop if threshold < limit.
70
  ------------------------------------------------------
71
72
  pause if silence 5s 0.3
73
  pause if silence 5s 30%
74
  pause if silence 5s -24dB
75
  pause if silence
76
77
  -- During runtime:
78
       * Make sure VAD is running.
79
       * Send threshold signals to this module (timer.c)
80
       * Recorder will PAUSE if threshold < limit, and PLAY if threshold >= limit.
81
  ---------------------------------------------------------
82
83
  Using "sound", "voice" and "audio" commands.
84
85
  start if sound
86
  start if sound 0.3
87
  start if voice 30%
88
  start if voice 0.3
89
  start if audio -20dB
90
91
  NOTICE: Unfortunately these commands cannot handle time delay very well!
92
93
  These commands fire immediately after the volume exceeds/or is beneath the given limit (default limit is 0.3, or 30%).
94
95
  -- During runtime:
96
       * Make sure VAD is running.
97
       * Send threshold signals to gst-recorder.c.
98
       * Recorder will START if threshold >= limit, and PAUSE if threshold < limit.
463 by Osmo Antero
Updated README and INSTALL files.
99
  ---------------------------------------------------------
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
100
101
  File size:
102
103
  stop after 10MB
104
  pause if 2MB
105
106
  -- During runtime:
107
       * Take recorded file size and compare it to value in the timer command (in timer.c).
108
       * Start recorder (pipeline) if condition is TRUE.
463 by Osmo Antero
Updated README and INSTALL files.
109
  ---------------------------------------------------------
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
110
111
  stop after 2 GB | 12 pm | silence 4s
463 by Osmo Antero
Updated README and INSTALL files.
112
  start at 10:20 pm | voice
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
113
114
  Multiple conditions on one line, separated by "|" or "or".
463 by Osmo Antero
Updated README and INSTALL files.
115
  ---------------------------------------------------------
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
116
117
  Notice: The words "voice", "audio" and "sound" have all *same meaning*. Ok!
118
463 by Osmo Antero
Updated README and INSTALL files.
119
  The word "silence" is relative to the given volume level/threshold.
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
120
  Silence has both duration (in seconds) and volume limit.
121
122
  Volume limit can be given as:
123
  -Decimal value between [0, 1.0].
124
  -% value between [0%, 100%]. So 1.0 = 100%.
125
  -Or decibel value.
463 by Osmo Antero
Updated README and INSTALL files.
126
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
127
*/
128
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
129
// Timer function call frequency in seconds
130
#define TIMER_CALL_FREQ 1
1 by Osmo Antero Maatta
Initial import 17.jan.2011
131
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
132
// Default silence duration (in seconds)
133
#define DEF_SILENCE_DURATION 3
1 by Osmo Antero Maatta
Initial import 17.jan.2011
134
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
135
// Timer function id
1 by Osmo Antero Maatta
Initial import 17.jan.2011
136
static guint g_timer_func_id = 0;
137
138
// A GList of TimerRec nodes from the timer-parser.c
139
G_LOCK_DEFINE_STATIC(g_t_list);
140
static GList *g_t_list = NULL;
141
142
// Timer's start time
143
static struct tm g_timer_start_time;
144
145
void timer_func_stop();
146
gboolean timer_func_cb(gpointer user_data);
147
void timer_func_exit_cb(gpointer user_data);
148
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
149
gchar timer_func_eval_command(TimerRec *tr);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
150
151
static void timer_set_start_time();
152
static struct tm timer_get_start_time();
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
153
static void timer_update_records_1();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
154
155
static gchar timer_test_filesize(TimerRec *tr);
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
156
static gchar timer_test_clock_time_S(TimerRec *tr);
157
static gchar timer_test_clock_time_T(TimerRec *tr);
158
static gchar timer_test_clock_time_P(TimerRec *tr);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
159
static gchar timer_test_time_duration(TimerRec *tr);
160
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
161
static void test_silence(TimerRec *tr, GstClockTimeDiff time_diff, gdouble rms);
162
static void test_sound(TimerRec *tr, GstClockTimeDiff time_diff, gdouble rms);
163
static void execute_action(TimerRec *tr, gchar action);
164
1 by Osmo Antero Maatta
Initial import 17.jan.2011
165
void timer_module_init() {
166
    LOG_DEBUG("Init timer.c.\n");
167
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
168
    // Init gst-vad.c
169
    vad_module_init();
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
170
1 by Osmo Antero Maatta
Initial import 17.jan.2011
171
    g_timer_func_id = 0;
172
173
    // Init parser module
174
    parser_module_init();
175
176
    // Start the timer function
177
    timer_func_start();
178
}
179
180
void timer_module_exit() {
181
    LOG_DEBUG("Clean up timer.c.\n");
182
183
    // Stop timer function
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
184
    timer_func_stop();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
185
186
    // Clean up parser module
187
    parser_module_exit();
188
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
189
    // Clean up gst-vad.c
190
    vad_module_exit();
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
191
1 by Osmo Antero Maatta
Initial import 17.jan.2011
192
    g_t_list = NULL;
193
}
194
326 by Osmo Antero
Version 1.2. First build for Ubuntu 13.10 (Saucy Salamander). Renamed configure.in to configure.ac.
195
void timer_set_debug_flag(gboolean on) {
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
196
    // Set debug flag. Please see application options:
197
    // $ audio-recorder --help
326 by Osmo Antero
Version 1.2. First build for Ubuntu 13.10 (Saucy Salamander). Renamed configure.in to configure.ac.
198
    vad_set_debug_flag(on);
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
199
}
200
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
201
void timer_module_reset(gint for_state) {
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
202
    // Reset timer before we move to the given state
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
203
204
    switch (for_state) {
205
    case GST_STATE_PLAYING:
202 by osmoma at gmail
Changes towards v0.9.5 on Ubuntu 12.10 and Fedora 18.
206
    case GST_STATE_NULL:
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
207
        timer_update_records_1();
208
        break;
209
210
    case GST_STATE_PAUSED:
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
211
        //timer_update_records_2();
463 by Osmo Antero
Updated README and INSTALL files.
212
        break;
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
213
214
    default:
215
        ;
216
    }
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
217
}
218
219
void timer_module_rec_start() {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
220
    // Called when recording stops.
221
222
    // Reset timer
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
223
    timer_update_records_1();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
224
}
225
226
// --------------------------------------------------
227
// The actual timer function
228
// --------------------------------------------------
229
230
void timer_func_start() {
231
    // Already running?
232
    if (g_timer_func_id > 0) {
233
        return;
234
    }
235
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
236
    // Start the timer function
463 by Osmo Antero
Updated README and INSTALL files.
237
    g_timer_func_id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, TIMER_CALL_FREQ, (GSourceFunc)timer_func_cb,
238
                      (gpointer)1/*!= 0*/, (GDestroyNotify)timer_func_exit_cb);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
239
}
240
241
void timer_func_stop() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
242
    // Stop the timer funcion
243
    if (g_timer_func_id > 0) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
244
        g_source_remove(g_timer_func_id);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
245
    }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
246
    g_timer_func_id = 0;
247
}
248
249
void timer_func_exit_cb(gpointer user_data) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
250
    // Nothing to cleanup
251
    ;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
252
}
253
254
void timer_settings_changed() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
255
    // Increment the counter so various modules know that the timer-settings have been altered
256
    gint count = 0;
69 by Osmo Antero
Ported this application to GTK/GDK 3. Using now GSettings (and dconf) instead of GConf2
257
    conf_get_int_value("timer-setting-counter", &count);
258
    conf_save_int_value("timer-setting-counter", count+1);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
259
}
260
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
261
static void timer_update_records_1() {
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
262
    // Reset timer nodes
1 by Osmo Antero Maatta
Initial import 17.jan.2011
263
    G_LOCK(g_t_list);
264
265
    GList *item = g_list_first(g_t_list);
266
    while (item) {
267
        TimerRec *tr = (TimerRec*)item->data;
268
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
269
        // Start to count from 0
270
        tr->time_below = 0.0;
271
        tr->time_above = 0.0;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
272
273
        // Next item
274
        item = g_list_next(item);
275
    }
276
277
    G_UNLOCK(g_t_list);
278
}
279
463 by Osmo Antero
Updated README and INSTALL files.
280
#if 0
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
281
static void timer_update_records_2() {
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
282
    // Reset timer nodes
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
283
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
284
    G_LOCK(g_t_list);
285
286
    GList *item = g_list_first(g_t_list);
287
    while (item) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
288
        //TimerRec *tr = (TimerRec*)item->data;
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
289
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
290
        // Start to count from 0
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
291
        //tr->seconds = 0;
292
        //tr->seconds_x = 0;
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
293
294
        // Next item
295
        item = g_list_next(item);
296
    }
297
    G_UNLOCK(g_t_list);
298
}
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
299
#endif
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
300
1 by Osmo Antero Maatta
Initial import 17.jan.2011
301
static void timer_clear_list() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
302
    // Reset the timer list
1 by Osmo Antero Maatta
Initial import 17.jan.2011
303
304
    // Lock g_timer_list
305
    G_LOCK(g_t_list);
306
307
    parser_free_list();
308
    g_t_list = NULL;
309
310
    // Unlock
311
    G_UNLOCK(g_t_list);
312
}
313
314
static void timer_set_start_time() {
315
    // Set timer's start time
316
    time_t t = time(NULL);
317
    localtime_r(&t, &g_timer_start_time);
318
}
319
320
static struct tm timer_get_start_time() {
321
    return g_timer_start_time;
322
}
323
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
324
gdouble normalize_threshold(gdouble threshold, gchar *threshold_unit) {
325
    gdouble val = threshold;
326
    if (!threshold_unit) return val;
327
328
    // dB?
329
    if (threshold_unit[0] == 'd')  {
330
        // rms_dB:
1012.1.4 by David Rabel
Replaced http by https wherever it was reasonable. Also replaced some broken links and link that where only redirecting.
331
        // RMS, https://en.wikipedia.org/wiki/Root_mean_square
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
332
        // From dB to a normalized 0 - 1.0 value.
333
        val = pow(10, threshold / 20);
334
    }
335
    // [0 - 100]% ?
336
    else if (threshold_unit[0] == '%')  {
337
        val = val / 100.0;
338
463 by Osmo Antero
Updated README and INSTALL files.
339
        // Already in [0 - 1.0]
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
340
    } else {
341
        // val = threshold;
342
    }
343
344
    return val;
345
}
346
347
gboolean check_need_VAD() {
348
    GList *item = g_list_first(g_t_list);
349
    while (item) {
350
        TimerRec *tr = (TimerRec*)item->data;
351
352
        if (!g_strcmp0(tr->label, "silence") ||
463 by Osmo Antero
Updated README and INSTALL files.
353
                !g_strcmp0(tr->label, "voice") ||
354
                !g_strcmp0(tr->label, "sound") ||
355
                !g_strcmp0(tr->label, "audio")) {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
356
357
            return TRUE;
358
        }
359
360
        // Next item
361
        item = g_list_next(item);
362
    }
363
    return FALSE;
364
}
365
366
void normalize_values() {
367
    GList *item = g_list_first(g_t_list);
368
    while (item) {
369
        TimerRec *tr = (TimerRec*)item->data;
370
371
        // Convert hh:mm:ss to seconds
372
        tr->norm_secs = (gdouble)(tr->val[0]*3600 + tr->val[1]*60 + tr->val[2]);
373
374
        // Convert tr->threshold to [0 - 1.0] from tr->threshold_unit
375
        tr->norm_threshold = normalize_threshold(tr->threshold, tr->threshold_unit);
376
377
        // Next item
378
        item = g_list_next(item);
379
    }
380
}
381
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
382
static gchar highest_priority(gchar c1, gchar c2) {
383
    // sTop ha higher priority than Start, Continue or Pause
384
    // Start has higher priority than Pause or Continue
385
386
    if (c1 == 0 && c2 == 0) { // No action
387
        return 0;
388
    }
389
390
    if (c1 == 'T' || c2 == 'T') { // sTop
391
        return 'T';
392
    }
393
394
    if (c1 == 'S' || c2 == 'S') { // Start
395
        return 'S';
396
    }
463 by Osmo Antero
Updated README and INSTALL files.
397
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
398
    if (c1 == 'C' || c2 == 'C') { // Continue
399
        return 'C';
400
    }
463 by Osmo Antero
Updated README and INSTALL files.
401
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
402
    // Lowest priority Pause
403
    return 'P';
404
}
405
1 by Osmo Antero Maatta
Initial import 17.jan.2011
406
gboolean timer_func_cb(gpointer user_data) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
407
    // The actual timer function
1 by Osmo Antero Maatta
Initial import 17.jan.2011
408
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
409
    // Timer is ON/OFF?
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
410
    static gboolean timer_active = FALSE;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
411
412
    // Counter to detect if GConf settings have been altered
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
413
    static gint setting_counter = -1;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
414
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
415
    // Do we need VAD-pipeline?
416
    static gboolean need_VAD = FALSE;
417
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
418
    // Timer (GConf) settings changed?
1 by Osmo Antero Maatta
Initial import 17.jan.2011
419
    gint val = 0;
69 by Osmo Antero
Ported this application to GTK/GDK 3. Using now GSettings (and dconf) instead of GConf2
420
    conf_get_int_value("timer-setting-counter", &val);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
421
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
422
    if (val == setting_counter) {
423
        // No changes in parameters.
424
        // Evaluate timer values.
425
        goto EVAL_0;
426
    }
427
428
    // Save settings counter
429
    setting_counter = val;
430
431
    // Get new values from GConf and parse values
432
    conf_get_boolean_value("timer-active", &timer_active);
433
434
    LOG_TIMER("Timer settings changed:<%s>\n", (timer_active ? "timer ON" : "timer OFF"));
435
436
    // Timer is ON/OFF?
437
    if (!timer_active) {
438
        // It's OFF
439
440
        timer_clear_list();
441
442
        // Stop the listener
443
        vad_stop_VAD();
444
445
        goto LBL_1;
446
    }
463 by Osmo Antero
Updated README and INSTALL files.
447
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
448
    // Set timer's start time
449
    timer_set_start_time();
450
451
    // Free the old g_t_list
452
    timer_clear_list();
453
454
    // Set lock
455
    G_LOCK(g_t_list);
456
457
    // Get timer text
458
    gchar *timer_text = NULL;
459
    conf_get_string_value("timer-text", &timer_text);
460
461
    LOG_TIMER("----------------\nTimer text is:\n<%s>\n--------------\n", timer_text);
462
463
    // Parse timer conditions.
464
    // This will return pointer to the g_timer_list (GList) in timer-parser.c.
465
    g_t_list = parser_parse_actions(timer_text);
466
467
    g_free(timer_text);
468
469
    if (g_list_length(g_t_list) < 1) {
470
        LOG_TIMER("The timer has no conditions.\n");
471
472
    } else {
473
        LOG_TIMER("The timer conditions are:\n");
1 by Osmo Antero Maatta
Initial import 17.jan.2011
474
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
475
#if defined(DEBUG_TIMER)
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
476
        // Debug print the command list
477
        parser_print_list(g_t_list);
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
478
#endif
479
    }
480
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
481
    // Important: Start VAD-pipeline only when we needed.
482
    // Only "silence", "voice", "audio" and "sound" commands/conditions need VAD (Voice Activity Detection).
483
    need_VAD = check_need_VAD();
463 by Osmo Antero
Updated README and INSTALL files.
484
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
485
    // Normalize values
486
    normalize_values();
487
488
    G_UNLOCK(g_t_list);
489
463 by Osmo Antero
Updated README and INSTALL files.
490
EVAL_0:
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
491
175 by Osmo Antero
Minor changes towards v0.9.2.
492
    // Check if recorder was started with --debug-signal (or -d) argument
493
    need_VAD = need_VAD || vad_get_debug_flag();
494
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
495
    // Timer is ON?
496
    if (!timer_active) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
497
        // No.
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
498
        // Make sure the VAD has stopped (do not waste CPU cycles)
499
        vad_stop_VAD();
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
500
        goto LBL_1;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
501
    }
502
503
    // Yes. Timer is ON.
504
505
    // Do we need data from gst-vad.c?
506
    if (!need_VAD) {
507
        // No.
508
        // Make sure the VAD has stopped (do not waste CPU cycles)
509
        vad_stop_VAD();
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
510
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
511
    } else {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
512
        // Yes.
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
513
        // Start VAD (if not already running).
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
514
        vad_start_VAD();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
515
    }
516
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
517
    // ------------------------
518
    // Evaluate timer commands
519
    // ------------------------
49 by Osmo Antero Maatta
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)
520
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
521
    gchar saved_action = 0;
522
    TimerRec *saved_tr = NULL;
523
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
524
    // For all TimerRec structures in GList...
1 by Osmo Antero Maatta
Initial import 17.jan.2011
525
    GList *item = g_list_first(g_t_list);
526
    while (item) {
527
        TimerRec *tr = (TimerRec*)item->data;
528
51 by Osmo Antero Maatta
Formatted code with astyle (instructions about astyle in README)
529
        // Set lock
1 by Osmo Antero Maatta
Initial import 17.jan.2011
530
        G_LOCK(g_t_list);
531
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
532
        // Check the timer condition
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
533
        gchar c = timer_func_eval_command(tr);
463 by Osmo Antero
Updated README and INSTALL files.
534
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
535
        // Notice: The Timer list may have several commands like:
536
        //  start at 21:00
463 by Osmo Antero
Updated README and INSTALL files.
537
        //  stop at 21:30
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
538
        //  stop after 20MB | 09:20 pm | silence 4 sec 20%
539
        //
540
        // sTop ha higher priority than Start, Continue or Pause
541
        // Start has higher priority than Pause or Continue
542
        gchar highest = highest_priority(saved_action, c);
463 by Osmo Antero
Updated README and INSTALL files.
543
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
544
        if (highest == c || saved_action == 0) {
545
            // Save this action
546
            saved_action = c;
547
            saved_tr = tr;
548
        }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
549
550
        // Unlock
551
        G_UNLOCK(g_t_list);
552
553
        // Next item
554
        item = g_list_next(item);
555
    }
556
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
557
    // Execute timer command (if saved_action != 0)
558
    execute_action(saved_tr, saved_action);
559
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
560
LBL_1:
561
    // Continue calling this function
562
    return TRUE;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
563
}
564
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
565
gchar timer_func_eval_command(TimerRec *tr) {
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
566
    // Return action code: 'S'=Start, 'T'=sTop, 'P'=Pause, 'C'=Continue, 0=No action.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
567
    gchar action = 0;
568
569
    // Test filesize?
570
    if (tr->data_type == 'f') {
571
        // stop/pause if ### bytes/KB/MB/GB/TB
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
572
        // Example:
573
        // stop/pause if 250 MB
1 by Osmo Antero Maatta
Initial import 17.jan.2011
574
575
        action = timer_test_filesize(tr);
576
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
577
        if (action != 0) {
578
            LOG_TIMER("Filesize test is TRUE. Action is '%c' (%s).\n", action, parser_get_action_name(action));
1 by Osmo Antero Maatta
Initial import 17.jan.2011
579
        }
580
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
581
        // Test clock time ##:##:##?
1 by Osmo Antero Maatta
Initial import 17.jan.2011
582
    } else if (tr->data_type == 't') {
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
583
        // start/stop/pause at ##:##:## am/pm (where ##:##:## is a clock time in hh:mm:ss format)
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
584
        // Example:
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
585
        // start/stop/pause at 10:15:00 pm
1 by Osmo Antero Maatta
Initial import 17.jan.2011
586
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
587
        if (tr->action == 'S') { // 'S'tart
588
589
            action = timer_test_clock_time_S(tr);
590
591
        } else if (tr->action == 'T') { // s'T'op
592
593
            action = timer_test_clock_time_T(tr);
594
595
        } else if (tr->action == 'P') { // 'P'ause
463 by Osmo Antero
Updated README and INSTALL files.
596
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
597
            action = timer_test_clock_time_P(tr);
598
        }
1 by Osmo Antero Maatta
Initial import 17.jan.2011
599
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
600
        if (action != 0) {
601
            LOG_TIMER("Clock-time test is TRUE. Action is '%c' (%s).\n", action, parser_get_action_name(action));
1 by Osmo Antero Maatta
Initial import 17.jan.2011
602
        }
603
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
604
        // Test time duration?
1 by Osmo Antero Maatta
Initial import 17.jan.2011
605
    } else if (tr->data_type == 'd') {
606
        // start/stop/pause after # hour # min # seconds
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
607
        // Example:
608
        // start/stop/pause after 1 h 25 min
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
609
1 by Osmo Antero Maatta
Initial import 17.jan.2011
610
        action = timer_test_time_duration(tr);
611
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
612
        if (action != 0) {
613
            LOG_TIMER("Test for time period/duration is TRUE. Action is '%c' (%s).\n", action, parser_get_action_name(action));
1 by Osmo Antero Maatta
Initial import 17.jan.2011
614
        }
615
    }
616
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
617
    return action;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
618
}
619
620
static gchar timer_test_filesize(TimerRec *tr) {
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
621
    // Test filesize.
622
    // stop/pause if/after/on ### bytes/KB/MB/GB/TB
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
623
    // Examples:
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
624
    //  stop after 250MB
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
625
    //  pause if 1.2 GB
1 by Osmo Antero Maatta
Initial import 17.jan.2011
626
627
    gchar action = 0;
628
629
    // Get output filename
630
    gchar *filename = rec_manager_get_output_filename();
631
632
    if (!filename) {
633
        return action;
634
    }
635
636
    // Get file size
637
    gdouble filesize = 1.0 * get_file_size(filename);
638
639
    g_free(filename);
640
641
    // Filesize limit exceeded?
642
    if (filesize >= tr->val[0]) {
643
        // Execute
644
        action = tr->action;
645
    }
646
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
647
    LOG_TIMER("Testing filesize: trigger filesize=%3.1f bytes, unit=%s, current filesize=%3.1f bytes, filename=<%s>, -->%s\n",
648
              tr->val[0], tr->label, filesize, filename, (action == 0 ? "FALSE" : "TRUE"));
649
1 by Osmo Antero Maatta
Initial import 17.jan.2011
650
    return action;
651
}
652
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
653
#if 0
654
static TimerRec *find_action(gchar action) {
655
    // Find latest TimeRec record for action.
463 by Osmo Antero
Updated README and INSTALL files.
656
    TimerRec *found_tr = NULL;
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
657
658
    // Try to set lock
659
    gboolean locked = G_TRYLOCK(g_t_list);
660
661
    // For all TimerRec structures in GList...
662
    GList *item = g_list_first(g_t_list);
663
    while (item) {
664
        TimerRec *tr = (TimerRec*)item->data;
665
666
        if (action == tr->action) {
667
668
            switch (tr->data_type) {
669
670
            case 't':
671
                // Take the one with latest timestamp of found_tr and tr.
672
673
                if (!found_tr) {
674
                    // First match
675
                    found_tr = tr;
676
                } else {
677
678
                    // Compare timestamps of found_tr and tr.
679
                    gint64 timer_secs = tr->val[0]*3600 +  tr->val[1]*60 +  tr->val[2];
680
                    if (timer_secs > found_tr->norm_secs) {
681
                        found_tr = tr;
682
                    }
463 by Osmo Antero
Updated README and INSTALL files.
683
                }
684
685
                break;
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
686
687
            case 'd':
688
            case 'f':
463 by Osmo Antero
Updated README and INSTALL files.
689
                if (!found_tr) {
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
690
                    found_tr = tr;
691
                }
463 by Osmo Antero
Updated README and INSTALL files.
692
                break;
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
693
694
            }
695
        }
696
697
        // Next item
698
        item = g_list_next(item);
699
    }
700
701
    if (locked) {
702
        // Unlock
703
        G_UNLOCK(g_t_list);
704
    }
705
706
    return found_tr;
707
}
708
#endif
709
710
static gchar timer_test_clock_time_S(TimerRec *tr) {
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
711
    // Test clock time for 'S'tart recording.
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
712
    // start at ##:##:## am/pm (where ##:##:## is a clock time in hh:mm:ss format)
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
713
    // Examples:
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
714
    //  start at 10:15:00 pm
463 by Osmo Antero
Updated README and INSTALL files.
715
    //  start after 1 hour
716
1 by Osmo Antero Maatta
Initial import 17.jan.2011
717
    gchar action = 0;
718
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
719
    // Get date & time
1 by Osmo Antero Maatta
Initial import 17.jan.2011
720
    time_t t = time(NULL);
721
    struct tm *tmp;
722
    tmp = localtime(&t);
723
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
724
    // Timer command already executed today?
725
    if (tr->day_of_year == tmp->tm_yday) {
726
        // Fire only once a day
727
463 by Osmo Antero
Updated README and INSTALL files.
728
        LOG_TIMER("Timer command 'S'tart already executed today. Current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d).\n",
729
                  tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday, tr->day_of_year);
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
730
731
        return 0;
732
    }
733
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
734
    // Clock time in secs
1 by Osmo Antero Maatta
Initial import 17.jan.2011
735
    gint64 clock_secs = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
736
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
737
    // TimeRec's value in secs
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
738
    gint64 timer_secs = tr->norm_secs;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
739
12 by Osmo Antero Maatta
Better test for clock-time in timer.c
740
    // Note:
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
741
    // Do NOT fire if current clock time is 60 minutes or more over the timer value.
742
    // Eg. Assume the timer value is set to 14:00h:
743
    //     We start recording if clock time is between 14:00:01h and 15:00h. (1 hour over timer value).
744
    //     After 15:00h the timer must wait until next day.
12 by Osmo Antero Maatta
Better test for clock-time in timer.c
745
746
    gint64 diff_secs = (clock_secs - timer_secs);
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
747
748
    action = 0;
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
749
    if (clock_secs > timer_secs && diff_secs < (60*60L)/*1 HOUR HARD-CODED*/) {
463 by Osmo Antero
Updated README and INSTALL files.
750
        // Start-time is over current clock time.
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
751
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
752
        // Already fired today? (fire once a day)
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
753
        if (tr->day_of_year != tmp->tm_yday) {
754
            action = 'S';
755
        }
756
    }
463 by Osmo Antero
Updated README and INSTALL files.
757
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
758
    if (action) {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
759
        // Save day_of_year so we know when the clock turns around (to the next day).
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
760
        // Then this timer command will become valid and fire again.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
761
        tr->day_of_year = tmp->tm_yday;
762
    }
763
463 by Osmo Antero
Updated README and INSTALL files.
764
    LOG_TIMER("Test clock time for 'S'tart: current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d) diff in secs:%ld, -->%s\n",
765
              tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday,
766
              tr->day_of_year, (long)(timer_secs - clock_secs), (action == 0 ? "FALSE" : "TRUE"));
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
767
1 by Osmo Antero Maatta
Initial import 17.jan.2011
768
    return action;
769
}
770
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
771
static gchar timer_test_clock_time_T(TimerRec *tr) {
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
772
    // Test clock time for sTop recording.
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
773
    // stop at ##:##:## am/pm (where ##:##:## is a clock time in hh:mm:ss format)
774
    // Examples:
775
    //  stop at 23:00
776
    //  stop at 09:10 am
777
778
    gchar action = 0;
779
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
780
    // Get date & time
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
781
    time_t t = time(NULL);
782
    struct tm *tmp;
783
    tmp = localtime(&t);
784
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
785
    // Timer command already executed today?
786
    if (tr->day_of_year == tmp->tm_yday) {
787
        // Fire only once a day
788
463 by Osmo Antero
Updated README and INSTALL files.
789
        LOG_TIMER("Timer command s'T'op already executed today. Current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d).\n",
790
                  tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday, tr->day_of_year);
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
791
792
        return 0;
793
    }
794
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
795
    // Clock time in secs
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
796
    gint64 clock_secs = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
797
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
798
    // TimeRec's value in seconds
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
799
    gint64 timer_secs = tr->norm_secs;
800
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
801
    // Check if clock-time is over stop-time
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
802
    action = 0;
803
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
804
    if (clock_secs > timer_secs) {
463 by Osmo Antero
Updated README and INSTALL files.
805
        // FIXME: Should we check tr->day_of_year != tmp->tm_yday here?
806
        // if (timer_secs > clock_secs && tr->day_of_year != tmp->tm_yday) {
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
807
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
808
        // Already fired today? (fire once a day)
809
        if (tr->day_of_year != tmp->tm_yday) {
810
            action = 'T';
811
            tr->day_of_year = tmp->tm_yday;
812
        }
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
813
    }
814
463 by Osmo Antero
Updated README and INSTALL files.
815
    LOG_TIMER("Test clock time for s'T'op: current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d) diff in secs:%ld, -->%s\n",
816
              tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday,
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
817
              tr->day_of_year, (long)(timer_secs - clock_secs), (action == 0 ? "FALSE" : "TRUE"));
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
818
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
819
    return action;
820
}
821
822
static gchar timer_test_clock_time_P(TimerRec *tr) {
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
823
    // Test clock time for Pause recording.
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
824
    // pause at ##:##:## am/pm (where ##:##:## is a clock time in hh:mm:ss format)
825
    // Examples:
826
    //  pause at 23:00
827
    //  pause at 09:10 am
828
829
    gchar action = 0;
830
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
831
    // Get date & time
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
832
    time_t t = time(NULL);
833
    struct tm *tmp;
834
    tmp = localtime(&t);
835
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
836
    // Timer command already executed today?
837
    if (tr->day_of_year == tmp->tm_yday) {
838
        // Fire only once a day
839
463 by Osmo Antero
Updated README and INSTALL files.
840
        LOG_TIMER("Timer command 'P'ause already executed today. Current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d).\n",
841
                  tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday, tr->day_of_year);
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
842
843
        return 0;
844
    }
845
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
846
    // Clock time in secs
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
847
    gint64 clock_secs = tmp->tm_hour*3600 + tmp->tm_min*60 + tmp->tm_sec;
848
456 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
849
    // TimerRec's time in secs
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
850
    gint64 timer_secs = tr->norm_secs;
851
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
852
    // Check if clock-time is over pause-time
853
    if (clock_secs > timer_secs) {
463 by Osmo Antero
Updated README and INSTALL files.
854
        // FIXME: Should we check tr->day_of_year != tmp->tm_yday here?
855
        // if (timer_secs > clock_secs && tr->day_of_year != tmp->tm_yday) {
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
856
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
857
        // Already fired today? (fire once a day)
858
        if (tr->day_of_year != tmp->tm_yday) {
859
            action = 'P';
860
            tr->day_of_year = tmp->tm_yday;
861
        }
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
862
    }
863
463 by Osmo Antero
Updated README and INSTALL files.
864
    LOG_TIMER("Test clock time for 'P'ause: current time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f (day_of_year:%d/%d) diff in secs:%ld, -->%s\n",
865
              tmp->tm_hour, tmp->tm_min, tmp->tm_sec,  tr->val[0], tr->val[1], tr->val[2], tmp->tm_yday,
866
              tr->day_of_year, (long)(timer_secs - clock_secs), (action == 0 ? "FALSE" : "TRUE"));
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
867
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
868
    return action;
869
}
870
1 by Osmo Antero Maatta
Initial import 17.jan.2011
871
static gchar timer_test_time_duration(TimerRec *tr) {
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
872
    // Test time duration/time period.
873
    // start/stop/pause after # hour/h # minuntes/m/min # seconds/s/sec
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
874
    // Examples:
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
875
    //  start after 1 h 25 min
876
    //  stop after 30 minutes
877
    //  pause after 1 h 15 m 20 s
1 by Osmo Antero Maatta
Initial import 17.jan.2011
878
879
    gchar action = 0;
880
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
881
    // Get date & time
882
    time_t t = time(NULL);
883
    struct tm *tmp;
884
    tmp = localtime(&t);
885
886
    // Action is s'T'op or 'P'ause recording?
1 by Osmo Antero Maatta
Initial import 17.jan.2011
887
    if (tr->action == 'T'  || tr->action == 'P') {
888
        // Eg. stop/pause after 8 min 20 sec
889
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
890
        // Compare stream time to the given timer value.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
891
892
        // Get actual recording time in seconds
893
        gint64 recording_time_secs = rec_manager_get_stream_time();
894
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
895
        // TimeRec's value in seconds
896
        gint64 timer_secs = tr->norm_secs;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
897
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
898
        if (recording_time_secs >= timer_secs) {
899
            // Execute command
900
            action = tr->action;
901
902
            // tr->day_of_year not really used here
903
            tr->day_of_year = tmp->tm_yday;
904
        }
905
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
906
#if defined(DEBUG_TIMER) || defined(DEBUG_ALL)
907
        guint hh = -1;
908
        guint mm = -1;
909
        guint ss = -1;
910
        // Split value to hours, minutes and seconds
911
        seconds_to_h_m_s(recording_time_secs, &hh, &mm, &ss);
9 by Osmo Antero Maatta
Cleaning up the rec_stop_recording() code + other improvements
912
25 by Osmo Antero Maatta
Fixed crash on Fedora 14. LOG_TIMER(...) in timer.c caused crash when gint64 was formatted with %ld. Now I cast everything to long.
913
        gint64 diff = timer_secs - recording_time_secs;
914
463 by Osmo Antero
Updated README and INSTALL files.
915
        LOG_TIMER("Test time period for '%c' (%s): current rec.time:%02d:%02d:%02d timer value:%02.0f:%02.0f:%02.0f diff secs:%ld, -->%s.\n",
916
                  tr->action, parser_get_action_name(tr->action),
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
917
                  hh, mm, ss, tr->val[0], tr->val[1], tr->val[2], (long)diff, (action == 0 ? "FALSE" : "TRUE"));
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
918
#endif
1 by Osmo Antero Maatta
Initial import 17.jan.2011
919
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
920
    } else {
921
922
        action = 0;
923
924
        // Action is 'S'tart recording?
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
925
        // Eg. start after 1 min 20 sec (execute "start after..." command only once during timer's life time)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
926
9 by Osmo Antero Maatta
Cleaning up the rec_stop_recording() code + other improvements
927
        // Get start time for this timer (when lines were parsed)
1 by Osmo Antero Maatta
Initial import 17.jan.2011
928
        struct tm start_time = timer_get_start_time();
929
        gint64 start_time_secs = start_time.tm_hour*3600.0 + start_time.tm_min*60.0 + start_time.tm_sec;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
930
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
931
        // Command already executed? (we use -2 as "execute once" indicator)
932
        if (tr->day_of_year  == -2) {
933
934
            // Fire only once a day
935
            LOG_TIMER("Timer command 'S'tart already executed once!: clock time:%02d:%02d:%02d timer thread started:%02d:%02d:%02d"
463 by Osmo Antero
Updated README and INSTALL files.
936
                      " timer value (duration):%02.0f:%02.0f:%02.0f.\n",
937
                      tmp->tm_hour, tmp->tm_min, tmp->tm_sec, start_time.tm_hour, start_time.tm_min, start_time.tm_sec,
938
                      tr->val[0], tr->val[1], tr->val[2]);
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
939
940
            return 0;
941
        }
942
463 by Osmo Antero
Updated README and INSTALL files.
943
        // Clock time in secs
1 by Osmo Antero Maatta
Initial import 17.jan.2011
944
        gint64 curr_time_secs = tmp->tm_hour*3600.0 + tmp->tm_min*60.0 + tmp->tm_sec;
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
945
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
946
        // TimerRec's value in seconds
947
        gint64 timer_secs = tr->norm_secs;
948
949
        if ((curr_time_secs - start_time_secs) >= timer_secs ) {
950
            // Execute command
951
            action = tr->action;
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
952
953
            // Execute only once (we use -2 as "execute once" indicator)
954
            tr->day_of_year = -2;
955
            // tr->day_of_year = tmp->tm_yday;
455 by Osmo Antero
Version 1.5-9 (pre v1.6-0 for Ubuntu 15.04). Clean ups.
956
        }
9 by Osmo Antero Maatta
Cleaning up the rec_stop_recording() code + other improvements
957
25 by Osmo Antero Maatta
Fixed crash on Fedora 14. LOG_TIMER(...) in timer.c caused crash when gint64 was formatted with %ld. Now I cast everything to long.
958
        gint64 diff = timer_secs - (curr_time_secs - start_time_secs);
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
959
        (void) diff; // Avoid unused var message
40 by Osmo Antero Maatta
Added ability to record from multiple devices. Major changes in all modules.
960
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
961
        LOG_TIMER("Test time period for 'S'tart: clock time:%02d:%02d:%02d timer thread started:%02d:%02d:%02d"
463 by Osmo Antero
Updated README and INSTALL files.
962
                  " timer value (duration):%02.0f:%02.0f:%02.0f  diff secs:%ld, -->%s\n",
461 by Osmo Antero
Minor changes in timer-parser.c and timer.c.
963
                  tmp->tm_hour, tmp->tm_min, tmp->tm_sec, start_time.tm_hour, start_time.tm_min, start_time.tm_sec,
964
                  tr->val[0], tr->val[1], tr->val[2], (long)diff, (action == 0 ? "FALSE" : "TRUE"));
1 by Osmo Antero Maatta
Initial import 17.jan.2011
965
966
    }
967
968
    return action;
969
}
970
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
971
void timer_evaluate_triggers(GstClockTimeDiff time_diff, gdouble rms) {
972
    // This is called from gst-vad.c.
973
    // Evaluate VAD related timer commands.
974
    G_LOCK(g_t_list);
975
976
    GList *item = g_list_first(g_t_list);
977
    while (item) {
978
        TimerRec *tr = (TimerRec*)item->data;
979
980
        if (!g_strcmp0(tr->label, "silence")) {
981
982
            test_silence(tr, time_diff, rms);
983
984
        } else if (!g_strcmp0(tr->label, "voice") ||
985
                   !g_strcmp0(tr->label, "sound") ||
986
                   !g_strcmp0(tr->label, "audio")) {
987
988
            test_sound(tr, time_diff, rms);
989
        }
990
991
        // Next item
992
        item = g_list_next(item);
993
    }
994
995
    G_UNLOCK(g_t_list);
996
}
997
998
static void test_silence(TimerRec *tr, GstClockTimeDiff time_diff, gdouble rms) {
999
    // stop if silence
1000
    // stop if silence 5s
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1001
    // stop if silence 5s 0.1
1002
    // stop if silence 5s 10%
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1003
1004
    // pause if silence
1005
    // pause if silence 5s 0.3
1006
    // pause if silence 5s 30%
1007
    // pause if silence 5s -24dB
183 by osmoma at gmail
Fixed error in src/timer-parser.c
1008
    gchar action = '0';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1009
1010
    gdouble seconds = tr->norm_secs;
1011
1012
#if defined(DEBUG_TIMER) || defined(DEBUG_ALL)
1013
    // Get recording state (for debugging)
1014
    gint state = -1;
1015
    gint pending = -1;
1016
    rec_manager_get_state(&state, &pending);
1017
1018
    // Name of state
1019
    const gchar *state_name = rec_manager_get_state_name(state);
1020
1021
    LOG_TIMER("Silence test. timer value:%3.1f sec, count seconds:%3.1f sec, *RMS:%3.2f, threshold:%3.1f%s (%3.2f), state:%s\n",
463 by Osmo Antero
Updated README and INSTALL files.
1022
              seconds, tr->time_below, rms, tr->threshold, (tr->threshold_unit ? tr->threshold_unit : ""), tr->norm_threshold, state_name);
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1023
#endif
1024
1025
    // RMS > tr->threshold?
1026
    if (rms > tr->norm_threshold + 0.001) { // 0.001=noise
1027
        // Reset time teller
1028
        tr->time_below = 0.0;
1029
1030
        if (tr->action == 'P') { // Pause
1031
            // Resume (continue) recording after pause
463 by Osmo Antero
Updated README and INSTALL files.
1032
            action = 'C';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1033
            goto LBL_1;
1034
        }
1035
        return;
1036
    }
1037
1038
    // Add time_diff to tr->time_below, convert to seconds
1039
    tr->time_below += ((gdouble)time_diff / GST_SECOND);
1040
1041
    // tr->time_below < seconds?
1042
    if (tr->time_below < seconds) {
1043
        // Wait more
1044
        return;
1045
    }
1046
1047
    // Avoid overflow (no reason to keep this growing)
1048
    if (tr->time_below > seconds + 140000) {
1049
        tr->time_below = seconds + 140000;
1050
    }
1051
1052
    // If here: RMS has been <= tr->threshold in seconds time.
1053
    // Execute timer command.
1054
1055
    if (tr->action == 'T') { // sTop
1056
        // stop if silence
1057
        // stop if silence 0.3 4s
1058
        action = 'T';
1059
1060
    } else if (tr->action == 'P') { // Pause
1061
        // pause if silence 3s 0.2
1062
        action = 'P';
1063
    }
1064
1065
    LOG_TIMER("Condition %3.2f <= %3.2f (%3.2f%s) is TRUE in %3.1f seconds time. Execute command:%s.\n",
1066
              rms, tr->norm_threshold, tr->threshold, (tr->threshold_unit ? tr->threshold_unit : ""),
1067
              seconds, parser_get_action_name(action));
1068
463 by Osmo Antero
Updated README and INSTALL files.
1069
LBL_1:
1070
    // Exceute action
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1071
    execute_action(tr, action);
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1072
1073
    // Reset counters
1074
    tr->time_below = 0.0;
1075
    tr->time_above = 0.0;
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1076
}
1077
1078
static void test_sound(TimerRec *tr, GstClockTimeDiff time_diff, gdouble rms) {
1079
    // start if sound
1080
    // start if sound 0.3
1081
    // start if voice 30%
1082
    // start if voice 0.3
1083
    // start if audio -20dB
183 by osmoma at gmail
Fixed error in src/timer-parser.c
1084
    gchar action = '0';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1085
1086
    gdouble seconds = tr->norm_secs;
1087
1088
    gint state = -1;
1089
    gint pending = -1;
1090
    rec_manager_get_state(&state, &pending);
1091
1092
#if defined(DEBUG_TIMER) || defined(DEBUG_ALL)
1093
    // Name of state
1094
    const gchar *state_name = rec_manager_get_state_name(state);
1095
1096
    LOG_TIMER("Sound/Voice/Audio test. timer value:%3.1f sec, count seconds:%3.1f sec, *RMS:%3.2f, threshold:%3.1f%s (%3.2f), state:%s\n",
463 by Osmo Antero
Updated README and INSTALL files.
1097
              seconds, tr->time_above, rms, tr->threshold, (tr->threshold_unit ? tr->threshold_unit : ""), tr->norm_threshold, state_name);
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1098
#endif
1099
1100
    // rms over threshold?
1101
    if (rms > tr->norm_threshold + 0.001) { // 0.001=noise
1102
1103
        // Paused temporarily?
1104
        if (state == GST_STATE_PAUSED) {
1105
            // Resume/continue recording immediately
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
1106
            action = 'C';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1107
            tr->time_above = 0.0;
1108
            tr->time_below = 0.0;
1109
            goto LBL_1;
1110
        }
463 by Osmo Antero
Updated README and INSTALL files.
1111
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1112
        // Add time_diff to tr->time_above, convert to seconds
1113
        tr->time_above += ((gdouble)time_diff / GST_SECOND);
1114
1115
        // tr->time_above < seconds?
1116
        if (tr->time_above < seconds) {
1117
            // Wait more
1118
            return;
1119
        }
1120
1121
        // Avoid overflow (no reason to keep this growing)
1122
        if (tr->time_above > seconds + 140000) {
1123
            tr->time_above = seconds + 140000;
1124
        }
1125
1126
        tr->time_below = 0.0;
1127
1128
        // If here: RMS has been > tr->threshold in seconds time.
1129
        // Execute timer command.
1130
        action = tr->action;
1131
1132
        LOG_TIMER("Condition %3.2f > %3.2f (%3.2f%s) is TRUE in %3.1f seconds time. Execute command:%s.\n",
1133
                  rms, tr->norm_threshold, tr->threshold, (tr->threshold_unit ? tr->threshold_unit : ""),
1134
                  seconds, parser_get_action_name(action));
1135
1136
        goto LBL_1;
1137
    }
1138
1139
    // Here: rms < tr->norm_threshold.
1140
1141
    // Count seconds to pause
1142
    tr->time_below += ((gdouble)time_diff / GST_SECOND);
1143
1144
    if (tr->time_below < 4.0) {
1145
        // Wait more
1146
        return;
1147
    }
1148
1149
    // Pause recording temporarily
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
1150
    action = 'P';
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1151
    tr->time_above = 0.0;
1152
    tr->time_below = 0.0;
1153
463 by Osmo Antero
Updated README and INSTALL files.
1154
LBL_1:
1155
    // Excecute action
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1156
    execute_action(tr, action);
1157
1158
}
1159
1160
void execute_action(TimerRec *tr, gchar action) {
1161
    // Execute timer command
1162
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
1163
    if (action == 0 || tr == NULL) return;
183 by osmoma at gmail
Fixed error in src/timer-parser.c
1164
453 by Osmo Antero
Some changes in timer.c. Handle multiple timer commands better. Needs testing.
1165
    LOG_TIMER("Execute timer command '%c' (%s).\n", action, parser_get_action_name(action));
1166
349 by Osmo Antero
Version 1.4-2. Ref. question #241801 Timer.c fails to start and stop recording.
1167
    // Get state of pipeline
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1168
    gint state = -1;
1169
    gint pending = -1;
1170
    rec_manager_get_state(&state, &pending);
1171
1172
    switch (action) {
1173
    case 'S': // Start recording
349 by Osmo Antero
Version 1.4-2. Ref. question #241801 Timer.c fails to start and stop recording.
1174
        if (state == GST_STATE_PLAYING) {
1175
            return;
1176
        }
1177
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1178
        rec_manager_start_recording();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1179
        break;
1180
1181
    case 'T': // Stop recording
349 by Osmo Antero
Version 1.4-2. Ref. question #241801 Timer.c fails to start and stop recording.
1182
1183
        if (state == GST_STATE_NULL && pending == GST_STATE_NULL) {
1184
            return;
1185
        }
1186
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1187
        rec_manager_stop_recording();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1188
        break;
1189
1190
    case 'P': // Pause recording
349 by Osmo Antero
Version 1.4-2. Ref. question #241801 Timer.c fails to start and stop recording.
1191
1192
        if (state == GST_STATE_PAUSED) {
1193
            return;
1194
        }
1195
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1196
        rec_manager_pause_recording();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1197
        break;
1198
1199
    case 'p': // Pause recording
349 by Osmo Antero
Version 1.4-2. Ref. question #241801 Timer.c fails to start and stop recording.
1200
1201
        if (state == GST_STATE_PAUSED) {
1202
            return;
1203
        }
1204
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1205
        rec_manager_pause_recording();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1206
        break;
1207
451 by Osmo Antero
Some improvements in timer.c. Check priority if timer-text has several commands (that can fire). This needs more thinking and testing. Work continues.
1208
    case 'C': // Continue/resume (if was paused). Does not restart if recording is off.
344 by Osmo Antero
Bug #1258822 Timer.c does not stop recording in some rare cases.
1209
        rec_manager_continue_recording();
173 by Osmo Antero
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.
1210
        break;
1211
1212
    case 0: // Do nothing
1213
        break;
1214
1215
    default:
1216
        LOG_ERROR("Unknown timer action <%c>.\n", action);
1217
    }
1218
}
1219