~audio-recorder/audio-recorder/trunk

« back to all changes in this revision

Viewing changes to src/timer.c

  • Committer: Osmo Antero Maatta
  • Date: 2011-03-16 09:02:39 UTC
  • Revision ID: osmoma@gmail.com-20110316090239-jqygbkyzkjx0xdd5
Employ listener (in gst-listener.c) ONLY when needed. Also listener process was created 2 times (was unstoppable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
// Default RMS level for "silence" and "sound", "voice", "audio".
42
42
#define DEF_RMS_LEVEL 7 // 7% (ca. -24dB)
43
43
 
44
 
 
45
44
// Timer function id
46
45
static guint g_timer_func_id = 0;
47
46
 
72
71
void timer_module_init() {
73
72
    LOG_DEBUG("Init timer.c.\n");
74
73
 
 
74
        // Init gst-listener.c
 
75
    listener_module_init();
 
76
 
75
77
    g_timer_func_id = 0;
76
78
 
77
79
    // Init parser module
90
92
    // Clean up parser module
91
93
    parser_module_exit();
92
94
 
 
95
        // Clean up gst-listener.c
 
96
    listener_module_exit();
 
97
 
93
98
    g_t_list = NULL;
94
99
}
95
100
 
 
101
void timer_set_debug_flag(gboolean on_off) {
 
102
    // Set debug flag. Please see application options: 
 
103
        // $ audio-recorder --help
 
104
        listener_set_debug_flag(on_off);
 
105
}
 
106
 
96
107
void timer_module_reset(gint for_state) {
97
 
    // Reset timer for the given state (before gst-recorder moves to new state)
98
 
 
99
 
    /*
100
 
        stop if silence 5s 4%
101
 
 
102
 
        pause if silence
103
 
 
104
 
       start if sound
105
 
 
106
 
    */
 
108
    // Reset timer before we move to the given state
107
109
 
108
110
    switch (for_state) {
109
111
    case GST_STATE_PLAYING:
135
137
// --------------------------------------------------
136
138
 
137
139
void timer_func_start() {
138
 
 
139
140
    // Already running?
140
141
    if (g_timer_func_id > 0) {
141
142
        return;
166
167
}
167
168
 
168
169
static void timer_update_records_1() {
 
170
        // Reset timer nodes
 
171
 
169
172
    G_LOCK(g_t_list);
170
173
 
171
174
    GList *item = g_list_first(g_t_list);
186
189
}
187
190
 
188
191
static void timer_update_records_2() {
 
192
        // Reset timer nodes
 
193
 
189
194
    G_LOCK(g_t_list);
190
195
 
191
196
    GList *item = g_list_first(g_t_list);
229
234
gboolean timer_func_cb(gpointer user_data) {
230
235
    // The actual timer function
231
236
 
232
 
    // Timer ON/OFF?
 
237
    // Timer is ON/OFF?
233
238
    static gboolean timer_active = FALSE;
234
239
 
 
240
        // Do we need data from the gst-listener.c?
 
241
        static gboolean need_listener = FALSE;
 
242
 
235
243
    // Counter to detect if GConf settings have been altered
236
244
    static gint setting_counter = -1;
237
245
 
267
275
            // Free the old g_t_list
268
276
            timer_clear_list();
269
277
 
270
 
            // Get new timer (action/command) list.
 
278
                        // Set lock
271
279
            G_LOCK(g_t_list);
272
280
 
273
 
            // Get timer commands/text
 
281
            // Get timer text
274
282
            gchar *timer_text = NULL;
275
283
            conf_get_string_value("timer_text", &timer_text);
276
284
 
277
285
            LOG_TIMER("----------------\nTimer text is:\n<%s>\n--------------\n", timer_text);
278
286
 
279
 
            // Note: This returns pointer to the g_timer_list in timer-parser.c.
 
287
                        // Parse timer conditions.
 
288
            // This will return pointer to the g_timer_list (GList) in timer-parser.c.
280
289
            g_t_list = parser_parse_actions(timer_text);
281
290
 
282
291
            g_free(timer_text);
284
293
            if (g_list_length(g_t_list) < 1) {
285
294
                LOG_TIMER("The timer has no conditions.\n");
286
295
 
287
 
                // Timer has no conditions. Switch off listener.
288
 
                timer_active = FALSE;
289
 
 
290
296
            } else {
291
 
 
292
297
                LOG_TIMER("The timer conditions are:\n");
293
298
 
294
 
#if defined(DEBUG_TIMER)
295
 
                // Debug print the command list
296
 
                parser_print_list(g_t_list);
297
 
#endif
 
299
                                #if defined(DEBUG_TIMER)
 
300
                        // Debug print the command list
 
301
                        parser_print_list(g_t_list);
 
302
                                #endif
298
303
            }
299
304
 
 
305
                        // Important: Start the listener only when we need it. It's CPU intensive.
 
306
                        // Only "silence", "voice", "audio" and "sound" commands need data from the listener.
 
307
 
 
308
                        need_listener = FALSE;
 
309
 
 
310
                        GList *item = g_list_first(g_t_list);
 
311
                        while (item) {
 
312
                                TimerRec *tr = (TimerRec*)item->data;
 
313
 
 
314
                                if (!g_strcmp0(tr->label, "silence") ||
 
315
                                        !g_strcmp0(tr->label, "voice") ||
 
316
                                        !g_strcmp0(tr->label, "sound") ||
 
317
                                        !g_strcmp0(tr->label, "audio")) {
 
318
 
 
319
                                        need_listener = TRUE;
 
320
                                        break;
 
321
                                }
 
322
 
 
323
                                // Next item
 
324
                                item = g_list_next(item);
 
325
                        }
 
326
 
 
327
                        // Unlock
300
328
            G_UNLOCK(g_t_list);
301
329
        }
302
 
    }
303
 
 
304
 
    gboolean active = timer_active;
305
 
 
306
 
    // Execute timer commands?
307
 
    if (!active) {
 
330
        }
 
331
                
 
332
 
 
333
    // Timer is ON?
 
334
    if (!timer_active) {
308
335
        // No.
309
336
        // Make sure the listener has stopped (do not waste CPU cycles)
310
337
        listener_stop_listening();
311
 
 
312
338
        goto LBL_1;
 
339
 
313
340
    } else {
314
 
 
315
 
        // Start the listener and collect signal/level statistics
316
 
        listener_start_listening();
 
341
                // Yes. Timer is ON.
 
342
 
 
343
                // Do we need data from gst-listener.c?
 
344
                if (!need_listener) {
 
345
                        // No.
 
346
                // Make sure the listener has stopped (do not waste CPU cycles)
 
347
                listener_stop_listening();
 
348
 
 
349
                } else {
 
350
                        // Yes.
 
351
                    // Start the listener and collect signal/level statistics
 
352
                    listener_start_listening();
 
353
                }
317
354
    }
318
355
 
 
356
 
319
357
    // Execute the TimerRec commands. For all TimerRec structures in GList...
320
358
    GList *item = g_list_first(g_t_list);
321
359
    while (item) {
322
360
        TimerRec *tr = (TimerRec*)item->data;
323
361
 
324
 
        // Lock the list
 
362
        // Set lock 
325
363
        G_LOCK(g_t_list);
326
364
 
327
365
        // Check the timer condition
356
394
            LOG_TIMER("Filesize test = TRUE.\n");
357
395
        }
358
396
 
359
 
        // Test for silence (if average audio signal is under givel level)?
 
397
        // Test for silence (if average audio signal is under givel level)?
360
398
    } else if (!g_strcmp0(tr->label, "silence")) {
361
399
        // Examples:
362
400
        // stop/pause if silence 8s -26 dB
363
401
        // stop/pause if silence 8 sec 7 %
364
 
        // = start/stop/pause recording if sound level is under -26dB (ca.7%) in 8 seconds time.
 
402
        // = start/stop/pause recording if sound level is under x dB in n seconds time.
365
403
 
366
404
        action = timer_test_silence(tr);
367
405
 
371
409
            LOG_TIMER("Silence test = TRUE.\n");
372
410
        }
373
411
 
374
 
        // Test for voice/audio/sound?
 
412
        // Test for voice/audio/sound?
375
413
    } else if (!g_strcmp0(tr->label, "voice") ||
376
414
               !g_strcmp0(tr->label, "sound") ||
377
415
               !g_strcmp0(tr->label, "audio")) {
378
416
 
379
417
        // start/stop/pause if voice/audio/sound 8s 7%
380
418
        // start/stop/pause if voice/audio/sound 8s -26dB
381
 
        // = start/stop/pause recording if voice/audio/sound is above 7% (ca.-26dB) in 8 seconds period.
 
419
        // = start/stop/pause recording if voice/audio/sound is above x dB in n seconds period.
382
420
 
383
421
        action = timer_test_sound(tr);
384
422