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