Commit | Line | Data |
---|---|---|
47e5a032 JG |
1 | /* |
2 | * iterator.c | |
3 | * | |
4 | * Babeltrace Notification Iterator | |
5 | * | |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3230ee6b | 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
47e5a032 JG |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
5af447e5 PP |
28 | #define BT_LOG_TAG "NOTIF-ITER" |
29 | #include <babeltrace/lib-logging-internal.h> | |
30 | ||
3d9990ac | 31 | #include <babeltrace/compiler-internal.h> |
65300d60 | 32 | #include <babeltrace/object.h> |
56e18c4c | 33 | #include <babeltrace/trace-ir/fields.h> |
56e18c4c PP |
34 | #include <babeltrace/trace-ir/event-internal.h> |
35 | #include <babeltrace/trace-ir/packet-internal.h> | |
36 | #include <babeltrace/trace-ir/stream-internal.h> | |
73d5c1ad | 37 | #include <babeltrace/graph/connection.h> |
bd14d768 | 38 | #include <babeltrace/graph/connection-internal.h> |
b2e0c907 | 39 | #include <babeltrace/graph/component.h> |
e5be10ef | 40 | #include <babeltrace/graph/component-internal.h> |
b2e0c907 PP |
41 | #include <babeltrace/graph/component-source-internal.h> |
42 | #include <babeltrace/graph/component-class-internal.h> | |
8ed535b5 PP |
43 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
44 | #include <babeltrace/graph/component-sink.h> | |
fa054faf | 45 | #include <babeltrace/graph/notification.h> |
b2e0c907 PP |
46 | #include <babeltrace/graph/notification-iterator.h> |
47 | #include <babeltrace/graph/notification-iterator-internal.h> | |
e7fa96c3 | 48 | #include <babeltrace/graph/notification-internal.h> |
3230ee6b PP |
49 | #include <babeltrace/graph/notification-event.h> |
50 | #include <babeltrace/graph/notification-event-internal.h> | |
51 | #include <babeltrace/graph/notification-packet.h> | |
52 | #include <babeltrace/graph/notification-packet-internal.h> | |
53 | #include <babeltrace/graph/notification-stream.h> | |
54 | #include <babeltrace/graph/notification-stream-internal.h> | |
55 | #include <babeltrace/graph/port.h> | |
8ed535b5 | 56 | #include <babeltrace/graph/graph-internal.h> |
c55a9f58 | 57 | #include <babeltrace/types.h> |
f6ccaed9 | 58 | #include <babeltrace/assert-internal.h> |
f42867e2 | 59 | #include <babeltrace/assert-pre-internal.h> |
fa054faf | 60 | #include <stdint.h> |
2ec84d26 | 61 | #include <inttypes.h> |
0fbb9a9f | 62 | #include <stdlib.h> |
3230ee6b | 63 | |
d4393e08 PP |
64 | /* |
65 | * TODO: Use graph's state (number of active iterators, etc.) and | |
66 | * possibly system specifications to make a better guess than this. | |
67 | */ | |
68 | #define NOTIF_BATCH_SIZE 15 | |
69 | ||
3230ee6b | 70 | struct stream_state { |
50842bdc PP |
71 | struct bt_stream *stream; /* owned by this */ |
72 | struct bt_packet *cur_packet; /* owned by this */ | |
f42867e2 | 73 | uint64_t expected_notif_seq_num; |
c55a9f58 | 74 | bt_bool is_ended; |
3230ee6b PP |
75 | }; |
76 | ||
26e21a82 | 77 | BT_ASSERT_PRE_FUNC |
3230ee6b PP |
78 | static |
79 | void destroy_stream_state(struct stream_state *stream_state) | |
80 | { | |
81 | if (!stream_state) { | |
82 | return; | |
83 | } | |
84 | ||
5af447e5 PP |
85 | BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state); |
86 | BT_LOGV_STR("Putting stream state's current packet."); | |
65300d60 | 87 | bt_object_put_ref(stream_state->cur_packet); |
5af447e5 | 88 | BT_LOGV_STR("Putting stream state's stream."); |
65300d60 | 89 | bt_object_put_ref(stream_state->stream); |
3230ee6b PP |
90 | g_free(stream_state); |
91 | } | |
92 | ||
26e21a82 | 93 | BT_ASSERT_PRE_FUNC |
3230ee6b | 94 | static |
50842bdc | 95 | struct stream_state *create_stream_state(struct bt_stream *stream) |
3230ee6b PP |
96 | { |
97 | struct stream_state *stream_state = g_new0(struct stream_state, 1); | |
98 | ||
99 | if (!stream_state) { | |
5af447e5 | 100 | BT_LOGE_STR("Failed to allocate one stream state."); |
3230ee6b PP |
101 | goto end; |
102 | } | |
103 | ||
104 | /* | |
f42867e2 | 105 | * We keep a reference to the stream until we know it's ended. |
3230ee6b | 106 | */ |
65300d60 | 107 | stream_state->stream = bt_object_get_ref(stream); |
5af447e5 PP |
108 | BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", " |
109 | "stream-state-addr=%p", | |
50842bdc | 110 | stream, bt_stream_get_name(stream), stream_state); |
3230ee6b PP |
111 | |
112 | end: | |
113 | return stream_state; | |
114 | } | |
47e5a032 | 115 | |
8ed535b5 PP |
116 | static |
117 | void destroy_base_notification_iterator(struct bt_object *obj) | |
118 | { | |
d4393e08 PP |
119 | struct bt_notification_iterator *iterator = (void *) obj; |
120 | ||
121 | BT_ASSERT(iterator); | |
122 | ||
123 | if (iterator->notifs) { | |
124 | g_ptr_array_free(iterator->notifs, TRUE); | |
125 | } | |
126 | ||
127 | g_free(iterator); | |
8ed535b5 PP |
128 | } |
129 | ||
47e5a032 | 130 | static |
90157d89 | 131 | void bt_private_connection_notification_iterator_destroy(struct bt_object *obj) |
47e5a032 | 132 | { |
90157d89 | 133 | struct bt_notification_iterator_private_connection *iterator; |
8738a040 | 134 | |
f6ccaed9 | 135 | BT_ASSERT(obj); |
d3eb6e8f | 136 | |
bd14d768 PP |
137 | /* |
138 | * The notification iterator's reference count is 0 if we're | |
139 | * here. Increment it to avoid a double-destroy (possibly | |
140 | * infinitely recursive). This could happen for example if the | |
65300d60 PP |
141 | * notification iterator's finalization function does bt_object_get_ref() |
142 | * (or anything that causes bt_object_get_ref() to be called) on itself | |
143 | * (ref. count goes from 0 to 1), and then bt_object_put_ref(): the | |
bd14d768 PP |
144 | * reference count would go from 1 to 0 again and this function |
145 | * would be called again. | |
146 | */ | |
3fea54f6 | 147 | obj->ref_count++; |
07245ac2 | 148 | iterator = (void *) obj; |
8ed535b5 | 149 | BT_LOGD("Destroying private connection notification iterator object: addr=%p", |
5af447e5 | 150 | iterator); |
90157d89 | 151 | bt_private_connection_notification_iterator_finalize(iterator); |
d3eb6e8f | 152 | |
3230ee6b PP |
153 | if (iterator->stream_states) { |
154 | /* | |
155 | * Remove our destroy listener from each stream which | |
156 | * has a state in this iterator. Otherwise the destroy | |
157 | * listener would be called with an invalid/other | |
158 | * notification iterator object. | |
159 | */ | |
3230ee6b PP |
160 | g_hash_table_destroy(iterator->stream_states); |
161 | } | |
162 | ||
bd14d768 PP |
163 | if (iterator->connection) { |
164 | /* | |
165 | * Remove ourself from the originating connection so | |
166 | * that it does not try to finalize a dangling pointer | |
167 | * later. | |
168 | */ | |
169 | bt_connection_remove_iterator(iterator->connection, iterator); | |
170 | } | |
171 | ||
8ed535b5 | 172 | destroy_base_notification_iterator(obj); |
47e5a032 JG |
173 | } |
174 | ||
bd14d768 | 175 | BT_HIDDEN |
90157d89 PP |
176 | void bt_private_connection_notification_iterator_finalize( |
177 | struct bt_notification_iterator_private_connection *iterator) | |
bd14d768 PP |
178 | { |
179 | struct bt_component_class *comp_class = NULL; | |
180 | bt_component_class_notification_iterator_finalize_method | |
181 | finalize_method = NULL; | |
182 | ||
f6ccaed9 | 183 | BT_ASSERT(iterator); |
bd14d768 PP |
184 | |
185 | switch (iterator->state) { | |
90157d89 | 186 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 PP |
187 | /* Skip user finalization if user initialization failed */ |
188 | BT_LOGD("Not finalizing non-initialized notification iterator: " | |
189 | "addr=%p", iterator); | |
190 | return; | |
90157d89 PP |
191 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED: |
192 | case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED: | |
bd14d768 | 193 | /* Already finalized */ |
5af447e5 PP |
194 | BT_LOGD("Not finalizing notification iterator: already finalized: " |
195 | "addr=%p", iterator); | |
bd14d768 PP |
196 | return; |
197 | default: | |
198 | break; | |
199 | } | |
200 | ||
5af447e5 PP |
201 | BT_LOGD("Finalizing notification iterator: addr=%p", iterator); |
202 | ||
90157d89 | 203 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) { |
5af447e5 | 204 | BT_LOGD("Updating notification iterator's state: " |
90157d89 PP |
205 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED"); |
206 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED; | |
df14f8af | 207 | } else { |
5af447e5 | 208 | BT_LOGD("Updating notification iterator's state: " |
90157d89 PP |
209 | "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED"); |
210 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED; | |
df14f8af MD |
211 | } |
212 | ||
f6ccaed9 | 213 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
214 | comp_class = iterator->upstream_component->class; |
215 | ||
216 | /* Call user-defined destroy method */ | |
217 | switch (comp_class->type) { | |
218 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
219 | { | |
220 | struct bt_component_class_source *source_class; | |
221 | ||
222 | source_class = container_of(comp_class, struct bt_component_class_source, parent); | |
223 | finalize_method = source_class->methods.iterator.finalize; | |
224 | break; | |
225 | } | |
226 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
227 | { | |
228 | struct bt_component_class_filter *filter_class; | |
229 | ||
230 | filter_class = container_of(comp_class, struct bt_component_class_filter, parent); | |
231 | finalize_method = filter_class->methods.iterator.finalize; | |
232 | break; | |
233 | } | |
234 | default: | |
235 | /* Unreachable */ | |
0fbb9a9f | 236 | abort(); |
bd14d768 PP |
237 | } |
238 | ||
239 | if (finalize_method) { | |
5af447e5 PP |
240 | BT_LOGD("Calling user's finalization method: addr=%p", |
241 | iterator); | |
bd14d768 | 242 | finalize_method( |
90157d89 | 243 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator)); |
bd14d768 PP |
244 | } |
245 | ||
bd14d768 PP |
246 | iterator->upstream_component = NULL; |
247 | iterator->upstream_port = NULL; | |
5af447e5 | 248 | BT_LOGD("Finalized notification iterator: addr=%p", iterator); |
bd14d768 PP |
249 | } |
250 | ||
251 | BT_HIDDEN | |
90157d89 PP |
252 | void bt_private_connection_notification_iterator_set_connection( |
253 | struct bt_notification_iterator_private_connection *iterator, | |
bd14d768 PP |
254 | struct bt_connection *connection) |
255 | { | |
f6ccaed9 | 256 | BT_ASSERT(iterator); |
bd14d768 | 257 | iterator->connection = connection; |
5af447e5 PP |
258 | BT_LOGV("Set notification iterator's connection: " |
259 | "iter-addr=%p, conn-addr=%p", iterator, connection); | |
bd14d768 PP |
260 | } |
261 | ||
90157d89 | 262 | static |
d4393e08 | 263 | int init_notification_iterator(struct bt_notification_iterator *iterator, |
90157d89 PP |
264 | enum bt_notification_iterator_type type, |
265 | bt_object_release_func destroy) | |
266 | { | |
d4393e08 PP |
267 | int ret = 0; |
268 | ||
3fea54f6 | 269 | bt_object_init_shared(&iterator->base, destroy); |
90157d89 | 270 | iterator->type = type; |
d4393e08 PP |
271 | iterator->notifs = g_ptr_array_new(); |
272 | if (!iterator->notifs) { | |
273 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
274 | ret = -1; | |
275 | goto end; | |
276 | } | |
277 | ||
278 | g_ptr_array_set_size(iterator->notifs, NOTIF_BATCH_SIZE); | |
279 | ||
280 | end: | |
281 | return ret; | |
90157d89 PP |
282 | } |
283 | ||
47e5a032 | 284 | BT_HIDDEN |
90157d89 | 285 | enum bt_connection_status bt_private_connection_notification_iterator_create( |
3230ee6b | 286 | struct bt_component *upstream_comp, |
fa054faf | 287 | struct bt_port *upstream_port, |
73d5c1ad | 288 | struct bt_connection *connection, |
90157d89 | 289 | struct bt_notification_iterator_private_connection **user_iterator) |
47e5a032 | 290 | { |
73d5c1ad | 291 | enum bt_connection_status status = BT_CONNECTION_STATUS_OK; |
d3e4dcd8 | 292 | enum bt_component_class_type type; |
90157d89 | 293 | struct bt_notification_iterator_private_connection *iterator = NULL; |
d4393e08 | 294 | int ret; |
47e5a032 | 295 | |
f6ccaed9 PP |
296 | BT_ASSERT(upstream_comp); |
297 | BT_ASSERT(upstream_port); | |
f6ccaed9 PP |
298 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
299 | BT_ASSERT(user_iterator); | |
8ed535b5 | 300 | BT_LOGD("Creating notification iterator on private connection: " |
5af447e5 PP |
301 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
302 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
303 | "conn-addr=%p", | |
304 | upstream_comp, bt_component_get_name(upstream_comp), | |
305 | upstream_port, bt_port_get_name(upstream_port), | |
306 | connection); | |
3230ee6b | 307 | type = bt_component_get_class_type(upstream_comp); |
f6ccaed9 | 308 | BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
ef2f7566 | 309 | type == BT_COMPONENT_CLASS_TYPE_FILTER); |
90157d89 | 310 | iterator = g_new0(struct bt_notification_iterator_private_connection, 1); |
47e5a032 | 311 | if (!iterator) { |
8ed535b5 | 312 | BT_LOGE_STR("Failed to allocate one private connection notification iterator."); |
73d5c1ad PP |
313 | status = BT_CONNECTION_STATUS_NOMEM; |
314 | goto end; | |
47e5a032 JG |
315 | } |
316 | ||
d4393e08 | 317 | ret = init_notification_iterator((void *) iterator, |
90157d89 PP |
318 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, |
319 | bt_private_connection_notification_iterator_destroy); | |
d4393e08 PP |
320 | if (ret) { |
321 | /* init_notification_iterator() logs errors */ | |
322 | status = BT_CONNECTION_STATUS_NOMEM; | |
323 | goto end; | |
324 | } | |
3230ee6b PP |
325 | |
326 | iterator->stream_states = g_hash_table_new_full(g_direct_hash, | |
327 | g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state); | |
328 | if (!iterator->stream_states) { | |
5af447e5 | 329 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
73d5c1ad PP |
330 | status = BT_CONNECTION_STATUS_NOMEM; |
331 | goto end; | |
3230ee6b PP |
332 | } |
333 | ||
bd14d768 PP |
334 | iterator->upstream_component = upstream_comp; |
335 | iterator->upstream_port = upstream_port; | |
336 | iterator->connection = connection; | |
5c563278 | 337 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
90157d89 | 338 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED; |
5af447e5 PP |
339 | BT_LOGD("Created notification iterator: " |
340 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
341 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
342 | "conn-addr=%p, iter-addr=%p", | |
343 | upstream_comp, bt_component_get_name(upstream_comp), | |
344 | upstream_port, bt_port_get_name(upstream_port), | |
345 | connection, iterator); | |
1a6a376a PP |
346 | |
347 | /* Move reference to user */ | |
348 | *user_iterator = iterator; | |
349 | iterator = NULL; | |
3230ee6b | 350 | |
47e5a032 | 351 | end: |
65300d60 | 352 | bt_object_put_ref(iterator); |
73d5c1ad | 353 | return status; |
47e5a032 JG |
354 | } |
355 | ||
90157d89 PP |
356 | void *bt_private_connection_private_notification_iterator_get_user_data( |
357 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
ea8d3e58 | 358 | { |
5c563278 | 359 | struct bt_notification_iterator_private_connection *iterator = (void *) |
6d137876 | 360 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
890882ef | 361 | |
f42867e2 PP |
362 | BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator"); |
363 | return iterator->user_data; | |
ea8d3e58 JG |
364 | } |
365 | ||
366 | enum bt_notification_iterator_status | |
90157d89 PP |
367 | bt_private_connection_private_notification_iterator_set_user_data( |
368 | struct bt_private_connection_private_notification_iterator *private_iterator, | |
890882ef | 369 | void *data) |
ea8d3e58 | 370 | { |
5c563278 | 371 | struct bt_notification_iterator_private_connection *iterator = (void *) |
6d137876 | 372 | bt_private_connection_notification_iterator_borrow_from_private(private_iterator); |
ea8d3e58 | 373 | |
f42867e2 | 374 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
ea8d3e58 | 375 | iterator->user_data = data; |
5af447e5 PP |
376 | BT_LOGV("Set notification iterator's user data: " |
377 | "iter-addr=%p, user-data-addr=%p", iterator, data); | |
f42867e2 | 378 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
8738a040 | 379 | } |
413bc2c4 | 380 | |
5c563278 PP |
381 | struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph( |
382 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
383 | { | |
384 | struct bt_notification_iterator_private_connection *iterator = (void *) | |
385 | bt_private_connection_notification_iterator_borrow_from_private( | |
386 | private_iterator); | |
387 | ||
388 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); | |
389 | return iterator->graph; | |
390 | } | |
391 | ||
f42867e2 PP |
392 | BT_ASSERT_PRE_FUNC |
393 | static inline | |
394 | void bt_notification_borrow_packet_stream(struct bt_notification *notif, | |
395 | struct bt_stream **stream, struct bt_packet **packet) | |
fa054faf | 396 | { |
f42867e2 | 397 | BT_ASSERT(notif); |
fa054faf | 398 | |
f42867e2 | 399 | switch (notif->type) { |
fa054faf | 400 | case BT_NOTIFICATION_TYPE_EVENT: |
f42867e2 PP |
401 | *packet = bt_event_borrow_packet( |
402 | bt_notification_event_borrow_event(notif)); | |
403 | *stream = bt_packet_borrow_stream(*packet); | |
fa054faf PP |
404 | break; |
405 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
f42867e2 | 406 | *stream = bt_notification_stream_begin_borrow_stream(notif); |
fa054faf PP |
407 | break; |
408 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
f42867e2 | 409 | *stream = bt_notification_stream_end_borrow_stream(notif); |
fa054faf PP |
410 | break; |
411 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
f42867e2 PP |
412 | *packet = bt_notification_packet_begin_borrow_packet(notif); |
413 | *stream = bt_packet_borrow_stream(*packet); | |
fa054faf PP |
414 | break; |
415 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
f42867e2 PP |
416 | *packet = bt_notification_packet_end_borrow_packet(notif); |
417 | *stream = bt_packet_borrow_stream(*packet); | |
2ec84d26 | 418 | break; |
fa054faf | 419 | default: |
f42867e2 | 420 | break; |
fa054faf | 421 | } |
fa054faf PP |
422 | } |
423 | ||
f42867e2 PP |
424 | BT_ASSERT_PRE_FUNC |
425 | static inline | |
426 | bool validate_notification( | |
90157d89 | 427 | struct bt_notification_iterator_private_connection *iterator, |
f42867e2 | 428 | struct bt_notification *notif) |
3230ee6b | 429 | { |
f42867e2 | 430 | bool is_valid = true; |
3230ee6b | 431 | struct stream_state *stream_state; |
f42867e2 PP |
432 | struct bt_stream *stream = NULL; |
433 | struct bt_packet *packet = NULL; | |
434 | ||
435 | BT_ASSERT(notif); | |
436 | bt_notification_borrow_packet_stream(notif, &stream, &packet); | |
437 | ||
438 | if (!stream) { | |
439 | /* we don't care about notifications not attached to streams */ | |
440 | goto end; | |
441 | } | |
3230ee6b | 442 | |
f42867e2 PP |
443 | stream_state = g_hash_table_lookup(iterator->stream_states, stream); |
444 | if (!stream_state) { | |
3230ee6b | 445 | /* |
f42867e2 PP |
446 | * No stream state for this stream: this notification |
447 | * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification | |
448 | * and its sequence number must be 0. | |
3230ee6b | 449 | */ |
f42867e2 PP |
450 | if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) { |
451 | BT_ASSERT_PRE_MSG("Unexpected notification: missing a " | |
452 | "BT_NOTIFICATION_TYPE_STREAM_BEGIN " | |
453 | "notification prior to this notification: " | |
454 | "%![stream-]+s", stream); | |
455 | is_valid = false; | |
3230ee6b PP |
456 | goto end; |
457 | } | |
458 | ||
f42867e2 PP |
459 | if (notif->seq_num == -1ULL) { |
460 | notif->seq_num = 0; | |
3230ee6b PP |
461 | } |
462 | ||
f42867e2 PP |
463 | if (notif->seq_num != 0) { |
464 | BT_ASSERT_PRE_MSG("Unexpected notification sequence " | |
465 | "number for this notification iterator: " | |
466 | "this is the first notification for this " | |
467 | "stream, expecting sequence number 0: " | |
468 | "seq-num=%" PRIu64 ", %![stream-]+s", | |
469 | notif->seq_num, stream); | |
470 | is_valid = false; | |
3230ee6b | 471 | goto end; |
3230ee6b | 472 | } |
3230ee6b | 473 | |
f42867e2 PP |
474 | stream_state = create_stream_state(stream); |
475 | if (!stream_state) { | |
476 | abort(); | |
477 | } | |
fa054faf | 478 | |
f42867e2 PP |
479 | g_hash_table_insert(iterator->stream_states, stream, |
480 | stream_state); | |
481 | stream_state->expected_notif_seq_num++; | |
482 | goto end; | |
fa054faf PP |
483 | } |
484 | ||
f42867e2 PP |
485 | if (stream_state->is_ended) { |
486 | /* | |
487 | * There's a new notification which has a reference to a | |
488 | * stream which, from this iterator's point of view, is | |
489 | * ended ("end of stream" notification was returned). | |
490 | * This is bad: the API guarantees that it can never | |
491 | * happen. | |
492 | */ | |
493 | BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s", | |
494 | stream); | |
495 | is_valid = false; | |
fa054faf PP |
496 | goto end; |
497 | } | |
498 | ||
f42867e2 PP |
499 | if (notif->seq_num == -1ULL) { |
500 | notif->seq_num = stream_state->expected_notif_seq_num; | |
3230ee6b PP |
501 | } |
502 | ||
f42867e2 PP |
503 | if (notif->seq_num != -1ULL && |
504 | notif->seq_num != stream_state->expected_notif_seq_num) { | |
505 | BT_ASSERT_PRE_MSG("Unexpected notification sequence number: " | |
506 | "seq-num=%" PRIu64 ", " | |
507 | "expected-seq-num=%" PRIu64 ", %![stream-]+s", | |
508 | notif->seq_num, stream_state->expected_notif_seq_num, | |
509 | stream); | |
510 | is_valid = false; | |
fa054faf PP |
511 | goto end; |
512 | } | |
513 | ||
f42867e2 PP |
514 | switch (notif->type) { |
515 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
516 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN " | |
517 | "notification at this point: notif-seq-num=%" PRIu64 ", " | |
518 | "%![stream-]+s", notif->seq_num, stream); | |
519 | is_valid = false; | |
520 | goto end; | |
521 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
522 | if (stream_state->cur_packet) { | |
523 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END " | |
524 | "notification: missing a " | |
525 | "BT_NOTIFICATION_TYPE_PACKET_END notification " | |
526 | "prior to this notification: " | |
527 | "notif-seq-num=%" PRIu64 ", " | |
528 | "%![stream-]+s", notif->seq_num, stream); | |
529 | is_valid = false; | |
530 | goto end; | |
531 | } | |
532 | stream_state->expected_notif_seq_num++; | |
533 | stream_state->is_ended = true; | |
534 | goto end; | |
535 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
536 | if (stream_state->cur_packet) { | |
537 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN " | |
538 | "notification at this point: missing a " | |
539 | "BT_NOTIFICATION_TYPE_PACKET_END notification " | |
540 | "prior to this notification: " | |
541 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
542 | "%![packet-]+a", notif->seq_num, stream, | |
543 | packet); | |
544 | is_valid = false; | |
545 | goto end; | |
546 | } | |
547 | stream_state->expected_notif_seq_num++; | |
65300d60 | 548 | stream_state->cur_packet = bt_object_get_ref(packet); |
f42867e2 PP |
549 | goto end; |
550 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
551 | if (!stream_state->cur_packet) { | |
552 | BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END " | |
553 | "notification at this point: missing a " | |
554 | "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification " | |
555 | "prior to this notification: " | |
556 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
557 | "%![packet-]+a", notif->seq_num, stream, | |
558 | packet); | |
559 | is_valid = false; | |
560 | goto end; | |
561 | } | |
562 | stream_state->expected_notif_seq_num++; | |
65300d60 | 563 | BT_OBJECT_PUT_REF_AND_RESET(stream_state->cur_packet); |
f42867e2 PP |
564 | goto end; |
565 | case BT_NOTIFICATION_TYPE_EVENT: | |
566 | if (packet != stream_state->cur_packet) { | |
567 | BT_ASSERT_PRE_MSG("Unexpected packet for " | |
568 | "BT_NOTIFICATION_TYPE_EVENT notification: " | |
569 | "notif-seq-num=%" PRIu64 ", %![stream-]+s, " | |
570 | "%![notif-packet-]+a, %![expected-packet-]+a", | |
571 | notif->seq_num, stream, | |
572 | stream_state->cur_packet, packet); | |
573 | is_valid = false; | |
574 | goto end; | |
575 | } | |
576 | stream_state->expected_notif_seq_num++; | |
577 | goto end; | |
578 | default: | |
579 | break; | |
3230ee6b PP |
580 | } |
581 | ||
3230ee6b | 582 | end: |
f42867e2 | 583 | return is_valid; |
3230ee6b PP |
584 | } |
585 | ||
d4393e08 PP |
586 | BT_ASSERT_PRE_FUNC |
587 | static inline | |
588 | bool validate_notifications( | |
589 | struct bt_notification_iterator_private_connection *iterator, | |
590 | uint64_t count) | |
591 | { | |
592 | bool ret = true; | |
593 | bt_notification_array notifs = (void *) iterator->base.notifs->pdata; | |
594 | uint64_t i; | |
595 | ||
596 | for (i = 0; i < count; i++) { | |
597 | ret = validate_notification(iterator, notifs[i]); | |
598 | if (!ret) { | |
599 | break; | |
600 | } | |
601 | } | |
602 | ||
603 | return ret; | |
604 | } | |
605 | ||
f42867e2 PP |
606 | BT_ASSERT_PRE_FUNC |
607 | static inline bool priv_conn_notif_iter_can_end( | |
608 | struct bt_notification_iterator_private_connection *iterator) | |
3230ee6b | 609 | { |
f42867e2 PP |
610 | GHashTableIter iter; |
611 | gpointer stream_key, state_value; | |
612 | bool ret = true; | |
3230ee6b | 613 | |
f42867e2 PP |
614 | /* |
615 | * Verify that this iterator received a | |
616 | * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream | |
617 | * which has a state. | |
618 | */ | |
3230ee6b | 619 | |
f42867e2 | 620 | g_hash_table_iter_init(&iter, iterator->stream_states); |
3230ee6b | 621 | |
f42867e2 PP |
622 | while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) { |
623 | struct stream_state *stream_state = (void *) state_value; | |
3230ee6b | 624 | |
f42867e2 PP |
625 | BT_ASSERT(stream_state); |
626 | BT_ASSERT(stream_key); | |
fa054faf | 627 | |
f42867e2 PP |
628 | if (!stream_state->is_ended) { |
629 | BT_ASSERT_PRE_MSG("Ending notification iterator, " | |
630 | "but stream is not ended: " | |
631 | "%![stream-]s", stream_key); | |
632 | ret = false; | |
633 | goto end; | |
634 | } | |
3230ee6b PP |
635 | } |
636 | ||
3230ee6b | 637 | end: |
3230ee6b PP |
638 | return ret; |
639 | } | |
640 | ||
f42867e2 | 641 | enum bt_notification_iterator_status |
07245ac2 PP |
642 | bt_private_connection_notification_iterator_next( |
643 | struct bt_notification_iterator *user_iterator, | |
d4393e08 | 644 | struct bt_notification ***user_notifs, uint64_t *user_count) |
3230ee6b | 645 | { |
07245ac2 PP |
646 | struct bt_notification_iterator_private_connection *iterator = |
647 | (void *) user_iterator; | |
f42867e2 PP |
648 | struct bt_private_connection_private_notification_iterator *priv_iterator = |
649 | bt_private_connection_private_notification_iterator_from_notification_iterator(iterator); | |
650 | bt_component_class_notification_iterator_next_method next_method = NULL; | |
f42867e2 PP |
651 | enum bt_notification_iterator_status status = |
652 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
3230ee6b | 653 | |
07245ac2 | 654 | BT_ASSERT_PRE_NON_NULL(user_iterator, "Notification iterator"); |
d4393e08 PP |
655 | BT_ASSERT_PRE_NON_NULL(user_notifs, "Notification array"); |
656 | BT_ASSERT_PRE_NON_NULL(user_count, "Notification count"); | |
07245ac2 PP |
657 | BT_ASSERT_PRE(user_iterator->type == |
658 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, | |
659 | "Notification iterator was not created from a private connection: " | |
660 | "%!+i", iterator); | |
661 | BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i", | |
f42867e2 PP |
662 | iterator); |
663 | BT_ASSERT_PRE(iterator->state == | |
664 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE, | |
665 | "Notification iterator's \"next\" called, but " | |
666 | "iterator is in the wrong state: %!+i", iterator); | |
667 | BT_ASSERT(iterator->upstream_component); | |
668 | BT_ASSERT(iterator->upstream_component->class); | |
d3eb6e8f | 669 | |
3230ee6b PP |
670 | /* Pick the appropriate "next" method */ |
671 | switch (iterator->upstream_component->class->type) { | |
d3eb6e8f PP |
672 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
673 | { | |
674 | struct bt_component_class_source *source_class = | |
3230ee6b | 675 | container_of(iterator->upstream_component->class, |
d3eb6e8f PP |
676 | struct bt_component_class_source, parent); |
677 | ||
f6ccaed9 | 678 | BT_ASSERT(source_class->methods.iterator.next); |
d3eb6e8f PP |
679 | next_method = source_class->methods.iterator.next; |
680 | break; | |
681 | } | |
682 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
683 | { | |
684 | struct bt_component_class_filter *filter_class = | |
3230ee6b | 685 | container_of(iterator->upstream_component->class, |
d3eb6e8f PP |
686 | struct bt_component_class_filter, parent); |
687 | ||
f6ccaed9 | 688 | BT_ASSERT(filter_class->methods.iterator.next); |
d3eb6e8f PP |
689 | next_method = filter_class->methods.iterator.next; |
690 | break; | |
691 | } | |
692 | default: | |
0fbb9a9f | 693 | abort(); |
d3eb6e8f PP |
694 | } |
695 | ||
3230ee6b PP |
696 | /* |
697 | * Call the user's "next" method to get the next notification | |
fa054faf | 698 | * and status. |
3230ee6b | 699 | */ |
f6ccaed9 | 700 | BT_ASSERT(next_method); |
f42867e2 | 701 | BT_LOGD_STR("Calling user's \"next\" method."); |
d4393e08 PP |
702 | status = next_method(priv_iterator, |
703 | (void *) user_iterator->notifs->pdata, | |
704 | NOTIF_BATCH_SIZE, user_count); | |
f42867e2 | 705 | BT_LOGD("User method returned: status=%s", |
d4393e08 PP |
706 | bt_notification_iterator_status_string(status)); |
707 | if (status < 0) { | |
f42867e2 | 708 | BT_LOGW_STR("User method failed."); |
f42867e2 PP |
709 | goto end; |
710 | } | |
3230ee6b | 711 | |
f42867e2 PP |
712 | if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED || |
713 | iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) { | |
714 | /* | |
715 | * The user's "next" method, somehow, cancelled its own | |
716 | * notification iterator. This can happen, for example, | |
717 | * when the user's method removes the port on which | |
718 | * there's the connection from which the iterator was | |
719 | * created. In this case, said connection is ended, and | |
720 | * all its notification iterators are finalized. | |
721 | * | |
65300d60 | 722 | * Only bt_object_put_ref() the returned notification if |
f42867e2 PP |
723 | * the status is |
724 | * BT_NOTIFICATION_ITERATOR_STATUS_OK because | |
725 | * otherwise this field could be garbage. | |
726 | */ | |
d4393e08 PP |
727 | if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
728 | uint64_t i; | |
729 | bt_notification_array notifs = | |
730 | (void *) user_iterator->notifs->pdata; | |
731 | ||
732 | for (i = 0; i < *user_count; i++) { | |
65300d60 | 733 | bt_object_put_ref(notifs[i]); |
d4393e08 | 734 | } |
3230ee6b PP |
735 | } |
736 | ||
f42867e2 PP |
737 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; |
738 | goto end; | |
739 | } | |
8cf27cc5 | 740 | |
d4393e08 PP |
741 | switch (status) { |
742 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
743 | BT_ASSERT_PRE(validate_notifications(iterator, *user_count), | |
744 | "Notifications are invalid at this point: " | |
745 | "%![notif-iter-]+i, count=%" PRIu64, | |
746 | iterator, *user_count); | |
747 | *user_notifs = (void *) user_iterator->notifs->pdata; | |
748 | break; | |
749 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
750 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; | |
751 | goto end; | |
f42867e2 PP |
752 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
753 | BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator), | |
754 | "Notification iterator cannot end at this point: " | |
755 | "%!+i", iterator); | |
756 | BT_ASSERT(iterator->state == | |
757 | BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE); | |
758 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED; | |
759 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
760 | BT_LOGD("Set new status: status=%s", | |
761 | bt_notification_iterator_status_string(status)); | |
762 | goto end; | |
f42867e2 PP |
763 | default: |
764 | /* Unknown non-error status */ | |
765 | abort(); | |
41a2b7ae PP |
766 | } |
767 | ||
768 | end: | |
3230ee6b PP |
769 | return status; |
770 | } | |
771 | ||
772 | enum bt_notification_iterator_status | |
07245ac2 PP |
773 | bt_output_port_notification_iterator_next( |
774 | struct bt_notification_iterator *iterator, | |
d4393e08 PP |
775 | bt_notification_array *notifs_to_user, |
776 | uint64_t *count_to_user) | |
3230ee6b PP |
777 | { |
778 | enum bt_notification_iterator_status status; | |
07245ac2 PP |
779 | struct bt_notification_iterator_output_port *out_port_iter = |
780 | (void *) iterator; | |
781 | enum bt_graph_status graph_status; | |
3230ee6b | 782 | |
f42867e2 | 783 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
d4393e08 PP |
784 | BT_ASSERT_PRE_NON_NULL(notifs_to_user, "Notification array"); |
785 | BT_ASSERT_PRE_NON_NULL(count_to_user, "Notification count"); | |
07245ac2 PP |
786 | BT_ASSERT_PRE(iterator->type == |
787 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, | |
788 | "Notification iterator was not created from an output port: " | |
789 | "%!+i", iterator); | |
790 | BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i", | |
791 | iterator); | |
d4393e08 | 792 | |
07245ac2 PP |
793 | graph_status = bt_graph_consume_sink_no_check( |
794 | out_port_iter->graph, out_port_iter->colander); | |
795 | switch (graph_status) { | |
796 | case BT_GRAPH_STATUS_CANCELED: | |
07245ac2 | 797 | status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED; |
8ed535b5 | 798 | break; |
07245ac2 | 799 | case BT_GRAPH_STATUS_AGAIN: |
07245ac2 PP |
800 | status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN; |
801 | break; | |
802 | case BT_GRAPH_STATUS_END: | |
07245ac2 PP |
803 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
804 | break; | |
805 | case BT_GRAPH_STATUS_NOMEM: | |
07245ac2 PP |
806 | status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
807 | break; | |
808 | case BT_GRAPH_STATUS_OK: | |
07245ac2 | 809 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
d4393e08 PP |
810 | |
811 | /* | |
812 | * On success, the colander sink moves the notifications | |
813 | * to this iterator's array and sets this iterator's | |
814 | * notification count: move them to the user. | |
815 | */ | |
816 | *notifs_to_user = (void *) iterator->notifs->pdata; | |
817 | *count_to_user = out_port_iter->count; | |
90157d89 | 818 | break; |
90157d89 | 819 | default: |
07245ac2 PP |
820 | /* Other errors */ |
821 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
90157d89 | 822 | } |
3230ee6b | 823 | |
3230ee6b | 824 | return status; |
53d45b87 JG |
825 | } |
826 | ||
90157d89 | 827 | struct bt_component *bt_private_connection_notification_iterator_get_component( |
413bc2c4 JG |
828 | struct bt_notification_iterator *iterator) |
829 | { | |
90157d89 PP |
830 | struct bt_notification_iterator_private_connection *iter_priv_conn; |
831 | ||
f42867e2 PP |
832 | BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator"); |
833 | BT_ASSERT_PRE(iterator->type == | |
834 | BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION, | |
835 | "Notification iterator was not created from a private connection: " | |
836 | "%!+i", iterator); | |
90157d89 | 837 | iter_priv_conn = (void *) iterator; |
65300d60 | 838 | return bt_object_get_ref(iter_priv_conn->upstream_component); |
413bc2c4 JG |
839 | } |
840 | ||
91457551 | 841 | struct bt_private_component * |
90157d89 PP |
842 | bt_private_connection_private_notification_iterator_get_private_component( |
843 | struct bt_private_connection_private_notification_iterator *private_iterator) | |
91457551 PP |
844 | { |
845 | return bt_private_component_from_component( | |
90157d89 | 846 | bt_private_connection_notification_iterator_get_component( |
6d137876 | 847 | (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator))); |
91457551 | 848 | } |
8ed535b5 PP |
849 | |
850 | static | |
851 | void bt_output_port_notification_iterator_destroy(struct bt_object *obj) | |
852 | { | |
853 | struct bt_notification_iterator_output_port *iterator = | |
854 | (void *) container_of(obj, struct bt_notification_iterator, base); | |
855 | ||
856 | BT_LOGD("Destroying output port notification iterator object: addr=%p", | |
857 | iterator); | |
858 | BT_LOGD_STR("Putting graph."); | |
65300d60 | 859 | bt_object_put_ref(iterator->graph); |
8ed535b5 | 860 | BT_LOGD_STR("Putting colander sink component."); |
65300d60 | 861 | bt_object_put_ref(iterator->colander); |
8ed535b5 PP |
862 | destroy_base_notification_iterator(obj); |
863 | } | |
864 | ||
865 | struct bt_notification_iterator *bt_output_port_notification_iterator_create( | |
866 | struct bt_port *output_port, | |
f42867e2 | 867 | const char *colander_component_name) |
8ed535b5 | 868 | { |
8ed535b5 PP |
869 | struct bt_notification_iterator_output_port *iterator = NULL; |
870 | struct bt_component_class *colander_comp_cls = NULL; | |
871 | struct bt_component *output_port_comp = NULL; | |
872 | struct bt_component *colander_comp; | |
873 | struct bt_graph *graph = NULL; | |
874 | enum bt_graph_status graph_status; | |
875 | const char *colander_comp_name; | |
876 | struct bt_port *colander_in_port = NULL; | |
877 | struct bt_component_class_sink_colander_data colander_data; | |
d4393e08 | 878 | int ret; |
8ed535b5 | 879 | |
f42867e2 PP |
880 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
881 | BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT, | |
882 | "Port is not an output port: %!+p", output_port); | |
8ed535b5 | 883 | output_port_comp = bt_port_get_component(output_port); |
f42867e2 PP |
884 | BT_ASSERT_PRE(output_port_comp, |
885 | "Output port has no component: %!+p", output_port); | |
e5be10ef | 886 | graph = bt_object_get_ref(bt_component_borrow_graph(output_port_comp)); |
f6ccaed9 | 887 | BT_ASSERT(graph); |
8ed535b5 PP |
888 | |
889 | /* Create notification iterator */ | |
890 | BT_LOGD("Creating notification iterator on output port: " | |
891 | "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"", | |
892 | output_port_comp, bt_component_get_name(output_port_comp), | |
893 | output_port, bt_port_get_name(output_port)); | |
894 | iterator = g_new0(struct bt_notification_iterator_output_port, 1); | |
895 | if (!iterator) { | |
896 | BT_LOGE_STR("Failed to allocate one output port notification iterator."); | |
897 | goto error; | |
898 | } | |
899 | ||
d4393e08 | 900 | ret = init_notification_iterator((void *) iterator, |
8ed535b5 PP |
901 | BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT, |
902 | bt_output_port_notification_iterator_destroy); | |
d4393e08 PP |
903 | if (ret) { |
904 | /* init_notification_iterator() logs errors */ | |
65300d60 | 905 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
906 | goto end; |
907 | } | |
8ed535b5 PP |
908 | |
909 | /* Create colander component */ | |
910 | colander_comp_cls = bt_component_class_sink_colander_get(); | |
911 | if (!colander_comp_cls) { | |
912 | BT_LOGW("Cannot get colander sink component class."); | |
913 | goto error; | |
914 | } | |
915 | ||
65300d60 | 916 | BT_OBJECT_MOVE_REF(iterator->graph, graph); |
8ed535b5 PP |
917 | colander_comp_name = |
918 | colander_component_name ? colander_component_name : "colander"; | |
d4393e08 PP |
919 | colander_data.notifs = (void *) iterator->base.notifs->pdata; |
920 | colander_data.count_addr = &iterator->count; | |
921 | ||
8ed535b5 PP |
922 | graph_status = bt_graph_add_component_with_init_method_data( |
923 | iterator->graph, colander_comp_cls, colander_comp_name, | |
924 | NULL, &colander_data, &iterator->colander); | |
925 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
926 | BT_LOGW("Cannot add colander sink component to graph: " | |
927 | "graph-addr=%p, name=\"%s\", graph-status=%s", | |
928 | iterator->graph, colander_comp_name, | |
929 | bt_graph_status_string(graph_status)); | |
930 | goto error; | |
931 | } | |
932 | ||
933 | /* | |
934 | * Connect provided output port to the colander component's | |
935 | * input port. | |
936 | */ | |
937 | colander_in_port = bt_component_sink_get_input_port_by_index( | |
938 | iterator->colander, 0); | |
f6ccaed9 | 939 | BT_ASSERT(colander_in_port); |
8ed535b5 PP |
940 | graph_status = bt_graph_connect_ports(iterator->graph, |
941 | output_port, colander_in_port, NULL); | |
942 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
943 | BT_LOGW("Cannot add colander sink component to graph: " | |
944 | "graph-addr=%p, name=\"%s\", graph-status=%s", | |
945 | iterator->graph, colander_comp_name, | |
946 | bt_graph_status_string(graph_status)); | |
947 | goto error; | |
948 | } | |
949 | ||
950 | /* | |
951 | * At this point everything went fine. Make the graph | |
952 | * nonconsumable forever so that only this notification iterator | |
953 | * can consume (thanks to bt_graph_consume_sink_no_check()). | |
954 | * This avoids leaking the notification created by the colander | |
07245ac2 PP |
955 | * sink and moved to the notification iterator's notification |
956 | * member. | |
8ed535b5 PP |
957 | */ |
958 | bt_graph_set_can_consume(iterator->graph, BT_FALSE); | |
959 | goto end; | |
960 | ||
961 | error: | |
962 | if (iterator && iterator->graph && iterator->colander) { | |
963 | int ret; | |
964 | ||
965 | /* Remove created colander component from graph if any */ | |
966 | colander_comp = iterator->colander; | |
65300d60 | 967 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
8ed535b5 PP |
968 | |
969 | /* | |
970 | * At this point the colander component's reference | |
971 | * count is 0 because iterator->colander was the only | |
972 | * owner. We also know that it is not connected because | |
973 | * this is the last operation before this function | |
974 | * succeeds. | |
975 | * | |
976 | * Since we honor the preconditions here, | |
977 | * bt_graph_remove_unconnected_component() always | |
978 | * succeeds. | |
979 | */ | |
980 | ret = bt_graph_remove_unconnected_component(iterator->graph, | |
981 | colander_comp); | |
f6ccaed9 | 982 | BT_ASSERT(ret == 0); |
8ed535b5 PP |
983 | } |
984 | ||
65300d60 | 985 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
8ed535b5 PP |
986 | |
987 | end: | |
65300d60 PP |
988 | bt_object_put_ref(colander_in_port); |
989 | bt_object_put_ref(colander_comp_cls); | |
990 | bt_object_put_ref(output_port_comp); | |
991 | bt_object_put_ref(graph); | |
8ed535b5 PP |
992 | return (void *) iterator; |
993 | } | |
25b68514 PP |
994 | |
995 | struct bt_notification_iterator * | |
5c563278 | 996 | bt_private_connection_notification_iterator_borrow_from_private( |
25b68514 PP |
997 | struct bt_private_connection_private_notification_iterator *private_notification_iterator) |
998 | { | |
5c563278 | 999 | return (void *) private_notification_iterator; |
25b68514 | 1000 | } |