]> sigrok.org Git - sigrok-cli.git/blob - session.c
Force backup analog traces only if needed
[sigrok-cli.git] / session.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <glib.h>
22 #include <glib/gstdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "sigrok-cli.h"
26
27 static uint64_t limit_samples = 0;
28 static uint64_t limit_frames = 0;
29
30 #ifdef HAVE_SRD
31 extern struct srd_session *srd_sess;
32 #endif
33
34 static int set_limit_time(const struct sr_dev_inst *sdi)
35 {
36         GVariant *gvar;
37         uint64_t time_msec;
38         uint64_t samplerate;
39         struct sr_dev_driver *driver;
40
41         driver = sr_dev_inst_driver_get(sdi);
42
43         if (!(time_msec = sr_parse_timestring(opt_time))) {
44                 g_critical("Invalid time '%s'", opt_time);
45                 return SR_ERR;
46         }
47
48         if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_LIMIT_MSEC)
49                         & SR_CONF_SET) {
50                 gvar = g_variant_new_uint64(time_msec);
51                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
52                         g_critical("Failed to configure time limit.");
53                         return SR_ERR;
54                 }
55         } else if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_SAMPLERATE)
56                         & (SR_CONF_GET | SR_CONF_SET)) {
57                 /* Convert to samples based on the samplerate. */
58                 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
59                 samplerate = g_variant_get_uint64(gvar);
60                 g_variant_unref(gvar);
61                 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
62                 if (limit_samples == 0) {
63                         g_critical("Not enough time at this samplerate.");
64                         return SR_ERR;
65                 }
66                 gvar = g_variant_new_uint64(limit_samples);
67                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
68                         g_critical("Failed to configure time-based sample limit.");
69                         return SR_ERR;
70                 }
71         } else {
72                 g_critical("This device does not support time limits.");
73                 return SR_ERR;
74         }
75
76         return SR_OK;
77 }
78
79 const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile)
80 {
81         const struct sr_output_module *omod;
82         const struct sr_option **options;
83         const struct sr_output *o;
84         GHashTable *fmtargs, *fmtopts;
85         char *fmtspec;
86
87         if (!opt_output_format) {
88                 if (opt_output_file) {
89                         opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE;
90                 } else {
91                         opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE;
92                 }
93         }
94
95         fmtargs = parse_generic_arg(opt_output_format, TRUE);
96         fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
97         if (!fmtspec)
98                 g_critical("Invalid output format.");
99         if (!(omod = sr_output_find(fmtspec)))
100                 g_critical("Unknown output module '%s'.", fmtspec);
101         g_hash_table_remove(fmtargs, "sigrok_key");
102         if ((options = sr_output_options_get(omod))) {
103                 fmtopts = generic_arg_to_opt(options, fmtargs);
104                 sr_output_options_free(options);
105         } else
106                 fmtopts = NULL;
107         o = sr_output_new(omod, fmtopts, sdi, opt_output_file);
108
109         if (opt_output_file) {
110                 if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING))
111                         *outfile = g_fopen(opt_output_file, "wb");
112                 else
113                         *outfile = NULL;
114         } else {
115                 *outfile = stdout;
116         }
117
118         if (fmtopts)
119                 g_hash_table_destroy(fmtopts);
120         g_hash_table_destroy(fmtargs);
121
122         return o;
123 }
124
125 const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
126 {
127         const struct sr_transform_module *tmod;
128         const struct sr_option **options;
129         const struct sr_transform *t;
130         GHashTable *fmtargs, *fmtopts;
131         char *fmtspec;
132
133         if (!opt_transform_module)
134                 opt_transform_module = "nop";
135
136         fmtargs = parse_generic_arg(opt_transform_module, TRUE);
137         fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
138         if (!fmtspec)
139                 g_critical("Invalid transform module.");
140         if (!(tmod = sr_transform_find(fmtspec)))
141                 g_critical("Unknown transform module '%s'.", fmtspec);
142         g_hash_table_remove(fmtargs, "sigrok_key");
143         if ((options = sr_transform_options_get(tmod))) {
144                 fmtopts = generic_arg_to_opt(options, fmtargs);
145                 sr_transform_options_free(options);
146         } else
147                 fmtopts = NULL;
148         t = sr_transform_new(tmod, fmtopts, sdi);
149         if (fmtopts)
150                 g_hash_table_destroy(fmtopts);
151         g_hash_table_destroy(fmtargs);
152
153         return t;
154 }
155
156 void datafeed_in(const struct sr_dev_inst *sdi,
157                 const struct sr_datafeed_packet *packet, void *cb_data)
158 {
159         const struct sr_datafeed_meta *meta;
160         const struct sr_datafeed_logic *logic;
161         const struct sr_datafeed_analog *analog;
162         struct sr_session *session;
163         struct sr_config *src;
164         static const struct sr_output *o = NULL;
165         static const struct sr_output *oa = NULL;
166         static uint64_t rcvd_samples_logic = 0;
167         static uint64_t rcvd_samples_analog = 0;
168         static uint64_t samplerate = 0;
169         static int triggered = 0;
170         static FILE *outfile = NULL;
171         GSList *l;
172         GString *out;
173         GVariant *gvar;
174         uint64_t end_sample;
175         uint64_t input_len;
176         struct sr_dev_driver *driver;
177
178         /* Avoid warnings when building without decoder support. */
179         (void)session;
180         (void)input_len;
181
182         driver = sr_dev_inst_driver_get(sdi);
183
184         /* If the first packet to come in isn't a header, don't even try. */
185         if (packet->type != SR_DF_HEADER && !o)
186                 return;
187
188         session = cb_data;
189         switch (packet->type) {
190         case SR_DF_HEADER:
191                 g_debug("cli: Received SR_DF_HEADER.");
192                 if (!(o = setup_output_format(sdi, &outfile)))
193                         g_critical("Failed to initialize output module.");
194
195                 /* Set up backup analog output module. */
196                 if (outfile)
197                         oa = sr_output_new(sr_output_find("analog"), NULL,
198                                         sdi, NULL);
199
200                 rcvd_samples_logic = rcvd_samples_analog = 0;
201
202                 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
203                                 &gvar) == SR_OK) {
204                         samplerate = g_variant_get_uint64(gvar);
205                         g_variant_unref(gvar);
206                 }
207
208 #ifdef HAVE_SRD
209                 if (opt_pds) {
210                         if (samplerate) {
211                                 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
212                                                 g_variant_new_uint64(samplerate)) != SRD_OK) {
213                                         g_critical("Failed to configure decode session.");
214                                         break;
215                                 }
216                         }
217                         if (srd_session_start(srd_sess) != SRD_OK) {
218                                 g_critical("Failed to start decode session.");
219                                 break;
220                         }
221                 }
222 #endif
223                 break;
224
225         case SR_DF_META:
226                 g_debug("cli: Received SR_DF_META.");
227                 meta = packet->payload;
228                 for (l = meta->config; l; l = l->next) {
229                         src = l->data;
230                         switch (src->key) {
231                         case SR_CONF_SAMPLERATE:
232                                 samplerate = g_variant_get_uint64(src->data);
233                                 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
234 #ifdef HAVE_SRD
235                                 if (opt_pds) {
236                                         if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
237                                                         g_variant_new_uint64(samplerate)) != SRD_OK) {
238                                                 g_critical("Failed to pass samplerate to decoder.");
239                                         }
240                                 }
241 #endif
242                                 break;
243                         case SR_CONF_SAMPLE_INTERVAL:
244                                 samplerate = g_variant_get_uint64(src->data);
245                                 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
246                                 break;
247                         default:
248                                 /* Unknown metadata is not an error. */
249                                 break;
250                         }
251                 }
252                 break;
253
254         case SR_DF_TRIGGER:
255                 g_debug("cli: Received SR_DF_TRIGGER.");
256                 triggered = 1;
257                 break;
258
259         case SR_DF_LOGIC:
260                 logic = packet->payload;
261                 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
262                                 logic->length, logic->unitsize);
263                 if (logic->length == 0)
264                         break;
265
266                 /* Don't store any samples until triggered. */
267                 if (opt_wait_trigger && !triggered)
268                         break;
269
270                 if (limit_samples && rcvd_samples_logic >= limit_samples)
271                         break;
272
273                 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
274                 /* Cut off last packet according to the sample limit. */
275                 if (limit_samples && end_sample > limit_samples)
276                         end_sample = limit_samples;
277                 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
278
279                 if (opt_pds) {
280 #ifdef HAVE_SRD
281                         if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
282                                         logic->data, input_len, logic->unitsize) != SRD_OK)
283                                 sr_session_stop(session);
284 #endif
285                 }
286
287                 rcvd_samples_logic = end_sample;
288                 break;
289
290         case SR_DF_ANALOG:
291                 analog = packet->payload;
292                 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
293                 if (analog->num_samples == 0)
294                         break;
295
296                 if (limit_samples && rcvd_samples_analog >= limit_samples)
297                         break;
298
299                 rcvd_samples_analog += analog->num_samples;
300                 break;
301
302         case SR_DF_FRAME_BEGIN:
303                 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
304                 break;
305
306         case SR_DF_FRAME_END:
307                 g_debug("cli: Received SR_DF_FRAME_END.");
308                 break;
309
310         default:
311                 break;
312         }
313
314         if (o && !opt_pds) {
315                 if (sr_output_send(o, packet, &out) == SR_OK) {
316                         if (oa && !out) {
317                                 /*
318                                  * The user didn't specify an output module,
319                                  * but needs to see this analog data.
320                                  */
321                                 sr_output_send(oa, packet, &out);
322                         }
323                         if (outfile && out && out->len > 0) {
324                                 fwrite(out->str, 1, out->len, outfile);
325                                 fflush(outfile);
326                         }
327                         if (out)
328                                 g_string_free(out, TRUE);
329                 }
330         }
331
332         /*
333          * SR_DF_END needs to be handled after the output module's receive()
334          * is called, so it can properly clean up that module.
335          */
336         if (packet->type == SR_DF_END) {
337                 g_debug("cli: Received SR_DF_END.");
338
339                 if (o)
340                         sr_output_free(o);
341                 o = NULL;
342
343                 if (oa)
344                         sr_output_free(oa);
345                 oa = NULL;
346
347                 if (outfile && outfile != stdout)
348                         fclose(outfile);
349
350                 if (limit_samples) {
351                         if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
352                                 g_warning("Device only sent %" PRIu64 " samples.",
353                                            rcvd_samples_logic);
354                         else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
355                                 g_warning("Device only sent %" PRIu64 " samples.",
356                                            rcvd_samples_analog);
357                 }
358         }
359
360 }
361
362 int opt_to_gvar(char *key, char *value, struct sr_config *src)
363 {
364         const struct sr_key_info *srci, *srmqi;
365         double tmp_double, dlow, dhigh;
366         uint64_t tmp_u64, p, q, low, high, mqflags;
367         uint32_t mq;
368         GVariant *rational[2], *range[2], *gtup[2];
369         GVariantBuilder *vbl;
370         gboolean tmp_bool;
371         gchar **keyval;
372         int ret, i;
373
374         if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
375                 g_critical("Unknown device option '%s'.", (char *) key);
376                 return -1;
377         }
378         src->key = srci->key;
379
380         if ((!value || strlen(value) == 0) &&
381                 (srci->datatype != SR_T_BOOL)) {
382                 g_critical("Option '%s' needs a value.", (char *)key);
383                 return -1;
384         }
385
386         ret = 0;
387         switch (srci->datatype) {
388         case SR_T_UINT64:
389                 ret = sr_parse_sizestring(value, &tmp_u64);
390                 if (ret != 0)
391                         break;
392                 src->data = g_variant_new_uint64(tmp_u64);
393                 break;
394         case SR_T_INT32:
395                 ret = sr_parse_sizestring(value, &tmp_u64);
396                 if (ret != 0)
397                         break;
398                 src->data = g_variant_new_int32(tmp_u64);
399                 break;
400         case SR_T_STRING:
401                 src->data = g_variant_new_string(value);
402                 break;
403         case SR_T_BOOL:
404                 if (!value)
405                         tmp_bool = TRUE;
406                 else
407                         tmp_bool = sr_parse_boolstring(value);
408                 src->data = g_variant_new_boolean(tmp_bool);
409                 break;
410         case SR_T_FLOAT:
411                 tmp_double = strtof(value, NULL);
412                 src->data = g_variant_new_double(tmp_double);
413                 break;
414         case SR_T_RATIONAL_PERIOD:
415                 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
416                         break;
417                 rational[0] = g_variant_new_uint64(p);
418                 rational[1] = g_variant_new_uint64(q);
419                 src->data = g_variant_new_tuple(rational, 2);
420                 break;
421         case SR_T_RATIONAL_VOLT:
422                 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
423                         break;
424                 rational[0] = g_variant_new_uint64(p);
425                 rational[1] = g_variant_new_uint64(q);
426                 src->data = g_variant_new_tuple(rational, 2);
427                 break;
428         case SR_T_UINT64_RANGE:
429                 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
430                         ret = -1;
431                         break;
432                 } else {
433                         range[0] = g_variant_new_uint64(low);
434                         range[1] = g_variant_new_uint64(high);
435                         src->data = g_variant_new_tuple(range, 2);
436                 }
437                 break;
438         case SR_T_DOUBLE_RANGE:
439                 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
440                         ret = -1;
441                         break;
442                 } else {
443                         range[0] = g_variant_new_double(dlow);
444                         range[1] = g_variant_new_double(dhigh);
445                         src->data = g_variant_new_tuple(range, 2);
446                 }
447                 break;
448         case SR_T_KEYVALUE:
449                 /* Expects the argument to be in the form of key=value. */
450                 keyval = g_strsplit(value, "=", 2);
451                 if (!keyval[0] || !keyval[1]) {
452                         g_strfreev(keyval);
453                         ret = -1;
454                         break;
455                 } else {
456                         vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
457                         g_variant_builder_add(vbl, "{ss}",
458                                               keyval[0], keyval[1]);
459                         src->data = g_variant_builder_end(vbl);
460                         g_strfreev(keyval);
461                 }
462                 break;
463         case SR_T_MQ:
464                 /*
465                   Argument is MQ id e.g. ("voltage") optionally followed by one
466                   or more /<mqflag> e.g. "/ac".
467                  */
468                 keyval = g_strsplit(value, "/", 0);
469                 if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) {
470                         g_strfreev(keyval);
471                         ret = -1;
472                         break;
473                 }
474                 mq = srmqi->key;
475                 mqflags = 0;
476                 for (i = 1; keyval[i]; i++) {
477                         if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) {
478                                 ret = -1;
479                                 break;
480                         }
481                         mqflags |= srmqi->key;
482                 }
483                 g_strfreev(keyval);
484                 if (ret != -1) {
485                         gtup[0] = g_variant_new_uint32(mq);
486                         gtup[1] = g_variant_new_uint64(mqflags);
487                         src->data = g_variant_new_tuple(gtup, 2);
488                 }
489                 break;
490         default:
491                 g_critical("Unknown data type specified for option '%s' "
492                            "(driver implementation bug?).", key);
493                 ret = -1;
494         }
495
496         if (ret < 0)
497                 g_critical("Invalid value: '%s' for option '%s'", value, key);
498
499         return ret;
500 }
501
502 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
503 {
504         struct sr_config src;
505         struct sr_channel_group *cg;
506         GHashTableIter iter;
507         gpointer key, value;
508         int ret;
509
510         g_hash_table_iter_init(&iter, args);
511         while (g_hash_table_iter_next(&iter, &key, &value)) {
512                 if ((ret = opt_to_gvar(key, value, &src)) != 0)
513                         return ret;
514                 cg = select_channel_group(sdi);
515                 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
516                                 src.key, src.data)) != SR_OK) {
517                         g_critical("Failed to set device option '%s': %s.",
518                                    (char *)key, sr_strerror(ret));
519                         return ret;
520                 }
521         }
522
523         return SR_OK;
524 }
525
526 void run_session(void)
527 {
528         GSList *devices, *real_devices, *sd;
529         GHashTable *devargs;
530         GVariant *gvar;
531         struct sr_session *session;
532         struct sr_trigger *trigger;
533         struct sr_dev_inst *sdi;
534         uint64_t min_samples, max_samples;
535         GArray *drv_opts;
536         guint i;
537         int is_demo_dev;
538         struct sr_dev_driver *driver;
539         const struct sr_transform *t;
540         GMainLoop *main_loop;
541
542         devices = device_scan();
543         if (!devices) {
544                 g_critical("No devices found.");
545                 return;
546         }
547
548         real_devices = NULL;
549         for (sd = devices; sd; sd = sd->next) {
550                 sdi = sd->data;
551
552                 driver = sr_dev_inst_driver_get(sdi);
553
554                 if (!(drv_opts = sr_dev_options(driver, NULL, NULL))) {
555                         g_critical("Failed to query list of driver options.");
556                         return;
557                 }
558
559                 is_demo_dev = 0;
560                 for (i = 0; i < drv_opts->len; i++) {
561                         if (g_array_index(drv_opts, uint32_t, i) == SR_CONF_DEMO_DEV)
562                                 is_demo_dev = 1;
563                 }
564
565                 g_array_free(drv_opts, TRUE);
566
567                 if (!is_demo_dev)
568                         real_devices = g_slist_append(real_devices, sdi);
569         }
570
571         if (g_slist_length(devices) > 1) {
572                 if (g_slist_length(real_devices) != 1) {
573                         g_critical("sigrok-cli only supports one device for capturing.");
574                         return;
575                 } else {
576                         /* We only have one non-demo device. */
577                         g_slist_free(devices);
578                         devices = real_devices;
579                         real_devices = NULL;
580                 }
581         }
582
583         sdi = devices->data;
584         g_slist_free(devices);
585         g_slist_free(real_devices);
586
587         sr_session_new(sr_ctx, &session);
588         sr_session_datafeed_callback_add(session, datafeed_in, NULL);
589
590         if (sr_dev_open(sdi) != SR_OK) {
591                 g_critical("Failed to open device.");
592                 return;
593         }
594
595         if (sr_session_dev_add(session, sdi) != SR_OK) {
596                 g_critical("Failed to add device to session.");
597                 sr_session_destroy(session);
598                 return;
599         }
600
601         if (opt_config) {
602                 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
603                         if (set_dev_options(sdi, devargs) != SR_OK)
604                                 return;
605                         g_hash_table_destroy(devargs);
606                 }
607         }
608
609         if (select_channels(sdi) != SR_OK) {
610                 g_critical("Failed to set channels.");
611                 sr_session_destroy(session);
612                 return;
613         }
614
615         trigger = NULL;
616         if (opt_triggers) {
617                 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
618                         sr_session_destroy(session);
619                         return;
620                 }
621                 if (sr_session_trigger_set(session, trigger) != SR_OK) {
622                         sr_session_destroy(session);
623                         return;
624                 }
625         }
626
627         if (opt_continuous) {
628                 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
629                         g_critical("This device does not support continuous sampling.");
630                         sr_session_destroy(session);
631                         return;
632                 }
633         }
634
635         if (opt_time) {
636                 if (set_limit_time(sdi) != SR_OK) {
637                         sr_session_destroy(session);
638                         return;
639                 }
640         }
641
642         if (opt_samples) {
643                 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
644                         g_critical("Invalid sample limit '%s'.", opt_samples);
645                         sr_session_destroy(session);
646                         return;
647                 }
648                 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
649                                 &gvar) == SR_OK) {
650                         /*
651                          * The device has no compression, or compression is turned
652                          * off, and publishes its sample memory size.
653                          */
654                         g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
655                         g_variant_unref(gvar);
656                         if (limit_samples < min_samples) {
657                                 g_critical("The device stores at least %"PRIu64
658                                                 " samples with the current settings.", min_samples);
659                         }
660                         if (limit_samples > max_samples) {
661                                 g_critical("The device can store only %"PRIu64
662                                                 " samples with the current settings.", max_samples);
663                         }
664                 }
665                 gvar = g_variant_new_uint64(limit_samples);
666                 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
667                         g_critical("Failed to configure sample limit.");
668                         sr_session_destroy(session);
669                         return;
670                 }
671         }
672
673         if (opt_frames) {
674                 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
675                         g_critical("Invalid sample limit '%s'.", opt_samples);
676                         sr_session_destroy(session);
677                         return;
678                 }
679                 gvar = g_variant_new_uint64(limit_frames);
680                 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
681                         g_critical("Failed to configure frame limit.");
682                         sr_session_destroy(session);
683                         return;
684                 }
685         }
686
687         if (!(t = setup_transform_module(sdi)))
688                 g_critical("Failed to initialize transform module.");
689
690         main_loop = g_main_loop_new(NULL, FALSE);
691
692         sr_session_stopped_callback_set(session,
693                 (sr_session_stopped_callback)g_main_loop_quit, main_loop);
694
695         if (sr_session_start(session) != SR_OK) {
696                 g_critical("Failed to start session.");
697                 g_main_loop_unref(main_loop);
698                 sr_session_destroy(session);
699                 return;
700         }
701
702         if (opt_continuous)
703                 add_anykey(session);
704
705         g_main_loop_run(main_loop);
706
707         if (opt_continuous)
708                 clear_anykey();
709
710         if (trigger)
711                 sr_trigger_free(trigger);
712
713         sr_session_datafeed_callback_remove_all(session);
714         g_main_loop_unref(main_loop);
715         sr_session_destroy(session);
716 }