+static
+struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
+{
+ struct bt_component *trimmer = NULL;
+ struct bt_component_class *trimmer_class = NULL;
+ struct bt_value *trimmer_params = NULL;
+ struct bt_value *value;
+
+ trimmer_params = bt_value_map_create();
+ if (!trimmer_params) {
+ goto end;
+ }
+
+ value = bt_value_map_get(source_cfg->params, "begin");
+ if (value) {
+ enum bt_value_status ret;
+
+ ret = bt_value_map_insert(trimmer_params, "begin",
+ value);
+ BT_PUT(value);
+ if (ret) {
+ goto end;
+ }
+ }
+ value = bt_value_map_get(source_cfg->params, "end");
+ if (value) {
+ enum bt_value_status ret;
+
+ ret = bt_value_map_insert(trimmer_params, "end",
+ value);
+ BT_PUT(value);
+ if (ret) {
+ goto end;
+ }
+ }
+ value = bt_value_map_get(source_cfg->params, "clock-gmt");
+ if (value) {
+ enum bt_value_status ret;
+
+ ret = bt_value_map_insert(trimmer_params, "clock-gmt",
+ value);
+ BT_PUT(value);
+ if (ret) {
+ goto end;
+ }
+ }
+
+ trimmer_class = find_component_class("utils", "trimmer",
+ BT_COMPONENT_CLASS_TYPE_FILTER);
+ if (!trimmer_class) {
+ fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
+ goto end;
+ }
+ trimmer = bt_component_create(trimmer_class, "source_trimmer",
+ trimmer_params);
+ if (!trimmer) {
+ goto end;
+ }
+end:
+ bt_put(trimmer_params);
+ bt_put(trimmer_class);
+ return trimmer;
+}
+
+static
+int connect_source_sink(struct bt_component *source,
+ struct bt_config_component *source_cfg,
+ struct bt_component *sink)
+{
+ int ret = 0;
+ enum bt_component_status sink_status;
+ struct bt_component *trimmer = NULL;
+ struct bt_notification_iterator *source_it = NULL;
+ struct bt_notification_iterator *to_sink_it = NULL;
+
+ source_it = bt_component_source_create_iterator(source);
+ if (!source_it) {
+ fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ if (bt_value_map_has_key(source_cfg->params, "begin")
+ || bt_value_map_has_key(source_cfg->params, "end")) {
+ /* A trimmer must be inserted in the graph. */
+ enum bt_component_status trimmer_status;
+
+ trimmer = create_trimmer(source_cfg);
+ if (!trimmer) {
+ fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ trimmer_status = bt_component_filter_add_iterator(trimmer,
+ source_it);
+ BT_PUT(source_it);
+ if (trimmer_status != BT_COMPONENT_STATUS_OK) {
+ fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+
+ to_sink_it = bt_component_filter_create_iterator(trimmer);
+ if (!to_sink_it) {
+ fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+ } else {
+ BT_MOVE(to_sink_it, source_it);
+ }
+
+ sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
+ if (sink_status != BT_COMPONENT_STATUS_OK) {
+ fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
+ ret = -1;
+ goto end;
+ }
+end:
+ bt_put(trimmer);
+ bt_put(source_it);
+ bt_put(to_sink_it);
+ return ret;
+}
+
+static
+void add_to_loaded_plugins(struct bt_plugin **plugins)
+{
+ while (*plugins) {
+ struct bt_plugin *plugin = *plugins;
+ /* Check if it's already loaded (from another path). */
+ struct bt_plugin *loaded_plugin =
+ find_plugin(bt_plugin_get_name(plugin));
+
+ if (loaded_plugin) {
+ printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n",
+ bt_plugin_get_path(plugin),
+ bt_plugin_get_path(loaded_plugin));
+ BT_PUT(loaded_plugin);
+ BT_PUT(plugin);
+ } else {
+ /* Transfer ownership to global array. */
+ g_ptr_array_add(loaded_plugins, plugin);
+ }
+ *(plugins++) = NULL;
+ }
+}
+
+static
+int load_dynamic_plugins(struct bt_config *cfg)
+{
+ int nr_paths, i, ret = 0;
+
+ nr_paths = bt_value_array_size(cfg->plugin_paths);
+ if (nr_paths < 0) {
+ ret = -1;
+ goto end;
+ }
+
+ for (i = 0; i < nr_paths; i++) {
+ struct bt_value *plugin_path_value = NULL;
+ const char *plugin_path;
+ struct bt_plugin **plugins;
+
+ plugin_path_value = bt_value_array_get(cfg->plugin_paths, i);
+ if (bt_value_string_get(plugin_path_value,
+ &plugin_path)) {
+ BT_PUT(plugin_path_value);
+ continue;
+ }
+
+ plugins = bt_plugin_create_all_from_dir(plugin_path, true);
+ if (!plugins) {
+ printf_debug("Unable to dynamically load plugins from path %s.\n",
+ plugin_path);
+ BT_PUT(plugin_path_value);
+ continue;
+ }
+
+ add_to_loaded_plugins(plugins);
+ free(plugins);
+
+ BT_PUT(plugin_path_value);
+ }
+end:
+ return ret;
+}
+
+static
+int load_static_plugins(void)
+{
+ int ret = 0;
+ struct bt_plugin **plugins;
+
+ plugins = bt_plugin_create_all_from_static();
+ if (!plugins) {
+ printf_debug("Unable to load static plugins.\n");
+ ret = -1;
+ goto end;
+ }
+
+ add_to_loaded_plugins(plugins);
+ free(plugins);
+end:
+ return ret;
+}
+
+int main(int argc, const char **argv)