Commit | Line | Data |
---|---|---|
0d884c50 JG |
1 | /* |
2 | * sink.c | |
3 | * | |
47e5a032 | 4 | * Babeltrace Sink Component |
0d884c50 JG |
5 | * |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@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 | ||
be9ef048 PP |
29 | #define BT_LOG_TAG "COMP-SINK" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
3d9990ac | 32 | #include <babeltrace/compiler-internal.h> |
fec2a9f2 | 33 | #include <babeltrace/values.h> |
b2e0c907 PP |
34 | #include <babeltrace/graph/component-sink-internal.h> |
35 | #include <babeltrace/graph/component-internal.h> | |
36 | #include <babeltrace/graph/notification.h> | |
bd7cc15b | 37 | #include <babeltrace/graph/graph.h> |
f6ccaed9 PP |
38 | #include <babeltrace/assert-internal.h> |
39 | #include <babeltrace/assert-internal.h> | |
0d884c50 | 40 | |
72b913fb | 41 | BT_HIDDEN |
fec2a9f2 JG |
42 | void bt_component_sink_destroy(struct bt_component *component) |
43 | { | |
fec2a9f2 JG |
44 | } |
45 | ||
8738a040 | 46 | BT_HIDDEN |
fb2dcc52 | 47 | struct bt_component *bt_component_sink_create( |
36712f1d | 48 | struct bt_component_class *class) |
0d884c50 JG |
49 | { |
50 | struct bt_component_sink *sink = NULL; | |
0d884c50 | 51 | |
0d884c50 JG |
52 | sink = g_new0(struct bt_component_sink, 1); |
53 | if (!sink) { | |
be9ef048 | 54 | BT_LOGE_STR("Failed to allocate one sink component."); |
0d884c50 JG |
55 | goto end; |
56 | } | |
57 | ||
0d884c50 JG |
58 | end: |
59 | return sink ? &sink->parent : NULL; | |
60 | } | |
fa55ed98 | 61 | |
973a2bd1 | 62 | BT_HIDDEN |
fec2a9f2 JG |
63 | enum bt_component_status bt_component_sink_consume( |
64 | struct bt_component *component) | |
fa55ed98 JG |
65 | { |
66 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
d3e4dcd8 | 67 | struct bt_component_class_sink *sink_class = NULL; |
fa55ed98 | 68 | |
f6ccaed9 PP |
69 | BT_ASSERT(component); |
70 | BT_ASSERT(bt_component_get_class_type(component) == BT_COMPONENT_CLASS_TYPE_SINK); | |
d3e4dcd8 | 71 | sink_class = container_of(component->class, struct bt_component_class_sink, parent); |
f6ccaed9 | 72 | BT_ASSERT(sink_class->methods.consume); |
be9ef048 PP |
73 | BT_LOGD("Calling user's consume method: " |
74 | "comp-addr=%p, comp-name=\"%s\"", | |
75 | component, bt_component_get_name(component)); | |
890882ef | 76 | ret = sink_class->methods.consume(bt_private_component_from_component(component)); |
be9ef048 PP |
77 | BT_LOGD("User method returned: status=%s", |
78 | bt_component_status_string(ret)); | |
79 | if (ret < 0) { | |
80 | BT_LOGW_STR("Consume method failed."); | |
81 | } | |
82 | ||
fa55ed98 JG |
83 | return ret; |
84 | } | |
34a9ed19 | 85 | |
544d0515 | 86 | int64_t bt_component_sink_get_input_port_count(struct bt_component *component) |
952ebade | 87 | { |
544d0515 | 88 | int64_t ret; |
952ebade | 89 | |
be9ef048 PP |
90 | if (!component) { |
91 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
92 | ret = (int64_t) -1; | |
93 | goto end; | |
94 | } | |
95 | ||
96 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
97 | BT_LOGW("Invalid parameter: component's class is not a sink component class: " | |
98 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
99 | component, bt_component_get_name(component), | |
100 | bt_component_class_type_string(component->class->type)); | |
101 | ret = (int64_t) -1; | |
952ebade JG |
102 | goto end; |
103 | } | |
104 | ||
be9ef048 | 105 | /* bt_component_get_input_port_count() logs details/errors */ |
544d0515 | 106 | ret = bt_component_get_input_port_count(component); |
be9ef048 | 107 | |
fec2a9f2 | 108 | end: |
544d0515 | 109 | return ret; |
fec2a9f2 JG |
110 | } |
111 | ||
9ac68eb1 | 112 | struct bt_port *bt_component_sink_get_input_port_by_name( |
366e034f | 113 | struct bt_component *component, const char *name) |
fec2a9f2 | 114 | { |
72b913fb | 115 | struct bt_port *port = NULL; |
fec2a9f2 | 116 | |
be9ef048 PP |
117 | if (!component) { |
118 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
119 | goto end; | |
120 | } | |
121 | ||
122 | if (!name) { | |
123 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
124 | goto end; | |
125 | } | |
126 | ||
127 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
128 | BT_LOGW("Invalid parameter: component's class is not a sink component class: " | |
129 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
130 | component, bt_component_get_name(component), | |
131 | bt_component_class_type_string(component->class->type)); | |
fec2a9f2 JG |
132 | goto end; |
133 | } | |
134 | ||
be9ef048 | 135 | /* bt_component_get_input_port_by_name() logs details/errors */ |
9ac68eb1 | 136 | port = bt_component_get_input_port_by_name(component, name); |
be9ef048 | 137 | |
fec2a9f2 | 138 | end: |
72b913fb | 139 | return port; |
fec2a9f2 JG |
140 | } |
141 | ||
9ac68eb1 PP |
142 | struct bt_port *bt_component_sink_get_input_port_by_index( |
143 | struct bt_component *component, uint64_t index) | |
fec2a9f2 | 144 | { |
366e034f | 145 | struct bt_port *port = NULL; |
fec2a9f2 | 146 | |
be9ef048 PP |
147 | if (!component) { |
148 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
149 | goto end; | |
150 | } | |
151 | ||
152 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
153 | BT_LOGW("Invalid parameter: component's class is not a sink component class: " | |
154 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
155 | component, bt_component_get_name(component), | |
156 | bt_component_class_type_string(component->class->type)); | |
fec2a9f2 JG |
157 | goto end; |
158 | } | |
159 | ||
be9ef048 | 160 | /* bt_component_get_input_port_by_index() logs details/errors */ |
9ac68eb1 | 161 | port = bt_component_get_input_port_by_index(component, index); |
be9ef048 | 162 | |
fec2a9f2 | 163 | end: |
366e034f | 164 | return port; |
fec2a9f2 JG |
165 | } |
166 | ||
890882ef | 167 | struct bt_private_port * |
9ac68eb1 PP |
168 | bt_private_component_sink_get_input_private_port_by_index( |
169 | struct bt_private_component *private_component, uint64_t index) | |
890882ef | 170 | { |
be9ef048 | 171 | /* bt_component_sink_get_input_port_by_index() logs details/errors */ |
890882ef | 172 | return bt_private_port_from_port( |
9ac68eb1 | 173 | bt_component_sink_get_input_port_by_index( |
6d137876 | 174 | bt_component_borrow_from_private(private_component), index)); |
890882ef PP |
175 | } |
176 | ||
b9d103be PP |
177 | struct bt_private_port * |
178 | bt_private_component_sink_get_input_private_port_by_name( | |
179 | struct bt_private_component *private_component, | |
180 | const char *name) | |
890882ef | 181 | { |
be9ef048 | 182 | /* bt_component_sink_get_input_port_by_name() logs details/errors */ |
890882ef | 183 | return bt_private_port_from_port( |
b9d103be | 184 | bt_component_sink_get_input_port_by_name( |
6d137876 | 185 | bt_component_borrow_from_private(private_component), name)); |
890882ef PP |
186 | } |
187 | ||
147337a3 | 188 | enum bt_component_status bt_private_component_sink_add_input_private_port( |
890882ef | 189 | struct bt_private_component *private_component, |
147337a3 PP |
190 | const char *name, void *user_data, |
191 | struct bt_private_port **user_priv_port) | |
366e034f | 192 | { |
147337a3 | 193 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
72b913fb | 194 | struct bt_port *port = NULL; |
890882ef | 195 | struct bt_component *component = |
6d137876 | 196 | bt_component_borrow_from_private(private_component); |
bd7cc15b | 197 | struct bt_graph *graph; |
fec2a9f2 | 198 | |
be9ef048 PP |
199 | if (!component) { |
200 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
147337a3 | 201 | status = BT_COMPONENT_STATUS_INVALID; |
be9ef048 PP |
202 | goto end; |
203 | } | |
204 | ||
205 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
206 | BT_LOGW("Invalid parameter: component's class is not a sink component class: " | |
207 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
208 | component, bt_component_get_name(component), | |
209 | bt_component_class_type_string(component->class->type)); | |
147337a3 | 210 | status = BT_COMPONENT_STATUS_INVALID; |
fec2a9f2 JG |
211 | goto end; |
212 | } | |
213 | ||
bd7cc15b PP |
214 | graph = bt_component_borrow_graph(component); |
215 | ||
216 | if (graph && bt_graph_is_canceled(graph)) { | |
43ca7dcc | 217 | BT_LOGW("Cannot add input port to sink component: graph is canceled: " |
bd7cc15b PP |
218 | "comp-addr=%p, comp-name=\"%s\", graph-addr=%p", |
219 | component, bt_component_get_name(component), | |
220 | bt_component_borrow_graph(component)); | |
221 | status = BT_COMPONENT_STATUS_GRAPH_IS_CANCELED; | |
222 | goto end; | |
223 | } | |
224 | ||
be9ef048 | 225 | /* bt_component_add_input_port() logs details/errors */ |
3e9b0023 | 226 | port = bt_component_add_input_port(component, name, user_data); |
147337a3 PP |
227 | if (!port) { |
228 | status = BT_COMPONENT_STATUS_NOMEM; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | if (user_priv_port) { | |
233 | /* Move reference to user */ | |
234 | *user_priv_port = bt_private_port_from_port(port); | |
235 | port = NULL; | |
236 | } | |
be9ef048 | 237 | |
fec2a9f2 | 238 | end: |
147337a3 PP |
239 | bt_put(port); |
240 | return status; | |
fec2a9f2 | 241 | } |