| 1 | /* |
| 2 | * babeltrace.c |
| 3 | * |
| 4 | * Babeltrace Trace Converter |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/babeltrace.h> |
| 30 | #include <babeltrace/plugin/plugin.h> |
| 31 | #include <babeltrace/component/component.h> |
| 32 | #include <babeltrace/component/component-source.h> |
| 33 | #include <babeltrace/component/component-sink.h> |
| 34 | #include <babeltrace/component/component-filter.h> |
| 35 | #include <babeltrace/component/component-class.h> |
| 36 | #include <babeltrace/component/notification/iterator.h> |
| 37 | #include <babeltrace/ref.h> |
| 38 | #include <babeltrace/values.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
| 41 | #include <popt.h> |
| 42 | #include <string.h> |
| 43 | #include <stdio.h> |
| 44 | #include <glib.h> |
| 45 | #include "babeltrace-cfg.h" |
| 46 | #include "default-cfg.h" |
| 47 | |
| 48 | GPtrArray *loaded_plugins; |
| 49 | |
| 50 | static |
| 51 | void init_loaded_plugins_array(void) |
| 52 | { |
| 53 | loaded_plugins = g_ptr_array_new_full(8, bt_put); |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | void fini_loaded_plugins_array(void) |
| 58 | { |
| 59 | g_ptr_array_free(loaded_plugins, TRUE); |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | struct bt_plugin *find_plugin(const char *name) |
| 64 | { |
| 65 | int i; |
| 66 | struct bt_plugin *plugin = NULL; |
| 67 | |
| 68 | for (i = 0; i < loaded_plugins->len; i++) { |
| 69 | plugin = g_ptr_array_index(loaded_plugins, i); |
| 70 | |
| 71 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | plugin = NULL; |
| 76 | } |
| 77 | |
| 78 | return bt_get(plugin); |
| 79 | } |
| 80 | |
| 81 | static |
| 82 | struct bt_component_class *find_component_class(const char *plugin_name, |
| 83 | const char *comp_class_name, |
| 84 | enum bt_component_class_type comp_class_type) |
| 85 | { |
| 86 | struct bt_component_class *comp_class = NULL; |
| 87 | struct bt_plugin *plugin = find_plugin(plugin_name); |
| 88 | |
| 89 | if (!plugin) { |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | comp_class = bt_plugin_get_component_class_by_name_and_type(plugin, |
| 94 | comp_class_name, comp_class_type); |
| 95 | BT_PUT(plugin); |
| 96 | end: |
| 97 | return comp_class; |
| 98 | } |
| 99 | |
| 100 | static |
| 101 | const char *component_type_str(enum bt_component_class_type type) |
| 102 | { |
| 103 | switch (type) { |
| 104 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 105 | return "source"; |
| 106 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 107 | return "sink"; |
| 108 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 109 | return "filter"; |
| 110 | case BT_COMPONENT_CLASS_TYPE_UNKNOWN: |
| 111 | default: |
| 112 | return "unknown"; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | void print_component_classes_found(void) |
| 118 | { |
| 119 | int plugins_count, component_classes_count = 0, i; |
| 120 | |
| 121 | if (!babeltrace_verbose) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | plugins_count = loaded_plugins->len; |
| 126 | if (plugins_count == 0) { |
| 127 | fprintf(stderr, "No plugins found. Please make sure your plug-in search path is set correctly.\n"); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | for (i = 0; i < plugins_count; i++) { |
| 132 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); |
| 133 | |
| 134 | component_classes_count += bt_plugin_get_component_class_count(plugin); |
| 135 | } |
| 136 | |
| 137 | printf_verbose("Found %d component classes in %d plugins.\n", |
| 138 | component_classes_count, plugins_count); |
| 139 | |
| 140 | for (i = 0; i < plugins_count; i++) { |
| 141 | int j; |
| 142 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); |
| 143 | |
| 144 | component_classes_count = |
| 145 | bt_plugin_get_component_class_count(plugin); |
| 146 | |
| 147 | for (j = 0; j < component_classes_count; j++) { |
| 148 | struct bt_component_class *comp_class = |
| 149 | bt_plugin_get_component_class(plugin, j); |
| 150 | const char *plugin_name = bt_plugin_get_name(plugin); |
| 151 | const char *comp_class_name = |
| 152 | bt_component_class_get_name(comp_class); |
| 153 | const char *path = bt_plugin_get_path(plugin); |
| 154 | const char *author = bt_plugin_get_author(plugin); |
| 155 | const char *license = bt_plugin_get_license(plugin); |
| 156 | const char *plugin_description = |
| 157 | bt_plugin_get_description(plugin); |
| 158 | const char *comp_class_description = |
| 159 | bt_component_class_get_description(comp_class); |
| 160 | enum bt_component_class_type type = |
| 161 | bt_component_class_get_type(comp_class); |
| 162 | |
| 163 | printf_verbose("[%s - %s (%s)]\n", plugin_name, |
| 164 | comp_class_name, component_type_str(type)); |
| 165 | printf_verbose("\tpath: %s\n", path ? path : "None"); |
| 166 | printf_verbose("\tauthor: %s\n", |
| 167 | author ? author : "Unknown"); |
| 168 | printf_verbose("\tlicense: %s\n", |
| 169 | license ? license : "Unknown"); |
| 170 | printf_verbose("\tplugin description: %s\n", |
| 171 | plugin_description ? plugin_description : "None"); |
| 172 | printf_verbose("\tcomponent description: %s\n", |
| 173 | comp_class_description ? comp_class_description : "None"); |
| 174 | bt_put(comp_class); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | void print_indent(size_t indent) |
| 181 | { |
| 182 | size_t i; |
| 183 | |
| 184 | for (i = 0; i < indent; i++) { |
| 185 | printf_verbose(" "); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static |
| 190 | void print_value(struct bt_value *, size_t, bool); |
| 191 | |
| 192 | static |
| 193 | bool print_map_value(const char *key, struct bt_value *object, void *data) |
| 194 | { |
| 195 | size_t indent = (size_t) data; |
| 196 | |
| 197 | print_indent(indent); |
| 198 | printf_verbose("\"%s\": ", key); |
| 199 | print_value(object, indent, false); |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | static |
| 205 | void print_value(struct bt_value *value, size_t indent, bool do_indent) |
| 206 | { |
| 207 | bool bool_val; |
| 208 | int64_t int_val; |
| 209 | double dbl_val; |
| 210 | const char *str_val; |
| 211 | int size; |
| 212 | int i; |
| 213 | |
| 214 | if (!value) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (do_indent) { |
| 219 | print_indent(indent); |
| 220 | } |
| 221 | |
| 222 | switch (bt_value_get_type(value)) { |
| 223 | case BT_VALUE_TYPE_NULL: |
| 224 | printf_verbose("null\n"); |
| 225 | break; |
| 226 | case BT_VALUE_TYPE_BOOL: |
| 227 | bt_value_bool_get(value, &bool_val); |
| 228 | printf_verbose("%s\n", bool_val ? "true" : "false"); |
| 229 | break; |
| 230 | case BT_VALUE_TYPE_INTEGER: |
| 231 | bt_value_integer_get(value, &int_val); |
| 232 | printf_verbose("%" PRId64 "\n", int_val); |
| 233 | break; |
| 234 | case BT_VALUE_TYPE_FLOAT: |
| 235 | bt_value_float_get(value, &dbl_val); |
| 236 | printf_verbose("%lf\n", dbl_val); |
| 237 | break; |
| 238 | case BT_VALUE_TYPE_STRING: |
| 239 | bt_value_string_get(value, &str_val); |
| 240 | printf_verbose("\"%s\"\n", str_val); |
| 241 | break; |
| 242 | case BT_VALUE_TYPE_ARRAY: |
| 243 | size = bt_value_array_size(value); |
| 244 | printf_verbose("[\n"); |
| 245 | |
| 246 | for (i = 0; i < size; i++) { |
| 247 | struct bt_value *element = |
| 248 | bt_value_array_get(value, i); |
| 249 | |
| 250 | print_value(element, indent + 2, true); |
| 251 | BT_PUT(element); |
| 252 | } |
| 253 | |
| 254 | print_indent(indent); |
| 255 | printf_verbose("]\n"); |
| 256 | break; |
| 257 | case BT_VALUE_TYPE_MAP: |
| 258 | if (bt_value_map_is_empty(value)) { |
| 259 | printf_verbose("{}\n"); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | printf_verbose("{\n"); |
| 264 | bt_value_map_foreach(value, print_map_value, |
| 265 | (void *) (indent + 2)); |
| 266 | print_indent(indent); |
| 267 | printf_verbose("}\n"); |
| 268 | break; |
| 269 | default: |
| 270 | assert(false); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static |
| 275 | void print_bt_config_component(struct bt_config_component *bt_config_component) |
| 276 | { |
| 277 | printf_verbose(" %s.%s\n", bt_config_component->plugin_name->str, |
| 278 | bt_config_component->component_name->str); |
| 279 | printf_verbose(" params:\n"); |
| 280 | print_value(bt_config_component->params, 6, true); |
| 281 | } |
| 282 | |
| 283 | static |
| 284 | void print_bt_config_components(GPtrArray *array) |
| 285 | { |
| 286 | size_t i; |
| 287 | |
| 288 | for (i = 0; i < array->len; i++) { |
| 289 | struct bt_config_component *cfg_component = |
| 290 | bt_config_get_component(array, i); |
| 291 | print_bt_config_component(cfg_component); |
| 292 | BT_PUT(cfg_component); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static |
| 297 | void print_cfg(struct bt_config *cfg) |
| 298 | { |
| 299 | printf_verbose("debug: %d\n", cfg->debug); |
| 300 | printf_verbose("verbose: %d\n", cfg->verbose); |
| 301 | printf_verbose("do list: %d\n", cfg->do_list); |
| 302 | printf_verbose("force correlate: %d\n", cfg->force_correlate); |
| 303 | printf_verbose("plugin paths:\n"); |
| 304 | print_value(cfg->plugin_paths, 2, true); |
| 305 | printf_verbose("sources:\n"); |
| 306 | print_bt_config_components(cfg->sources); |
| 307 | printf_verbose("sinks:\n"); |
| 308 | print_bt_config_components(cfg->sinks); |
| 309 | } |
| 310 | |
| 311 | static |
| 312 | struct bt_component *create_trimmer(struct bt_config_component *source_cfg) |
| 313 | { |
| 314 | struct bt_component *trimmer = NULL; |
| 315 | struct bt_component_class *trimmer_class = NULL; |
| 316 | struct bt_value *trimmer_params = NULL; |
| 317 | struct bt_value *value; |
| 318 | |
| 319 | trimmer_params = bt_value_map_create(); |
| 320 | if (!trimmer_params) { |
| 321 | goto end; |
| 322 | } |
| 323 | |
| 324 | value = bt_value_map_get(source_cfg->params, "begin"); |
| 325 | if (value) { |
| 326 | enum bt_value_status ret; |
| 327 | |
| 328 | ret = bt_value_map_insert(trimmer_params, "begin", |
| 329 | value); |
| 330 | BT_PUT(value); |
| 331 | if (ret) { |
| 332 | goto end; |
| 333 | } |
| 334 | } |
| 335 | value = bt_value_map_get(source_cfg->params, "end"); |
| 336 | if (value) { |
| 337 | enum bt_value_status ret; |
| 338 | |
| 339 | ret = bt_value_map_insert(trimmer_params, "end", |
| 340 | value); |
| 341 | BT_PUT(value); |
| 342 | if (ret) { |
| 343 | goto end; |
| 344 | } |
| 345 | } |
| 346 | value = bt_value_map_get(source_cfg->params, "clock-gmt"); |
| 347 | if (value) { |
| 348 | enum bt_value_status ret; |
| 349 | |
| 350 | ret = bt_value_map_insert(trimmer_params, "clock-gmt", |
| 351 | value); |
| 352 | BT_PUT(value); |
| 353 | if (ret) { |
| 354 | goto end; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | trimmer_class = find_component_class("utils", "trimmer", |
| 359 | BT_COMPONENT_CLASS_TYPE_FILTER); |
| 360 | if (!trimmer_class) { |
| 361 | fprintf(stderr, "Could not find trimmer component class. Aborting...\n"); |
| 362 | goto end; |
| 363 | } |
| 364 | trimmer = bt_component_create(trimmer_class, "source_trimmer", |
| 365 | trimmer_params); |
| 366 | if (!trimmer) { |
| 367 | goto end; |
| 368 | } |
| 369 | end: |
| 370 | bt_put(trimmer_params); |
| 371 | bt_put(trimmer_class); |
| 372 | return trimmer; |
| 373 | } |
| 374 | |
| 375 | static |
| 376 | int connect_source_sink(struct bt_component *source, |
| 377 | struct bt_config_component *source_cfg, |
| 378 | struct bt_component *sink) |
| 379 | { |
| 380 | int ret = 0; |
| 381 | enum bt_component_status sink_status; |
| 382 | struct bt_component *trimmer = NULL; |
| 383 | struct bt_notification_iterator *source_it = NULL; |
| 384 | struct bt_notification_iterator *to_sink_it = NULL; |
| 385 | |
| 386 | source_it = bt_component_source_create_iterator(source); |
| 387 | if (!source_it) { |
| 388 | fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n"); |
| 389 | ret = -1; |
| 390 | goto end; |
| 391 | } |
| 392 | |
| 393 | if (bt_value_map_has_key(source_cfg->params, "begin") |
| 394 | || bt_value_map_has_key(source_cfg->params, "end")) { |
| 395 | /* A trimmer must be inserted in the graph. */ |
| 396 | enum bt_component_status trimmer_status; |
| 397 | |
| 398 | trimmer = create_trimmer(source_cfg); |
| 399 | if (!trimmer) { |
| 400 | fprintf(stderr, "Failed to create trimmer component. Aborting...\n"); |
| 401 | ret = -1; |
| 402 | goto end; |
| 403 | } |
| 404 | |
| 405 | trimmer_status = bt_component_filter_add_iterator(trimmer, |
| 406 | source_it); |
| 407 | BT_PUT(source_it); |
| 408 | if (trimmer_status != BT_COMPONENT_STATUS_OK) { |
| 409 | fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n"); |
| 410 | ret = -1; |
| 411 | goto end; |
| 412 | } |
| 413 | |
| 414 | to_sink_it = bt_component_filter_create_iterator(trimmer); |
| 415 | if (!to_sink_it) { |
| 416 | fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n"); |
| 417 | ret = -1; |
| 418 | goto end; |
| 419 | } |
| 420 | } else { |
| 421 | BT_MOVE(to_sink_it, source_it); |
| 422 | } |
| 423 | |
| 424 | sink_status = bt_component_sink_add_iterator(sink, to_sink_it); |
| 425 | if (sink_status != BT_COMPONENT_STATUS_OK) { |
| 426 | fprintf(stderr, "Failed to connect to sink component. Aborting...\n"); |
| 427 | ret = -1; |
| 428 | goto end; |
| 429 | } |
| 430 | end: |
| 431 | bt_put(trimmer); |
| 432 | bt_put(source_it); |
| 433 | bt_put(to_sink_it); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static |
| 438 | void add_to_loaded_plugins(struct bt_plugin **plugins) |
| 439 | { |
| 440 | while (*plugins) { |
| 441 | struct bt_plugin *plugin = *plugins; |
| 442 | /* Check if it's already loaded (from another path). */ |
| 443 | struct bt_plugin *loaded_plugin = |
| 444 | find_plugin(bt_plugin_get_name(plugin)); |
| 445 | |
| 446 | if (loaded_plugin) { |
| 447 | printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n", |
| 448 | bt_plugin_get_path(plugin), |
| 449 | bt_plugin_get_path(loaded_plugin)); |
| 450 | BT_PUT(loaded_plugin); |
| 451 | BT_PUT(plugin); |
| 452 | } else { |
| 453 | /* Transfer ownership to global array. */ |
| 454 | g_ptr_array_add(loaded_plugins, plugin); |
| 455 | } |
| 456 | *(plugins++) = NULL; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static |
| 461 | int load_dynamic_plugins(struct bt_config *cfg) |
| 462 | { |
| 463 | int nr_paths, i, ret = 0; |
| 464 | |
| 465 | nr_paths = bt_value_array_size(cfg->plugin_paths); |
| 466 | if (nr_paths < 0) { |
| 467 | ret = -1; |
| 468 | goto end; |
| 469 | } |
| 470 | |
| 471 | for (i = 0; i < nr_paths; i++) { |
| 472 | struct bt_value *plugin_path_value = NULL; |
| 473 | const char *plugin_path; |
| 474 | struct bt_plugin **plugins; |
| 475 | |
| 476 | plugin_path_value = bt_value_array_get(cfg->plugin_paths, i); |
| 477 | if (bt_value_string_get(plugin_path_value, |
| 478 | &plugin_path)) { |
| 479 | BT_PUT(plugin_path_value); |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | plugins = bt_plugin_create_all_from_dir(plugin_path, true); |
| 484 | if (!plugins) { |
| 485 | printf_debug("Unable to dynamically load plugins from path %s.\n", |
| 486 | plugin_path); |
| 487 | BT_PUT(plugin_path_value); |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | add_to_loaded_plugins(plugins); |
| 492 | free(plugins); |
| 493 | |
| 494 | BT_PUT(plugin_path_value); |
| 495 | } |
| 496 | end: |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static |
| 501 | int load_static_plugins(void) |
| 502 | { |
| 503 | int ret = 0; |
| 504 | struct bt_plugin **plugins; |
| 505 | |
| 506 | plugins = bt_plugin_create_all_from_static(); |
| 507 | if (!plugins) { |
| 508 | printf_debug("Unable to load static plugins.\n"); |
| 509 | ret = -1; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | add_to_loaded_plugins(plugins); |
| 514 | free(plugins); |
| 515 | end: |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | int main(int argc, const char **argv) |
| 520 | { |
| 521 | int ret; |
| 522 | struct bt_component_class *source_class = NULL; |
| 523 | struct bt_component_class *sink_class = NULL; |
| 524 | struct bt_component *source = NULL, *sink = NULL; |
| 525 | struct bt_value *source_params = NULL, *sink_params = NULL; |
| 526 | struct bt_config *cfg; |
| 527 | enum bt_component_status sink_status; |
| 528 | struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL; |
| 529 | |
| 530 | init_loaded_plugins_array(); |
| 531 | |
| 532 | cfg = bt_config_create(); |
| 533 | if (!cfg) { |
| 534 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); |
| 535 | ret = 1; |
| 536 | goto end; |
| 537 | } |
| 538 | |
| 539 | ret = set_default_config(cfg); |
| 540 | if (ret) { |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | ret = bt_config_init_from_args(cfg, argc, argv); |
| 545 | if (ret == 0) { |
| 546 | babeltrace_verbose = cfg->verbose; |
| 547 | babeltrace_debug = cfg->debug; |
| 548 | print_cfg(cfg); |
| 549 | } else { |
| 550 | goto end; |
| 551 | } |
| 552 | |
| 553 | /* TODO handle more than 1 source and 1 sink. */ |
| 554 | if (cfg->sources->len != 1 || cfg->sinks->len != 1) { |
| 555 | ret = -1; |
| 556 | goto end; |
| 557 | } |
| 558 | |
| 559 | printf_verbose("Verbose mode active.\n"); |
| 560 | printf_debug("Debug mode active.\n"); |
| 561 | |
| 562 | if (load_dynamic_plugins(cfg)) { |
| 563 | fprintf(stderr, "Failed to load dynamic plugins.\n"); |
| 564 | ret = -1; |
| 565 | goto end; |
| 566 | } |
| 567 | |
| 568 | if (load_static_plugins()) { |
| 569 | fprintf(stderr, "Failed to load static plugins.\n"); |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | print_component_classes_found(); |
| 574 | source_cfg = bt_config_get_component(cfg->sources, 0); |
| 575 | source_params = bt_get(source_cfg->params); |
| 576 | source_class = find_component_class(source_cfg->plugin_name->str, |
| 577 | source_cfg->component_name->str, |
| 578 | BT_COMPONENT_CLASS_TYPE_SOURCE); |
| 579 | if (!source_class) { |
| 580 | fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n", |
| 581 | source_cfg->plugin_name->str, |
| 582 | source_cfg->component_name->str); |
| 583 | ret = -1; |
| 584 | goto end; |
| 585 | } |
| 586 | |
| 587 | sink_cfg = bt_config_get_component(cfg->sinks, 0); |
| 588 | sink_params = bt_get(sink_cfg->params); |
| 589 | sink_class = find_component_class(sink_cfg->plugin_name->str, |
| 590 | sink_cfg->component_name->str, |
| 591 | BT_COMPONENT_CLASS_TYPE_SINK); |
| 592 | if (!sink_class) { |
| 593 | fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n", |
| 594 | sink_cfg->plugin_name->str, |
| 595 | sink_cfg->component_name->str); |
| 596 | ret = -1; |
| 597 | goto end; |
| 598 | } |
| 599 | |
| 600 | source = bt_component_create(source_class, "source", source_params); |
| 601 | if (!source) { |
| 602 | fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n"); |
| 603 | ret = -1; |
| 604 | goto end; |
| 605 | } |
| 606 | |
| 607 | sink = bt_component_create(sink_class, "sink", sink_params); |
| 608 | if (!sink) { |
| 609 | fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n"); |
| 610 | ret = -1; |
| 611 | goto end; |
| 612 | } |
| 613 | |
| 614 | ret = connect_source_sink(source, source_cfg, sink); |
| 615 | if (ret) { |
| 616 | goto end; |
| 617 | } |
| 618 | |
| 619 | while (true) { |
| 620 | sink_status = bt_component_sink_consume(sink); |
| 621 | switch (sink_status) { |
| 622 | case BT_COMPONENT_STATUS_AGAIN: |
| 623 | /* Wait for an arbitraty 500 ms. */ |
| 624 | usleep(500000); |
| 625 | break; |
| 626 | case BT_COMPONENT_STATUS_OK: |
| 627 | break; |
| 628 | case BT_COMPONENT_STATUS_END: |
| 629 | goto end; |
| 630 | default: |
| 631 | fprintf(stderr, "Sink component returned an error, aborting...\n"); |
| 632 | ret = -1; |
| 633 | goto end; |
| 634 | } |
| 635 | } |
| 636 | end: |
| 637 | BT_PUT(sink_class); |
| 638 | BT_PUT(source_class); |
| 639 | BT_PUT(source); |
| 640 | BT_PUT(sink); |
| 641 | BT_PUT(source_params); |
| 642 | BT_PUT(sink_params); |
| 643 | BT_PUT(cfg); |
| 644 | BT_PUT(sink_cfg); |
| 645 | BT_PUT(source_cfg); |
| 646 | fini_loaded_plugins_array(); |
| 647 | return ret ? 1 : 0; |
| 648 | } |