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