Commit | Line | Data |
---|---|---|
47e5a032 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
47e5a032 | 3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
47e5a032 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/MSG-ITER" |
c2d9d9cf | 25 | #include "lib/logging.h" |
5af447e5 | 26 | |
578e048b | 27 | #include "compat/compiler.h" |
e74dbb33 | 28 | #include "compat/glib.h" |
578e048b MJ |
29 | #include "lib/trace-ir/clock-class.h" |
30 | #include "lib/trace-ir/clock-snapshot.h" | |
3fadfbc0 MJ |
31 | #include <babeltrace2/trace-ir/field.h> |
32 | #include <babeltrace2/trace-ir/event-const.h> | |
578e048b | 33 | #include "lib/trace-ir/event.h" |
3fadfbc0 | 34 | #include <babeltrace2/trace-ir/packet-const.h> |
578e048b MJ |
35 | #include "lib/trace-ir/packet.h" |
36 | #include "lib/trace-ir/stream.h" | |
3fadfbc0 | 37 | #include <babeltrace2/graph/connection-const.h> |
3fadfbc0 | 38 | #include <babeltrace2/graph/component-const.h> |
3fadfbc0 | 39 | #include <babeltrace2/graph/component-sink-const.h> |
3fadfbc0 MJ |
40 | #include <babeltrace2/graph/message-const.h> |
41 | #include <babeltrace2/graph/message-iterator-const.h> | |
3fadfbc0 MJ |
42 | #include <babeltrace2/graph/self-component-port-input-message-iterator.h> |
43 | #include <babeltrace2/graph/port-output-message-iterator.h> | |
3fadfbc0 | 44 | #include <babeltrace2/graph/message-event-const.h> |
5b7b55be SM |
45 | #include <babeltrace2/graph/message-message-iterator-inactivity-const.h> |
46 | #include <babeltrace2/graph/message-packet-beginning.h> | |
3fadfbc0 MJ |
47 | #include <babeltrace2/graph/message-packet-beginning-const.h> |
48 | #include <babeltrace2/graph/message-packet-end-const.h> | |
5b7b55be SM |
49 | #include <babeltrace2/graph/message-stream-activity-beginning.h> |
50 | #include <babeltrace2/graph/message-stream-activity-beginning-const.h> | |
51 | #include <babeltrace2/graph/message-stream-activity-end-const.h> | |
52 | #include <babeltrace2/graph/message-stream-beginning.h> | |
3fadfbc0 MJ |
53 | #include <babeltrace2/graph/message-stream-beginning-const.h> |
54 | #include <babeltrace2/graph/message-stream-end-const.h> | |
3fadfbc0 MJ |
55 | #include <babeltrace2/graph/port-const.h> |
56 | #include <babeltrace2/graph/graph.h> | |
57 | #include <babeltrace2/graph/graph-const.h> | |
3fadfbc0 | 58 | #include <babeltrace2/types.h> |
578e048b MJ |
59 | #include "common/assert.h" |
60 | #include "lib/assert-pre.h" | |
fa054faf | 61 | #include <stdint.h> |
2ec84d26 | 62 | #include <inttypes.h> |
0fbb9a9f | 63 | #include <stdlib.h> |
3230ee6b | 64 | |
578e048b MJ |
65 | #include "component-class.h" |
66 | #include "component-class-sink-colander.h" | |
67 | #include "component.h" | |
68 | #include "component-sink.h" | |
69 | #include "component-source.h" | |
70 | #include "connection.h" | |
71 | #include "graph.h" | |
72 | #include "message/discarded-items.h" | |
73 | #include "message/event.h" | |
74 | #include "message/iterator.h" | |
75 | #include "message/message.h" | |
76 | #include "message/message-iterator-inactivity.h" | |
77 | #include "message/stream.h" | |
78 | #include "message/packet.h" | |
79 | #include "message/stream-activity.h" | |
80 | ||
d4393e08 PP |
81 | /* |
82 | * TODO: Use graph's state (number of active iterators, etc.) and | |
83 | * possibly system specifications to make a better guess than this. | |
84 | */ | |
d6e69534 | 85 | #define MSG_BATCH_SIZE 15 |
d4393e08 | 86 | |
7474e7d3 PP |
87 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
88 | BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \ | |
89 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \ | |
90 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ | |
91 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ | |
92 | "Message iterator is in the wrong state: %!+i", _iter) | |
47e5a032 | 93 | |
d0fea130 | 94 | static inline |
003e713f | 95 | void set_self_comp_port_input_msg_iterator_state( |
d0fea130 PP |
96 | struct bt_self_component_port_input_message_iterator *iterator, |
97 | enum bt_self_component_port_input_message_iterator_state state) | |
98 | { | |
99 | BT_ASSERT(iterator); | |
7474e7d3 | 100 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
d0fea130 PP |
101 | bt_self_component_port_input_message_iterator_state_string(state)); |
102 | iterator->state = state; | |
103 | } | |
104 | ||
8ed535b5 | 105 | static |
d6e69534 | 106 | void destroy_base_message_iterator(struct bt_object *obj) |
8ed535b5 | 107 | { |
d6e69534 | 108 | struct bt_message_iterator *iterator = (void *) obj; |
d4393e08 PP |
109 | |
110 | BT_ASSERT(iterator); | |
111 | ||
d6e69534 PP |
112 | if (iterator->msgs) { |
113 | g_ptr_array_free(iterator->msgs, TRUE); | |
114 | iterator->msgs = NULL; | |
d4393e08 PP |
115 | } |
116 | ||
117 | g_free(iterator); | |
8ed535b5 PP |
118 | } |
119 | ||
47e5a032 | 120 | static |
d6e69534 | 121 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
47e5a032 | 122 | { |
d6e69534 | 123 | struct bt_self_component_port_input_message_iterator *iterator; |
8738a040 | 124 | |
f6ccaed9 | 125 | BT_ASSERT(obj); |
d3eb6e8f | 126 | |
bd14d768 | 127 | /* |
d6e69534 | 128 | * The message iterator's reference count is 0 if we're |
bd14d768 PP |
129 | * here. Increment it to avoid a double-destroy (possibly |
130 | * infinitely recursive). This could happen for example if the | |
d6e69534 | 131 | * message iterator's finalization function does |
d94d92ac PP |
132 | * bt_object_get_ref() (or anything that causes |
133 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
134 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
135 | * count would go from 1 to 0 again and this function would be | |
136 | * called again. | |
bd14d768 | 137 | */ |
3fea54f6 | 138 | obj->ref_count++; |
07245ac2 | 139 | iterator = (void *) obj; |
3f7d4d90 | 140 | BT_LIB_LOGI("Destroying self component input port message iterator object: " |
d94d92ac | 141 | "%!+i", iterator); |
d0fea130 | 142 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
d3eb6e8f | 143 | |
bd14d768 PP |
144 | if (iterator->connection) { |
145 | /* | |
146 | * Remove ourself from the originating connection so | |
147 | * that it does not try to finalize a dangling pointer | |
148 | * later. | |
149 | */ | |
150 | bt_connection_remove_iterator(iterator->connection, iterator); | |
d94d92ac | 151 | iterator->connection = NULL; |
bd14d768 PP |
152 | } |
153 | ||
da9c4c52 SM |
154 | if (iterator->auto_seek.msgs) { |
155 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { | |
5b9e151d | 156 | bt_object_put_no_null_check( |
da9c4c52 | 157 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
7474e7d3 PP |
158 | } |
159 | ||
da9c4c52 SM |
160 | g_queue_free(iterator->auto_seek.msgs); |
161 | iterator->auto_seek.msgs = NULL; | |
7474e7d3 PP |
162 | } |
163 | ||
d6e69534 | 164 | destroy_base_message_iterator(obj); |
47e5a032 JG |
165 | } |
166 | ||
bd14d768 | 167 | BT_HIDDEN |
d0fea130 | 168 | void bt_self_component_port_input_message_iterator_try_finalize( |
d6e69534 | 169 | struct bt_self_component_port_input_message_iterator *iterator) |
bd14d768 | 170 | { |
d94d92ac PP |
171 | typedef void (*method_t)(void *); |
172 | ||
bd14d768 | 173 | struct bt_component_class *comp_class = NULL; |
d94d92ac | 174 | method_t method = NULL; |
bd14d768 | 175 | |
f6ccaed9 | 176 | BT_ASSERT(iterator); |
bd14d768 PP |
177 | |
178 | switch (iterator->state) { | |
d6e69534 | 179 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 | 180 | /* Skip user finalization if user initialization failed */ |
d6e69534 | 181 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
d94d92ac | 182 | "%!+i", iterator); |
d0fea130 | 183 | goto end; |
d6e69534 | 184 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
bd14d768 | 185 | /* Already finalized */ |
d6e69534 | 186 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
d94d92ac | 187 | "%!+i", iterator); |
d0fea130 PP |
188 | goto end; |
189 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: | |
190 | /* Already finalized */ | |
191 | BT_LIB_LOGF("Message iterator is already being finalized: " | |
192 | "%!+i", iterator); | |
193 | abort(); | |
bd14d768 PP |
194 | default: |
195 | break; | |
196 | } | |
197 | ||
d6e69534 | 198 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
d0fea130 PP |
199 | set_self_comp_port_input_msg_iterator_state(iterator, |
200 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); | |
f6ccaed9 | 201 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
202 | comp_class = iterator->upstream_component->class; |
203 | ||
204 | /* Call user-defined destroy method */ | |
205 | switch (comp_class->type) { | |
206 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
207 | { | |
d94d92ac PP |
208 | struct bt_component_class_source *src_comp_cls = |
209 | (void *) comp_class; | |
bd14d768 | 210 | |
d6e69534 | 211 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
212 | break; |
213 | } | |
214 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
215 | { | |
d94d92ac PP |
216 | struct bt_component_class_filter *flt_comp_cls = |
217 | (void *) comp_class; | |
bd14d768 | 218 | |
d6e69534 | 219 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
220 | break; |
221 | } | |
222 | default: | |
223 | /* Unreachable */ | |
0fbb9a9f | 224 | abort(); |
bd14d768 PP |
225 | } |
226 | ||
d94d92ac PP |
227 | if (method) { |
228 | BT_LIB_LOGD("Calling user's finalization method: %!+i", | |
5af447e5 | 229 | iterator); |
d94d92ac | 230 | method(iterator); |
bd14d768 PP |
231 | } |
232 | ||
bd14d768 PP |
233 | iterator->upstream_component = NULL; |
234 | iterator->upstream_port = NULL; | |
d0fea130 PP |
235 | set_self_comp_port_input_msg_iterator_state(iterator, |
236 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); | |
d6e69534 | 237 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
d0fea130 PP |
238 | |
239 | end: | |
240 | return; | |
bd14d768 PP |
241 | } |
242 | ||
243 | BT_HIDDEN | |
d6e69534 PP |
244 | void bt_self_component_port_input_message_iterator_set_connection( |
245 | struct bt_self_component_port_input_message_iterator *iterator, | |
bd14d768 PP |
246 | struct bt_connection *connection) |
247 | { | |
f6ccaed9 | 248 | BT_ASSERT(iterator); |
bd14d768 | 249 | iterator->connection = connection; |
3f7d4d90 | 250 | BT_LIB_LOGI("Set message iterator's connection: " |
d94d92ac | 251 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
bd14d768 PP |
252 | } |
253 | ||
90157d89 | 254 | static |
d6e69534 PP |
255 | int init_message_iterator(struct bt_message_iterator *iterator, |
256 | enum bt_message_iterator_type type, | |
90157d89 PP |
257 | bt_object_release_func destroy) |
258 | { | |
d4393e08 PP |
259 | int ret = 0; |
260 | ||
3fea54f6 | 261 | bt_object_init_shared(&iterator->base, destroy); |
90157d89 | 262 | iterator->type = type; |
d6e69534 PP |
263 | iterator->msgs = g_ptr_array_new(); |
264 | if (!iterator->msgs) { | |
d4393e08 PP |
265 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
266 | ret = -1; | |
267 | goto end; | |
268 | } | |
269 | ||
d6e69534 | 270 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
d4393e08 PP |
271 | |
272 | end: | |
273 | return ret; | |
90157d89 PP |
274 | } |
275 | ||
7474e7d3 PP |
276 | static |
277 | bt_bool can_seek_ns_from_origin_true( | |
278 | struct bt_self_component_port_input_message_iterator *iterator, | |
279 | int64_t ns_from_origin) | |
280 | { | |
281 | return BT_TRUE; | |
282 | } | |
283 | ||
284 | static | |
285 | bt_bool can_seek_beginning_true( | |
286 | struct bt_self_component_port_input_message_iterator *iterator) | |
287 | { | |
288 | return BT_TRUE; | |
289 | } | |
290 | ||
d94d92ac | 291 | static |
d6e69534 PP |
292 | struct bt_self_component_port_input_message_iterator * |
293 | bt_self_component_port_input_message_iterator_create_initial( | |
3230ee6b | 294 | struct bt_component *upstream_comp, |
d94d92ac | 295 | struct bt_port *upstream_port) |
47e5a032 | 296 | { |
d4393e08 | 297 | int ret; |
d6e69534 | 298 | struct bt_self_component_port_input_message_iterator *iterator = NULL; |
47e5a032 | 299 | |
f6ccaed9 PP |
300 | BT_ASSERT(upstream_comp); |
301 | BT_ASSERT(upstream_port); | |
f6ccaed9 | 302 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
3f7d4d90 | 303 | BT_LIB_LOGI("Creating initial message iterator on self component input port: " |
d94d92ac PP |
304 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); |
305 | BT_ASSERT(bt_component_get_class_type(upstream_comp) == | |
306 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
307 | bt_component_get_class_type(upstream_comp) == | |
308 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
309 | iterator = g_new0( | |
d6e69534 | 310 | struct bt_self_component_port_input_message_iterator, 1); |
47e5a032 | 311 | if (!iterator) { |
d94d92ac | 312 | BT_LOGE_STR("Failed to allocate one self component input port " |
d6e69534 | 313 | "message iterator."); |
73d5c1ad | 314 | goto end; |
47e5a032 JG |
315 | } |
316 | ||
d6e69534 PP |
317 | ret = init_message_iterator((void *) iterator, |
318 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT, | |
319 | bt_self_component_port_input_message_iterator_destroy); | |
d4393e08 | 320 | if (ret) { |
d6e69534 | 321 | /* init_message_iterator() logs errors */ |
d94d92ac | 322 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
323 | goto end; |
324 | } | |
3230ee6b | 325 | |
da9c4c52 SM |
326 | iterator->auto_seek.msgs = g_queue_new(); |
327 | if (!iterator->auto_seek.msgs) { | |
5b9e151d | 328 | BT_LOGE_STR("Failed to allocate a GQueue."); |
7474e7d3 | 329 | ret = -1; |
73d5c1ad | 330 | goto end; |
3230ee6b PP |
331 | } |
332 | ||
bd14d768 PP |
333 | iterator->upstream_component = upstream_comp; |
334 | iterator->upstream_port = upstream_port; | |
d94d92ac | 335 | iterator->connection = iterator->upstream_port->connection; |
5c563278 | 336 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
d0fea130 PP |
337 | set_self_comp_port_input_msg_iterator_state(iterator, |
338 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); | |
7474e7d3 PP |
339 | |
340 | switch (iterator->upstream_component->class->type) { | |
341 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
342 | { | |
343 | struct bt_component_class_source *src_comp_cls = | |
344 | (void *) iterator->upstream_component->class; | |
345 | ||
346 | iterator->methods.next = | |
347 | (bt_self_component_port_input_message_iterator_next_method) | |
348 | src_comp_cls->methods.msg_iter_next; | |
349 | iterator->methods.seek_ns_from_origin = | |
350 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
351 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
352 | iterator->methods.seek_beginning = | |
353 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
354 | src_comp_cls->methods.msg_iter_seek_beginning; | |
355 | iterator->methods.can_seek_ns_from_origin = | |
356 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
357 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
358 | iterator->methods.can_seek_beginning = | |
359 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
360 | src_comp_cls->methods.msg_iter_can_seek_beginning; | |
361 | break; | |
362 | } | |
363 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
364 | { | |
365 | struct bt_component_class_filter *flt_comp_cls = | |
366 | (void *) iterator->upstream_component->class; | |
367 | ||
368 | iterator->methods.next = | |
369 | (bt_self_component_port_input_message_iterator_next_method) | |
370 | flt_comp_cls->methods.msg_iter_next; | |
371 | iterator->methods.seek_ns_from_origin = | |
372 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
373 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
374 | iterator->methods.seek_beginning = | |
375 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
376 | flt_comp_cls->methods.msg_iter_seek_beginning; | |
377 | iterator->methods.can_seek_ns_from_origin = | |
378 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
379 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
380 | iterator->methods.can_seek_beginning = | |
381 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
382 | flt_comp_cls->methods.msg_iter_can_seek_beginning; | |
383 | break; | |
384 | } | |
385 | default: | |
386 | abort(); | |
387 | } | |
388 | ||
389 | if (iterator->methods.seek_ns_from_origin && | |
390 | !iterator->methods.can_seek_ns_from_origin) { | |
391 | iterator->methods.can_seek_ns_from_origin = | |
392 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
393 | can_seek_ns_from_origin_true; | |
394 | } | |
395 | ||
396 | if (iterator->methods.seek_beginning && | |
397 | !iterator->methods.can_seek_beginning) { | |
398 | iterator->methods.can_seek_beginning = | |
399 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
400 | can_seek_beginning_true; | |
401 | } | |
402 | ||
3f7d4d90 | 403 | BT_LIB_LOGI("Created initial message iterator on self component input port: " |
d94d92ac PP |
404 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
405 | upstream_port, upstream_comp, iterator); | |
3230ee6b | 406 | |
47e5a032 | 407 | end: |
d94d92ac | 408 | return iterator; |
47e5a032 JG |
409 | } |
410 | ||
d6e69534 PP |
411 | struct bt_self_component_port_input_message_iterator * |
412 | bt_self_component_port_input_message_iterator_create( | |
d94d92ac | 413 | struct bt_self_component_port_input *self_port) |
ea8d3e58 | 414 | { |
d6e69534 | 415 | typedef enum bt_self_message_iterator_status (*init_method_t)( |
d94d92ac PP |
416 | void *, void *, void *); |
417 | ||
418 | init_method_t init_method = NULL; | |
d6e69534 | 419 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac PP |
420 | NULL; |
421 | struct bt_port *port = (void *) self_port; | |
422 | struct bt_port *upstream_port; | |
423 | struct bt_component *comp; | |
424 | struct bt_component *upstream_comp; | |
425 | struct bt_component_class *upstream_comp_cls; | |
426 | ||
427 | BT_ASSERT_PRE_NON_NULL(port, "Port"); | |
0d72b8c3 | 428 | comp = bt_port_borrow_component_inline(port); |
d94d92ac PP |
429 | BT_ASSERT_PRE(bt_port_is_connected(port), |
430 | "Port is not connected: %![port-]+p", port); | |
431 | BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p", | |
432 | port); | |
433 | BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp), | |
434 | "Port's component's graph is canceled: " | |
435 | "%![port-]+p, %![comp-]+c", port, comp); | |
436 | BT_ASSERT(port->connection); | |
437 | upstream_port = port->connection->upstream_port; | |
438 | BT_ASSERT(upstream_port); | |
0d72b8c3 | 439 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
d94d92ac | 440 | BT_ASSERT(upstream_comp); |
5badd463 PP |
441 | BT_ASSERT_PRE( |
442 | bt_component_borrow_graph(upstream_comp)->config_state != | |
443 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
444 | "Graph is not configured: %!+g", | |
445 | bt_component_borrow_graph(upstream_comp)); | |
d94d92ac PP |
446 | upstream_comp_cls = upstream_comp->class; |
447 | BT_ASSERT(upstream_comp->class->type == | |
448 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
449 | upstream_comp->class->type == | |
450 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
d6e69534 | 451 | iterator = bt_self_component_port_input_message_iterator_create_initial( |
d94d92ac PP |
452 | upstream_comp, upstream_port); |
453 | if (!iterator) { | |
454 | BT_LOGW_STR("Cannot create self component input port " | |
d6e69534 | 455 | "message iterator."); |
d94d92ac PP |
456 | goto end; |
457 | } | |
890882ef | 458 | |
d94d92ac PP |
459 | switch (upstream_comp_cls->type) { |
460 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
461 | { | |
462 | struct bt_component_class_source *src_comp_cls = | |
463 | (void *) upstream_comp_cls; | |
464 | ||
465 | init_method = | |
d6e69534 | 466 | (init_method_t) src_comp_cls->methods.msg_iter_init; |
d94d92ac PP |
467 | break; |
468 | } | |
469 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
470 | { | |
471 | struct bt_component_class_filter *flt_comp_cls = | |
472 | (void *) upstream_comp_cls; | |
473 | ||
474 | init_method = | |
d6e69534 | 475 | (init_method_t) flt_comp_cls->methods.msg_iter_init; |
d94d92ac PP |
476 | break; |
477 | } | |
478 | default: | |
479 | /* Unreachable */ | |
480 | abort(); | |
481 | } | |
482 | ||
483 | if (init_method) { | |
484 | int iter_status; | |
485 | ||
486 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); | |
487 | iter_status = init_method(iterator, upstream_comp, | |
488 | upstream_port); | |
489 | BT_LOGD("User method returned: status=%s", | |
d6e69534 PP |
490 | bt_message_iterator_status_string(iter_status)); |
491 | if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
d94d92ac | 492 | BT_LOGW_STR("Initialization method failed."); |
d0fea130 | 493 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d94d92ac PP |
494 | goto end; |
495 | } | |
496 | } | |
497 | ||
d0fea130 PP |
498 | set_self_comp_port_input_msg_iterator_state(iterator, |
499 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
d94d92ac | 500 | g_ptr_array_add(port->connection->iterators, iterator); |
3f7d4d90 | 501 | BT_LIB_LOGI("Created message iterator on self component input port: " |
d94d92ac PP |
502 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
503 | upstream_port, upstream_comp, iterator); | |
504 | ||
505 | end: | |
506 | return iterator; | |
ea8d3e58 JG |
507 | } |
508 | ||
d6e69534 PP |
509 | void *bt_self_message_iterator_get_data( |
510 | const struct bt_self_message_iterator *self_iterator) | |
ea8d3e58 | 511 | { |
d6e69534 | 512 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 513 | (void *) self_iterator; |
ea8d3e58 | 514 | |
d6e69534 | 515 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 516 | return iterator->user_data; |
8738a040 | 517 | } |
413bc2c4 | 518 | |
d6e69534 PP |
519 | void bt_self_message_iterator_set_data( |
520 | struct bt_self_message_iterator *self_iterator, void *data) | |
5c563278 | 521 | { |
d6e69534 | 522 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 523 | (void *) self_iterator; |
5c563278 | 524 | |
d6e69534 | 525 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 526 | iterator->user_data = data; |
3f7d4d90 | 527 | BT_LIB_LOGD("Set message iterator's user data: " |
d94d92ac | 528 | "%!+i, user-data-addr=%p", iterator, data); |
5c563278 PP |
529 | } |
530 | ||
d6e69534 PP |
531 | enum bt_message_iterator_status |
532 | bt_self_component_port_input_message_iterator_next( | |
533 | struct bt_self_component_port_input_message_iterator *iterator, | |
534 | bt_message_array_const *msgs, uint64_t *user_count) | |
3230ee6b | 535 | { |
d6e69534 | 536 | int status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d94d92ac | 537 | |
d6e69534 PP |
538 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
539 | BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)"); | |
540 | BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)"); | |
f42867e2 | 541 | BT_ASSERT_PRE(iterator->state == |
d6e69534 PP |
542 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
543 | "Message iterator's \"next\" called, but " | |
7474e7d3 | 544 | "message iterator is in the wrong state: %!+i", iterator); |
f42867e2 PP |
545 | BT_ASSERT(iterator->upstream_component); |
546 | BT_ASSERT(iterator->upstream_component->class); | |
5badd463 PP |
547 | BT_ASSERT_PRE( |
548 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
549 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
550 | "Graph is not configured: %!+g", |
551 | bt_component_borrow_graph(iterator->upstream_component)); | |
d94d92ac | 552 | BT_LIB_LOGD("Getting next self component input port " |
3f7d4d90 PP |
553 | "message iterator's messages: %!+i, batch-size=%u", |
554 | iterator, MSG_BATCH_SIZE); | |
d3eb6e8f | 555 | |
3230ee6b | 556 | /* |
d6e69534 | 557 | * Call the user's "next" method to get the next messages |
fa054faf | 558 | * and status. |
3230ee6b | 559 | */ |
7474e7d3 | 560 | BT_ASSERT(iterator->methods.next); |
f42867e2 | 561 | BT_LOGD_STR("Calling user's \"next\" method."); |
3f7d4d90 | 562 | *user_count = 0; |
7474e7d3 PP |
563 | status = iterator->methods.next(iterator, |
564 | (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE, | |
565 | user_count); | |
3f7d4d90 PP |
566 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
567 | bt_message_iterator_status_string(status), *user_count); | |
d4393e08 | 568 | if (status < 0) { |
f42867e2 | 569 | BT_LOGW_STR("User method failed."); |
f42867e2 PP |
570 | goto end; |
571 | } | |
3230ee6b | 572 | |
d0fea130 PP |
573 | /* |
574 | * There is no way that this iterator could have been finalized | |
575 | * during its "next" method, as the only way to do this is to | |
576 | * put the last iterator's reference, and this can only be done | |
577 | * by its downstream owner. | |
7474e7d3 PP |
578 | * |
579 | * For the same reason, there is no way that this iterator could | |
580 | * have seeked (cannot seek a self message iterator). | |
d0fea130 PP |
581 | */ |
582 | BT_ASSERT(iterator->state == | |
583 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
8cf27cc5 | 584 | |
d4393e08 | 585 | switch (status) { |
d6e69534 | 586 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
7474e7d3 PP |
587 | BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE, |
588 | "Invalid returned message count: greater than " | |
589 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
590 | *user_count, MSG_BATCH_SIZE); | |
d6e69534 | 591 | *msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 592 | break; |
d6e69534 | 593 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
d4393e08 | 594 | goto end; |
d6e69534 | 595 | case BT_MESSAGE_ITERATOR_STATUS_END: |
d0fea130 PP |
596 | set_self_comp_port_input_msg_iterator_state(iterator, |
597 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); | |
f42867e2 | 598 | goto end; |
f42867e2 PP |
599 | default: |
600 | /* Unknown non-error status */ | |
601 | abort(); | |
41a2b7ae PP |
602 | } |
603 | ||
604 | end: | |
3230ee6b PP |
605 | return status; |
606 | } | |
607 | ||
d0fea130 | 608 | enum bt_message_iterator_status bt_port_output_message_iterator_next( |
d6e69534 PP |
609 | struct bt_port_output_message_iterator *iterator, |
610 | bt_message_array_const *msgs_to_user, | |
d4393e08 | 611 | uint64_t *count_to_user) |
3230ee6b | 612 | { |
d6e69534 | 613 | enum bt_message_iterator_status status; |
07245ac2 | 614 | enum bt_graph_status graph_status; |
3230ee6b | 615 | |
d6e69534 PP |
616 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
617 | BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)"); | |
618 | BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)"); | |
619 | BT_LIB_LOGD("Getting next output port message iterator's messages: " | |
07245ac2 | 620 | "%!+i", iterator); |
d94d92ac PP |
621 | graph_status = bt_graph_consume_sink_no_check(iterator->graph, |
622 | iterator->colander); | |
07245ac2 PP |
623 | switch (graph_status) { |
624 | case BT_GRAPH_STATUS_CANCELED: | |
07245ac2 | 625 | case BT_GRAPH_STATUS_AGAIN: |
07245ac2 | 626 | case BT_GRAPH_STATUS_END: |
07245ac2 | 627 | case BT_GRAPH_STATUS_NOMEM: |
d94d92ac | 628 | status = (int) graph_status; |
07245ac2 PP |
629 | break; |
630 | case BT_GRAPH_STATUS_OK: | |
d6e69534 | 631 | status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
632 | |
633 | /* | |
d6e69534 | 634 | * On success, the colander sink moves the messages |
d4393e08 | 635 | * to this iterator's array and sets this iterator's |
d6e69534 | 636 | * message count: move them to the user. |
d4393e08 | 637 | */ |
d6e69534 | 638 | *msgs_to_user = (void *) iterator->base.msgs->pdata; |
d94d92ac | 639 | *count_to_user = iterator->count; |
90157d89 | 640 | break; |
90157d89 | 641 | default: |
07245ac2 | 642 | /* Other errors */ |
d6e69534 | 643 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
90157d89 | 644 | } |
3230ee6b | 645 | |
3230ee6b | 646 | return status; |
53d45b87 JG |
647 | } |
648 | ||
7474e7d3 PP |
649 | struct bt_component * |
650 | bt_self_component_port_input_message_iterator_borrow_component( | |
d6e69534 | 651 | struct bt_self_component_port_input_message_iterator *iterator) |
d94d92ac | 652 | { |
d6e69534 | 653 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac PP |
654 | return iterator->upstream_component; |
655 | } | |
656 | ||
7474e7d3 PP |
657 | const struct bt_component * |
658 | bt_self_component_port_input_message_iterator_borrow_component_const( | |
659 | const struct bt_self_component_port_input_message_iterator *iterator) | |
660 | { | |
661 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
662 | return iterator->upstream_component; | |
663 | } | |
664 | ||
d6e69534 PP |
665 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
666 | struct bt_self_message_iterator *self_iterator) | |
413bc2c4 | 667 | { |
d6e69534 | 668 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 669 | (void *) self_iterator; |
90157d89 | 670 | |
d6e69534 | 671 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 672 | return (void *) iterator->upstream_component; |
413bc2c4 JG |
673 | } |
674 | ||
d6e69534 PP |
675 | struct bt_self_port_output *bt_self_message_iterator_borrow_port( |
676 | struct bt_self_message_iterator *self_iterator) | |
91457551 | 677 | { |
d6e69534 | 678 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac PP |
679 | (void *) self_iterator; |
680 | ||
d6e69534 | 681 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 682 | return (void *) iterator->upstream_port; |
91457551 | 683 | } |
8ed535b5 PP |
684 | |
685 | static | |
d6e69534 | 686 | void bt_port_output_message_iterator_destroy(struct bt_object *obj) |
8ed535b5 | 687 | { |
d6e69534 | 688 | struct bt_port_output_message_iterator *iterator = (void *) obj; |
8ed535b5 | 689 | |
3f7d4d90 | 690 | BT_LIB_LOGI("Destroying output port message iterator object: %!+i", |
8ed535b5 PP |
691 | iterator); |
692 | BT_LOGD_STR("Putting graph."); | |
d94d92ac | 693 | BT_OBJECT_PUT_REF_AND_RESET(iterator->graph); |
8ed535b5 | 694 | BT_LOGD_STR("Putting colander sink component."); |
d94d92ac | 695 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
d6e69534 | 696 | destroy_base_message_iterator(obj); |
8ed535b5 PP |
697 | } |
698 | ||
d6e69534 | 699 | struct bt_port_output_message_iterator * |
7474e7d3 | 700 | bt_port_output_message_iterator_create(struct bt_graph *graph, |
0d72b8c3 | 701 | const struct bt_port_output *output_port) |
8ed535b5 | 702 | { |
d6e69534 | 703 | struct bt_port_output_message_iterator *iterator = NULL; |
d94d92ac | 704 | struct bt_component_class_sink *colander_comp_cls = NULL; |
8ed535b5 | 705 | struct bt_component *output_port_comp = NULL; |
d94d92ac | 706 | struct bt_component_sink *colander_comp; |
8ed535b5 | 707 | enum bt_graph_status graph_status; |
d94d92ac | 708 | struct bt_port_input *colander_in_port = NULL; |
8ed535b5 | 709 | struct bt_component_class_sink_colander_data colander_data; |
d4393e08 | 710 | int ret; |
8ed535b5 | 711 | |
d94d92ac | 712 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
f42867e2 | 713 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
0d72b8c3 PP |
714 | output_port_comp = bt_port_borrow_component_inline( |
715 | (const void *) output_port); | |
f42867e2 PP |
716 | BT_ASSERT_PRE(output_port_comp, |
717 | "Output port has no component: %!+p", output_port); | |
d94d92ac PP |
718 | BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) == |
719 | (void *) graph, | |
720 | "Output port is not part of graph: %![graph-]+g, %![port-]+p", | |
721 | graph, output_port); | |
2bcba397 PP |
722 | BT_ASSERT_PRE(!graph->has_sink, |
723 | "Graph already has a sink component: %![graph-]+g"); | |
8ed535b5 | 724 | |
d6e69534 | 725 | /* Create message iterator */ |
3f7d4d90 | 726 | BT_LIB_LOGI("Creating message iterator on output port: " |
d94d92ac | 727 | "%![port-]+p, %![comp-]+c", output_port, output_port_comp); |
d6e69534 | 728 | iterator = g_new0(struct bt_port_output_message_iterator, 1); |
8ed535b5 | 729 | if (!iterator) { |
d6e69534 | 730 | BT_LOGE_STR("Failed to allocate one output port message iterator."); |
8ed535b5 PP |
731 | goto error; |
732 | } | |
733 | ||
d6e69534 PP |
734 | ret = init_message_iterator((void *) iterator, |
735 | BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT, | |
736 | bt_port_output_message_iterator_destroy); | |
d4393e08 | 737 | if (ret) { |
d6e69534 | 738 | /* init_message_iterator() logs errors */ |
65300d60 | 739 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
740 | goto end; |
741 | } | |
8ed535b5 PP |
742 | |
743 | /* Create colander component */ | |
744 | colander_comp_cls = bt_component_class_sink_colander_get(); | |
745 | if (!colander_comp_cls) { | |
746 | BT_LOGW("Cannot get colander sink component class."); | |
747 | goto error; | |
748 | } | |
749 | ||
398454ed PP |
750 | iterator->graph = graph; |
751 | bt_object_get_no_null_check(iterator->graph); | |
d6e69534 | 752 | colander_data.msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 753 | colander_data.count_addr = &iterator->count; |
5fd91d88 | 754 | |
e874da19 PP |
755 | /* |
756 | * Hope that nobody uses this very unique name. | |
757 | * | |
758 | * We pass `BT_LOGGING_LEVEL_NONE` but the colander component | |
759 | * class module does not use this level anyway since it belongs | |
760 | * to the library. | |
761 | */ | |
d94d92ac | 762 | graph_status = |
0d72b8c3 | 763 | bt_graph_add_sink_component_with_init_method_data( |
5fd91d88 PP |
764 | (void *) graph, colander_comp_cls, |
765 | "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1", | |
e874da19 PP |
766 | NULL, &colander_data, BT_LOGGING_LEVEL_NONE, |
767 | (void *) &iterator->colander); | |
8ed535b5 | 768 | if (graph_status != BT_GRAPH_STATUS_OK) { |
d94d92ac PP |
769 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
770 | "%1[graph-]+g, status=%s", graph, | |
8ed535b5 PP |
771 | bt_graph_status_string(graph_status)); |
772 | goto error; | |
773 | } | |
774 | ||
775 | /* | |
776 | * Connect provided output port to the colander component's | |
777 | * input port. | |
778 | */ | |
0d72b8c3 PP |
779 | colander_in_port = |
780 | (void *) bt_component_sink_borrow_input_port_by_index_const( | |
781 | (void *) iterator->colander, 0); | |
f6ccaed9 | 782 | BT_ASSERT(colander_in_port); |
0d72b8c3 | 783 | graph_status = bt_graph_connect_ports(graph, |
8ed535b5 PP |
784 | output_port, colander_in_port, NULL); |
785 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
d94d92ac PP |
786 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
787 | "%![graph-]+g, %![comp-]+c, status=%s", graph, | |
788 | iterator->colander, | |
8ed535b5 PP |
789 | bt_graph_status_string(graph_status)); |
790 | goto error; | |
791 | } | |
792 | ||
793 | /* | |
794 | * At this point everything went fine. Make the graph | |
d6e69534 | 795 | * nonconsumable forever so that only this message iterator |
8ed535b5 | 796 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
d6e69534 PP |
797 | * This avoids leaking the message created by the colander |
798 | * sink and moved to the message iterator's message | |
07245ac2 | 799 | * member. |
8ed535b5 | 800 | */ |
d94d92ac | 801 | bt_graph_set_can_consume(iterator->graph, false); |
5badd463 | 802 | |
36d1acad SM |
803 | /* Also set the graph as being configured. */ |
804 | graph_status = bt_graph_configure(graph); | |
805 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
806 | BT_LIB_LOGW("Cannot configure graph after having added colander: " | |
807 | "%![graph-]+g, status=%s", graph, | |
808 | bt_graph_status_string(graph_status)); | |
809 | goto error; | |
810 | } | |
8ed535b5 PP |
811 | goto end; |
812 | ||
813 | error: | |
814 | if (iterator && iterator->graph && iterator->colander) { | |
815 | int ret; | |
816 | ||
817 | /* Remove created colander component from graph if any */ | |
818 | colander_comp = iterator->colander; | |
65300d60 | 819 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
8ed535b5 PP |
820 | |
821 | /* | |
822 | * At this point the colander component's reference | |
823 | * count is 0 because iterator->colander was the only | |
824 | * owner. We also know that it is not connected because | |
825 | * this is the last operation before this function | |
826 | * succeeds. | |
827 | * | |
828 | * Since we honor the preconditions here, | |
829 | * bt_graph_remove_unconnected_component() always | |
830 | * succeeds. | |
831 | */ | |
832 | ret = bt_graph_remove_unconnected_component(iterator->graph, | |
d94d92ac | 833 | (void *) colander_comp); |
f6ccaed9 | 834 | BT_ASSERT(ret == 0); |
8ed535b5 PP |
835 | } |
836 | ||
65300d60 | 837 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
8ed535b5 PP |
838 | |
839 | end: | |
65300d60 | 840 | bt_object_put_ref(colander_comp_cls); |
8ed535b5 PP |
841 | return (void *) iterator; |
842 | } | |
c5b9b441 | 843 | |
7474e7d3 PP |
844 | bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
845 | struct bt_self_component_port_input_message_iterator *iterator, | |
846 | int64_t ns_from_origin) | |
847 | { | |
848 | bt_bool can = BT_FALSE; | |
849 | ||
850 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
851 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
852 | BT_ASSERT_PRE( |
853 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
854 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
855 | "Graph is not configured: %!+g", |
856 | bt_component_borrow_graph(iterator->upstream_component)); | |
857 | ||
858 | if (iterator->methods.can_seek_ns_from_origin) { | |
859 | can = iterator->methods.can_seek_ns_from_origin(iterator, | |
860 | ns_from_origin); | |
861 | goto end; | |
862 | } | |
863 | ||
864 | /* | |
865 | * Automatic seeking fall back: if we can seek to the beginning, | |
866 | * then we can automatically seek to any message. | |
867 | */ | |
868 | if (iterator->methods.can_seek_beginning) { | |
869 | can = iterator->methods.can_seek_beginning(iterator); | |
870 | } | |
871 | ||
872 | end: | |
873 | return can; | |
874 | } | |
875 | ||
876 | bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( | |
877 | struct bt_self_component_port_input_message_iterator *iterator) | |
878 | { | |
879 | bt_bool can = BT_FALSE; | |
880 | ||
881 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
882 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
883 | BT_ASSERT_PRE( |
884 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
885 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
886 | "Graph is not configured: %!+g", |
887 | bt_component_borrow_graph(iterator->upstream_component)); | |
888 | ||
889 | if (iterator->methods.can_seek_beginning) { | |
890 | can = iterator->methods.can_seek_beginning(iterator); | |
891 | } | |
892 | ||
893 | return can; | |
894 | } | |
895 | ||
896 | static inline | |
003e713f | 897 | void set_iterator_state_after_seeking( |
7474e7d3 PP |
898 | struct bt_self_component_port_input_message_iterator *iterator, |
899 | enum bt_message_iterator_status status) | |
900 | { | |
901 | enum bt_self_component_port_input_message_iterator_state new_state = 0; | |
902 | ||
903 | /* Set iterator's state depending on seeking status */ | |
904 | switch (status) { | |
905 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
906 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; | |
907 | break; | |
908 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
909 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; | |
910 | break; | |
911 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
912 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
913 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; | |
914 | break; | |
915 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
916 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; | |
917 | break; | |
918 | default: | |
919 | abort(); | |
920 | } | |
921 | ||
922 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); | |
923 | } | |
924 | ||
7474e7d3 PP |
925 | enum bt_message_iterator_status |
926 | bt_self_component_port_input_message_iterator_seek_beginning( | |
927 | struct bt_self_component_port_input_message_iterator *iterator) | |
928 | { | |
929 | int status; | |
930 | ||
931 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
932 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
933 | BT_ASSERT_PRE( |
934 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
935 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
936 | "Graph is not configured: %!+g", |
937 | bt_component_borrow_graph(iterator->upstream_component)); | |
938 | BT_ASSERT_PRE( | |
939 | bt_self_component_port_input_message_iterator_can_seek_beginning( | |
940 | iterator), | |
941 | "Message iterator cannot seek beginning: %!+i", iterator); | |
942 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); | |
943 | set_self_comp_port_input_msg_iterator_state(iterator, | |
944 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
945 | status = iterator->methods.seek_beginning(iterator); | |
946 | BT_LOGD("User method returned: status=%s", | |
947 | bt_message_iterator_status_string(status)); | |
948 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
949 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
950 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
951 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
952 | "Unexpected status: %![iter-]+i, status=%s", | |
bd1a54fe | 953 | iterator, bt_common_self_message_iterator_status_string(status)); |
7474e7d3 PP |
954 | set_iterator_state_after_seeking(iterator, status); |
955 | return status; | |
956 | } | |
957 | ||
5b7b55be SM |
958 | |
959 | /* | |
960 | * Structure used to record the state of a given stream during the fast-forward | |
961 | * phase of an auto-seek. | |
962 | */ | |
963 | struct auto_seek_stream_state { | |
964 | /* | |
965 | * Value representing which step of this timeline we are at. | |
966 | * | |
967 | * time ---> | |
968 | * [SB] 1 [SAB] 2 [PB] 3 [PE] 2 [SAE] 1 [SE] | |
969 | * | |
970 | * At each point in the timeline, the messages we need to replicate are: | |
971 | * | |
972 | * 1: Stream beginning | |
973 | * 2: Stream beginning, stream activity beginning | |
974 | * 3: Stream beginning, stream activity beginning, packet beginning | |
975 | * | |
976 | * Before "Stream beginning" and after "Stream end", we don't need to | |
977 | * replicate anything as the stream doesn't exist. | |
978 | */ | |
979 | enum { | |
980 | AUTO_SEEK_STREAM_STATE_STREAM_BEGAN, | |
981 | AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN, | |
982 | AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, | |
983 | } state; | |
984 | ||
985 | /* | |
986 | * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are | |
987 | * in. This is a weak reference, since the packet will always be | |
988 | * alive by the time we use it. | |
989 | */ | |
990 | struct bt_packet *packet; | |
991 | }; | |
992 | ||
993 | static | |
994 | struct auto_seek_stream_state *create_auto_seek_stream_state(void) | |
995 | { | |
996 | return g_new0(struct auto_seek_stream_state, 1); | |
997 | } | |
998 | ||
999 | static | |
1000 | void destroy_auto_seek_stream_state(void *ptr) | |
1001 | { | |
1002 | g_free(ptr); | |
1003 | } | |
1004 | ||
1005 | static | |
1006 | GHashTable *create_auto_seek_stream_states(void) | |
1007 | { | |
1008 | return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, | |
1009 | destroy_auto_seek_stream_state); | |
1010 | } | |
1011 | ||
1012 | static | |
1013 | void destroy_auto_seek_stream_states(GHashTable *stream_states) | |
1014 | { | |
1015 | g_hash_table_destroy(stream_states); | |
1016 | } | |
1017 | ||
1018 | /* | |
1019 | * Handle one message while we are in the fast-forward phase of an auto-seek. | |
1020 | * | |
1021 | * Sets `*got_first` to true if the message's timestamp is greater or equal to | |
1022 | * `ns_from_origin`. In other words, if this is the first message after our | |
1023 | * seek point. | |
1024 | * | |
1025 | * `stream_states` is an hash table of `bt_stream *` (weak reference) to | |
1026 | * `struct auto_seek_stream_state` used to keep the state of each stream | |
1027 | * during the fast-forward. | |
1028 | */ | |
1029 | ||
7474e7d3 | 1030 | static inline |
5b9e151d PP |
1031 | enum bt_message_iterator_status auto_seek_handle_message( |
1032 | struct bt_self_component_port_input_message_iterator *iterator, | |
1033 | int64_t ns_from_origin, const struct bt_message *msg, | |
5b7b55be | 1034 | bool *got_first, GHashTable *stream_states) |
7474e7d3 | 1035 | { |
5b9e151d PP |
1036 | enum bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; |
1037 | int64_t msg_ns_from_origin; | |
7474e7d3 | 1038 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
5b9e151d PP |
1039 | int ret; |
1040 | ||
1041 | BT_ASSERT(msg); | |
1042 | BT_ASSERT(got_first); | |
7474e7d3 PP |
1043 | |
1044 | switch (msg->type) { | |
1045 | case BT_MESSAGE_TYPE_EVENT: | |
1046 | { | |
1047 | const struct bt_message_event *event_msg = | |
1048 | (const void *) msg; | |
1049 | ||
2c091c04 | 1050 | clk_snapshot = event_msg->default_cs; |
7474e7d3 | 1051 | BT_ASSERT_PRE(clk_snapshot, |
c7072d5a PP |
1052 | "Event message has no default clock snapshot: %!+n", |
1053 | event_msg); | |
7474e7d3 PP |
1054 | break; |
1055 | } | |
b9fd9cbb | 1056 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
7474e7d3 | 1057 | { |
b9fd9cbb | 1058 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
7474e7d3 PP |
1059 | (const void *) msg; |
1060 | ||
7474e7d3 | 1061 | clk_snapshot = inactivity_msg->default_cs; |
16663a5e | 1062 | BT_ASSERT(clk_snapshot); |
7474e7d3 PP |
1063 | break; |
1064 | } | |
16663a5e PP |
1065 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
1066 | case BT_MESSAGE_TYPE_PACKET_END: | |
c7072d5a PP |
1067 | { |
1068 | const struct bt_message_packet *packet_msg = | |
1069 | (const void *) msg; | |
1070 | ||
1071 | clk_snapshot = packet_msg->default_cs; | |
1072 | BT_ASSERT_PRE(clk_snapshot, | |
1073 | "Packet message has no default clock snapshot: %!+n", | |
1074 | packet_msg); | |
1075 | break; | |
1076 | } | |
16663a5e PP |
1077 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1078 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
7474e7d3 | 1079 | { |
5b9e151d PP |
1080 | struct bt_message_discarded_items *msg_disc_items = |
1081 | (void *) msg; | |
1082 | ||
1083 | BT_ASSERT_PRE(msg_disc_items->default_begin_cs && | |
1084 | msg_disc_items->default_end_cs, | |
1085 | "Discarded events/packets message has no default clock snapshots: %!+n", | |
1086 | msg_disc_items); | |
1087 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1088 | msg_disc_items->default_begin_cs, | |
1089 | &msg_ns_from_origin); | |
1090 | if (ret) { | |
1091 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1092 | goto end; | |
1093 | } | |
7474e7d3 | 1094 | |
5b9e151d PP |
1095 | if (msg_ns_from_origin >= ns_from_origin) { |
1096 | *got_first = true; | |
1097 | goto push_msg; | |
1098 | } | |
1099 | ||
1100 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1101 | msg_disc_items->default_end_cs, | |
1102 | &msg_ns_from_origin); | |
1103 | if (ret) { | |
1104 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1105 | goto end; | |
1106 | } | |
1107 | ||
1108 | if (msg_ns_from_origin >= ns_from_origin) { | |
1109 | /* | |
1110 | * The discarded items message's beginning time | |
1111 | * is before the requested seeking time, but its | |
1112 | * end time is after. Modify the message so as | |
1113 | * to set its beginning time to the requested | |
1114 | * seeking time, and make its item count unknown | |
1115 | * as we don't know if items were really | |
1116 | * discarded within the new time range. | |
1117 | */ | |
1118 | uint64_t new_begin_raw_value; | |
1119 | ||
1120 | ret = bt_clock_class_clock_value_from_ns_from_origin( | |
1121 | msg_disc_items->default_end_cs->clock_class, | |
1122 | ns_from_origin, &new_begin_raw_value); | |
1123 | if (ret) { | |
1124 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1125 | goto end; | |
1126 | } | |
1127 | ||
1128 | bt_clock_snapshot_set_raw_value( | |
1129 | msg_disc_items->default_begin_cs, | |
1130 | new_begin_raw_value); | |
1131 | msg_disc_items->count.base.avail = | |
1132 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; | |
1133 | ||
1134 | /* | |
1135 | * It is safe to push it because its beginning | |
1136 | * time is exactly the requested seeking time. | |
1137 | */ | |
1138 | goto push_msg; | |
1139 | } else { | |
1140 | goto skip_msg; | |
1141 | } | |
7474e7d3 | 1142 | } |
16663a5e | 1143 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
7474e7d3 | 1144 | { |
16663a5e | 1145 | const struct bt_message_stream_activity *stream_act_msg = |
7474e7d3 PP |
1146 | (const void *) msg; |
1147 | ||
16663a5e PP |
1148 | switch (stream_act_msg->default_cs_state) { |
1149 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1150 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: | |
1151 | /* | |
1152 | * -inf is always less than any requested time, | |
1153 | * and we can't assume any specific time for an | |
1154 | * unknown clock snapshot, so skip this. | |
1155 | */ | |
5b9e151d | 1156 | goto skip_msg; |
16663a5e PP |
1157 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1158 | clk_snapshot = stream_act_msg->default_cs; | |
1159 | BT_ASSERT(clk_snapshot); | |
1160 | break; | |
1161 | default: | |
1162 | abort(); | |
1163 | } | |
1164 | ||
7474e7d3 PP |
1165 | break; |
1166 | } | |
16663a5e PP |
1167 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
1168 | { | |
1169 | const struct bt_message_stream_activity *stream_act_msg = | |
1170 | (const void *) msg; | |
1171 | ||
1172 | switch (stream_act_msg->default_cs_state) { | |
1173 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1174 | /* | |
1175 | * We can't assume any specific time for an | |
1176 | * unknown clock snapshot, so skip this. | |
1177 | */ | |
5b9e151d | 1178 | goto skip_msg; |
16663a5e PP |
1179 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: |
1180 | /* | |
1181 | * +inf is always greater than any requested | |
1182 | * time. | |
1183 | */ | |
5b9e151d PP |
1184 | *got_first = true; |
1185 | goto push_msg; | |
16663a5e PP |
1186 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1187 | clk_snapshot = stream_act_msg->default_cs; | |
1188 | BT_ASSERT(clk_snapshot); | |
1189 | break; | |
1190 | default: | |
1191 | abort(); | |
1192 | } | |
1193 | ||
1194 | break; | |
1195 | } | |
1196 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1197 | case BT_MESSAGE_TYPE_STREAM_END: | |
1198 | /* Ignore */ | |
5b9e151d | 1199 | goto skip_msg; |
7474e7d3 PP |
1200 | default: |
1201 | abort(); | |
1202 | } | |
1203 | ||
5b9e151d PP |
1204 | BT_ASSERT(clk_snapshot); |
1205 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, | |
1206 | &msg_ns_from_origin); | |
1207 | if (ret) { | |
1208 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
7474e7d3 PP |
1209 | goto end; |
1210 | } | |
1211 | ||
5b9e151d PP |
1212 | if (msg_ns_from_origin >= ns_from_origin) { |
1213 | *got_first = true; | |
1214 | goto push_msg; | |
1215 | } | |
1216 | ||
1217 | skip_msg: | |
5b7b55be SM |
1218 | /* This message won't be sent downstream. */ |
1219 | switch (msg->type) { | |
1220 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1221 | { | |
1222 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1223 | struct auto_seek_stream_state *stream_state; | |
5b7b55be SM |
1224 | |
1225 | /* Update stream's state: stream began. */ | |
1226 | stream_state = create_auto_seek_stream_state(); | |
1227 | if (!stream_state) { | |
1228 | status = BT_MESSAGE_ITERATOR_STATUS_NOMEM; | |
1229 | goto end; | |
1230 | } | |
1231 | ||
1232 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; | |
e74dbb33 SM |
1233 | |
1234 | BT_ASSERT(!bt_g_hash_table_contains(stream_states, stream_msg->stream)); | |
1235 | g_hash_table_insert(stream_states, stream_msg->stream, stream_state); | |
5b7b55be SM |
1236 | break; |
1237 | } | |
1238 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: | |
1239 | { | |
1240 | const struct bt_message_stream_activity *stream_act_msg = | |
1241 | (const void *) msg; | |
1242 | struct auto_seek_stream_state *stream_state; | |
1243 | ||
1244 | /* Update stream's state: stream activity began. */ | |
1245 | stream_state = g_hash_table_lookup(stream_states, stream_act_msg->stream); | |
1246 | BT_ASSERT(stream_state); | |
1247 | ||
1248 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
1249 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN; | |
1250 | BT_ASSERT(!stream_state->packet); | |
1251 | break; | |
1252 | } | |
1253 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
1254 | { | |
1255 | const struct bt_message_packet *packet_msg = | |
1256 | (const void *) msg; | |
1257 | struct auto_seek_stream_state *stream_state; | |
1258 | ||
1259 | /* Update stream's state: packet began. */ | |
1260 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
1261 | BT_ASSERT(stream_state); | |
1262 | ||
1263 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN); | |
1264 | stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN; | |
1265 | BT_ASSERT(!stream_state->packet); | |
1266 | stream_state->packet = packet_msg->packet; | |
1267 | break; | |
1268 | } | |
1269 | case BT_MESSAGE_TYPE_PACKET_END: | |
1270 | { | |
1271 | const struct bt_message_packet *packet_msg = | |
1272 | (const void *) msg; | |
1273 | struct auto_seek_stream_state *stream_state; | |
1274 | ||
1275 | /* Update stream's state: packet ended. */ | |
1276 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
1277 | BT_ASSERT(stream_state); | |
1278 | ||
1279 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN); | |
1280 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN; | |
1281 | BT_ASSERT(stream_state->packet); | |
1282 | stream_state->packet = NULL; | |
1283 | break; | |
1284 | } | |
1285 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: | |
1286 | { | |
1287 | const struct bt_message_stream_activity *stream_act_msg = | |
1288 | (const void *) msg; | |
1289 | struct auto_seek_stream_state *stream_state; | |
1290 | ||
1291 | /* Update stream's state: stream activity ended. */ | |
1292 | stream_state = g_hash_table_lookup(stream_states, stream_act_msg->stream); | |
1293 | BT_ASSERT(stream_state); | |
1294 | ||
1295 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN); | |
1296 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; | |
1297 | BT_ASSERT(!stream_state->packet); | |
1298 | break; | |
1299 | } | |
1300 | case BT_MESSAGE_TYPE_STREAM_END: | |
1301 | { | |
1302 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1303 | struct auto_seek_stream_state *stream_state; | |
1304 | ||
1305 | stream_state = g_hash_table_lookup(stream_states, stream_msg->stream); | |
1306 | BT_ASSERT(stream_state); | |
1307 | BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
1308 | BT_ASSERT(!stream_state->packet); | |
1309 | ||
1310 | /* Update stream's state: this stream doesn't exist anymore. */ | |
1311 | g_hash_table_remove(stream_states, stream_msg->stream); | |
1312 | break; | |
1313 | } | |
1314 | default: | |
1315 | break; | |
1316 | } | |
1317 | ||
5b9e151d | 1318 | bt_object_put_no_null_check(msg); |
04c0cec6 | 1319 | msg = NULL; |
5b9e151d PP |
1320 | goto end; |
1321 | ||
1322 | push_msg: | |
da9c4c52 | 1323 | g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg); |
5b9e151d | 1324 | msg = NULL; |
7474e7d3 PP |
1325 | |
1326 | end: | |
5b9e151d PP |
1327 | BT_ASSERT(!msg || status != BT_MESSAGE_ITERATOR_STATUS_OK); |
1328 | return status; | |
7474e7d3 PP |
1329 | } |
1330 | ||
1331 | static | |
1332 | enum bt_message_iterator_status find_message_ge_ns_from_origin( | |
1333 | struct bt_self_component_port_input_message_iterator *iterator, | |
5b7b55be | 1334 | int64_t ns_from_origin, GHashTable *stream_states) |
7474e7d3 PP |
1335 | { |
1336 | int status; | |
1337 | enum bt_self_component_port_input_message_iterator_state init_state = | |
1338 | iterator->state; | |
1339 | const struct bt_message *messages[MSG_BATCH_SIZE]; | |
1340 | uint64_t user_count = 0; | |
1341 | uint64_t i; | |
5b9e151d | 1342 | bool got_first = false; |
7474e7d3 PP |
1343 | |
1344 | BT_ASSERT(iterator); | |
1345 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); | |
1346 | ||
1347 | /* | |
1348 | * Make this iterator temporarily active (not seeking) to call | |
1349 | * the "next" method. | |
1350 | */ | |
1351 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1352 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1353 | ||
1354 | BT_ASSERT(iterator->methods.next); | |
1355 | ||
e0dade92 | 1356 | while (!got_first) { |
7474e7d3 PP |
1357 | /* |
1358 | * Call the user's "next" method to get the next | |
1359 | * messages and status. | |
1360 | */ | |
1361 | BT_LOGD_STR("Calling user's \"next\" method."); | |
1362 | status = iterator->methods.next(iterator, | |
1363 | &messages[0], MSG_BATCH_SIZE, &user_count); | |
1364 | BT_LOGD("User method returned: status=%s", | |
1365 | bt_message_iterator_status_string(status)); | |
1366 | ||
7474e7d3 PP |
1367 | /* |
1368 | * The user's "next" method must not do any action which | |
1369 | * would change the iterator's state. | |
1370 | */ | |
1371 | BT_ASSERT(iterator->state == | |
1372 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
7474e7d3 PP |
1373 | |
1374 | switch (status) { | |
1375 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1376 | BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE, | |
1377 | "Invalid returned message count: greater than " | |
1378 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
1379 | user_count, MSG_BATCH_SIZE); | |
1380 | break; | |
1381 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1382 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1383 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1384 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
1385 | goto end; | |
1386 | default: | |
1387 | abort(); | |
1388 | } | |
1389 | ||
7474e7d3 | 1390 | for (i = 0; i < user_count; i++) { |
5b9e151d | 1391 | if (got_first) { |
da9c4c52 | 1392 | g_queue_push_tail(iterator->auto_seek.msgs, |
5b9e151d PP |
1393 | (void *) messages[i]); |
1394 | messages[i] = NULL; | |
7474e7d3 PP |
1395 | continue; |
1396 | } | |
1397 | ||
5b9e151d | 1398 | status = auto_seek_handle_message(iterator, |
5b7b55be SM |
1399 | ns_from_origin, messages[i], &got_first, |
1400 | stream_states); | |
5b9e151d | 1401 | if (status == BT_MESSAGE_ITERATOR_STATUS_OK) { |
e0dade92 | 1402 | /* Message was either pushed or moved */ |
5b9e151d PP |
1403 | messages[i] = NULL; |
1404 | } else { | |
7474e7d3 PP |
1405 | goto end; |
1406 | } | |
7474e7d3 PP |
1407 | } |
1408 | } | |
1409 | ||
1410 | end: | |
1411 | for (i = 0; i < user_count; i++) { | |
1412 | if (messages[i]) { | |
1413 | bt_object_put_no_null_check(messages[i]); | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); | |
1418 | return status; | |
1419 | } | |
1420 | ||
5b7b55be SM |
1421 | /* |
1422 | * This function is installed as the iterator's next callback after we have | |
1423 | * auto-seeked (seeked to the beginning and fast-forwarded) to send the | |
1424 | * messages saved in iterator->auto_seek.msgs. Once this is done, the original | |
1425 | * next callback is put back. | |
1426 | */ | |
1427 | ||
7474e7d3 PP |
1428 | static |
1429 | enum bt_self_message_iterator_status post_auto_seek_next( | |
1430 | struct bt_self_component_port_input_message_iterator *iterator, | |
1431 | bt_message_array_const msgs, uint64_t capacity, | |
1432 | uint64_t *count) | |
1433 | { | |
da9c4c52 | 1434 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs)); |
5b9e151d | 1435 | *count = 0; |
7474e7d3 PP |
1436 | |
1437 | /* | |
1438 | * Move auto-seek messages to the output array (which is this | |
5b9e151d | 1439 | * iterator's base message array). |
7474e7d3 | 1440 | */ |
da9c4c52 SM |
1441 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) { |
1442 | msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs); | |
5b9e151d PP |
1443 | capacity--; |
1444 | (*count)++; | |
7474e7d3 | 1445 | } |
7474e7d3 | 1446 | |
5b9e151d PP |
1447 | BT_ASSERT(*count > 0); |
1448 | ||
da9c4c52 | 1449 | if (g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1450 | /* No more auto-seek messages, restore user's next callback. */ |
1451 | BT_ASSERT(iterator->auto_seek.original_next_callback); | |
1452 | iterator->methods.next = iterator->auto_seek.original_next_callback; | |
1453 | iterator->auto_seek.original_next_callback = NULL; | |
7474e7d3 PP |
1454 | } |
1455 | ||
1456 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
1457 | } | |
1458 | ||
5b7b55be SM |
1459 | static inline |
1460 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, | |
1461 | int64_t ns_from_origin, uint64_t *raw_value) | |
1462 | { | |
1463 | ||
1464 | int64_t cc_offset_s = clock_class->offset_seconds; | |
1465 | uint64_t cc_offset_cycles = clock_class->offset_cycles; | |
1466 | uint64_t cc_freq = clock_class->frequency; | |
1467 | ||
1468 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, | |
1469 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); | |
1470 | } | |
1471 | ||
1472 | ||
7474e7d3 PP |
1473 | enum bt_message_iterator_status |
1474 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1475 | struct bt_self_component_port_input_message_iterator *iterator, | |
1476 | int64_t ns_from_origin) | |
1477 | { | |
1478 | int status; | |
5b7b55be | 1479 | GHashTable *stream_states = NULL; |
7474e7d3 PP |
1480 | |
1481 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1482 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1483 | BT_ASSERT_PRE( |
1484 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1485 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1486 | "Graph is not configured: %!+g", |
1487 | bt_component_borrow_graph(iterator->upstream_component)); | |
1488 | BT_ASSERT_PRE( | |
1489 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1490 | iterator, ns_from_origin), | |
1491 | "Message iterator cannot seek nanoseconds from origin: %!+i, " | |
1492 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); | |
1493 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1494 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
1495 | ||
1496 | if (iterator->methods.seek_ns_from_origin) { | |
5b7b55be | 1497 | /* The iterator knows how to seek to a particular time, let it handle this. */ |
7474e7d3 PP |
1498 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " |
1499 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); | |
1500 | status = iterator->methods.seek_ns_from_origin(iterator, | |
1501 | ns_from_origin); | |
1502 | BT_LOGD("User method returned: status=%s", | |
1503 | bt_message_iterator_status_string(status)); | |
1504 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1505 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1506 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1507 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1508 | "Unexpected status: %![iter-]+i, status=%s", | |
1509 | iterator, | |
bd1a54fe | 1510 | bt_common_self_message_iterator_status_string(status)); |
7474e7d3 | 1511 | } else { |
5b7b55be SM |
1512 | /* |
1513 | * The iterator doesn't know how to seek to a particular time. We will | |
1514 | * seek to the beginning and fast forward to the right place. | |
1515 | */ | |
7474e7d3 PP |
1516 | BT_ASSERT(iterator->methods.can_seek_beginning(iterator)); |
1517 | BT_ASSERT(iterator->methods.seek_beginning); | |
1518 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", | |
1519 | iterator); | |
1520 | status = iterator->methods.seek_beginning(iterator); | |
1521 | BT_LOGD("User method returned: status=%s", | |
1522 | bt_message_iterator_status_string(status)); | |
1523 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1524 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1525 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1526 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1527 | "Unexpected status: %![iter-]+i, status=%s", | |
1528 | iterator, | |
bd1a54fe | 1529 | bt_common_self_message_iterator_status_string(status)); |
7474e7d3 PP |
1530 | switch (status) { |
1531 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1532 | break; | |
1533 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1534 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1535 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1536 | goto end; | |
1537 | default: | |
1538 | abort(); | |
1539 | } | |
1540 | ||
1541 | /* | |
1542 | * Find the first message which has a default clock | |
1543 | * snapshot greater than or equal to the requested | |
5b9e151d PP |
1544 | * seeking time, and move the received messages from |
1545 | * this point in the batch to this iterator's auto-seek | |
1546 | * message queue. | |
7474e7d3 | 1547 | */ |
da9c4c52 | 1548 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
5b9e151d | 1549 | bt_object_put_no_null_check( |
da9c4c52 | 1550 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
5b9e151d PP |
1551 | } |
1552 | ||
5b7b55be SM |
1553 | stream_states = create_auto_seek_stream_states(); |
1554 | if (!stream_states) { | |
1555 | BT_LOGE_STR("Failed to allocate one GHashTable."); | |
1556 | status = BT_MESSAGE_ITERATOR_STATUS_NOMEM; | |
1557 | goto end; | |
1558 | } | |
1559 | ||
7474e7d3 | 1560 | status = find_message_ge_ns_from_origin(iterator, |
5b7b55be | 1561 | ns_from_origin, stream_states); |
7474e7d3 PP |
1562 | switch (status) { |
1563 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
5b9e151d | 1564 | case BT_MESSAGE_ITERATOR_STATUS_END: |
5b7b55be SM |
1565 | { |
1566 | GHashTableIter iter; | |
1567 | gpointer key, value; | |
1568 | ||
1569 | /* | |
1570 | * If some streams exist at the seek time, prepend the | |
1571 | * required messages to put those streams in the right | |
1572 | * state. | |
1573 | */ | |
1574 | g_hash_table_iter_init(&iter, stream_states); | |
1575 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
1576 | const bt_stream *stream = key; | |
1577 | struct auto_seek_stream_state *stream_state = | |
1578 | (struct auto_seek_stream_state *) value; | |
1579 | bt_message *msg; | |
1580 | const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const( | |
1581 | bt_stream_borrow_class_const(stream)); | |
1582 | uint64_t raw_value; | |
1583 | ||
1584 | if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) { | |
1585 | BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K", | |
1586 | ns_from_origin, clock_class); | |
1587 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1588 | goto end; | |
1589 | } | |
1590 | ||
1591 | switch (stream_state->state) { | |
1592 | case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN: | |
1593 | BT_ASSERT(stream_state->packet); | |
1594 | BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet); | |
1595 | msg = bt_message_packet_beginning_create_with_default_clock_snapshot( | |
1596 | (bt_self_message_iterator *) iterator, stream_state->packet, raw_value); | |
1597 | if (!msg) { | |
1598 | status = BT_MESSAGE_ITERATOR_STATUS_NOMEM; | |
1599 | goto end; | |
1600 | } | |
1601 | ||
1602 | g_queue_push_head(iterator->auto_seek.msgs, msg); | |
1603 | msg = NULL; | |
1604 | /* fall-thru */ | |
1605 | case AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN: | |
1606 | msg = bt_message_stream_activity_beginning_create( | |
1607 | (bt_self_message_iterator *) iterator, stream); | |
1608 | if (!msg) { | |
1609 | status = BT_MESSAGE_ITERATOR_STATUS_NOMEM; | |
1610 | goto end; | |
1611 | } | |
1612 | ||
1613 | bt_message_stream_activity_beginning_set_default_clock_snapshot(msg, raw_value); | |
1614 | ||
1615 | g_queue_push_head(iterator->auto_seek.msgs, msg); | |
1616 | msg = NULL; | |
1617 | /* fall-thru */ | |
1618 | case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN: | |
1619 | msg = bt_message_stream_beginning_create( | |
1620 | (bt_self_message_iterator *) iterator, stream); | |
1621 | if (!msg) { | |
1622 | status = BT_MESSAGE_ITERATOR_STATUS_NOMEM; | |
1623 | goto end; | |
1624 | } | |
1625 | ||
1626 | g_queue_push_head(iterator->auto_seek.msgs, msg); | |
1627 | msg = NULL; | |
1628 | break; | |
1629 | } | |
1630 | } | |
1631 | ||
7474e7d3 | 1632 | /* |
5b9e151d PP |
1633 | * If there are messages in the auto-seek |
1634 | * message queue, replace the user's "next" | |
1635 | * method with a custom, temporary "next" method | |
1636 | * which returns them. | |
7474e7d3 | 1637 | */ |
da9c4c52 | 1638 | if (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1639 | BT_ASSERT(!iterator->auto_seek.original_next_callback); |
1640 | iterator->auto_seek.original_next_callback = iterator->methods.next; | |
1641 | ||
5b9e151d PP |
1642 | iterator->methods.next = |
1643 | (bt_self_component_port_input_message_iterator_next_method) | |
1644 | post_auto_seek_next; | |
1645 | } | |
1646 | ||
1647 | /* | |
1648 | * `BT_MESSAGE_ITERATOR_STATUS_END` becomes | |
1649 | * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next | |
1650 | * time this iterator's "next" method is called, | |
1651 | * it will return | |
1652 | * `BT_MESSAGE_ITERATOR_STATUS_END`. | |
1653 | */ | |
1654 | status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
7474e7d3 | 1655 | break; |
5b7b55be | 1656 | } |
7474e7d3 PP |
1657 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
1658 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1659 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1660 | goto end; | |
7474e7d3 PP |
1661 | default: |
1662 | abort(); | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | end: | |
5b7b55be SM |
1667 | if (stream_states) { |
1668 | destroy_auto_seek_stream_states(stream_states); | |
1669 | stream_states = NULL; | |
1670 | } | |
7474e7d3 | 1671 | set_iterator_state_after_seeking(iterator, status); |
7474e7d3 PP |
1672 | return status; |
1673 | } | |
1674 | ||
1675 | static inline | |
1676 | bt_self_component_port_input_message_iterator * | |
1677 | borrow_output_port_message_iterator_upstream_iterator( | |
1678 | struct bt_port_output_message_iterator *iterator) | |
1679 | { | |
1680 | struct bt_component_class_sink_colander_priv_data *colander_data; | |
1681 | ||
1682 | BT_ASSERT(iterator); | |
1683 | colander_data = (void *) iterator->colander->parent.user_data; | |
1684 | BT_ASSERT(colander_data); | |
1685 | BT_ASSERT(colander_data->msg_iter); | |
1686 | return colander_data->msg_iter; | |
1687 | } | |
1688 | ||
1689 | bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( | |
1690 | struct bt_port_output_message_iterator *iterator, | |
1691 | int64_t ns_from_origin) | |
1692 | { | |
1693 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1694 | return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1695 | borrow_output_port_message_iterator_upstream_iterator( | |
1696 | iterator), ns_from_origin); | |
1697 | } | |
1698 | ||
1699 | bt_bool bt_port_output_message_iterator_can_seek_beginning( | |
1700 | struct bt_port_output_message_iterator *iterator) | |
1701 | { | |
1702 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1703 | return bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1704 | borrow_output_port_message_iterator_upstream_iterator( | |
1705 | iterator)); | |
1706 | } | |
1707 | ||
1708 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin( | |
1709 | struct bt_port_output_message_iterator *iterator, | |
1710 | int64_t ns_from_origin) | |
1711 | { | |
1712 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1713 | return bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1714 | borrow_output_port_message_iterator_upstream_iterator(iterator), | |
1715 | ns_from_origin); | |
1716 | } | |
1717 | ||
1718 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning( | |
1719 | struct bt_port_output_message_iterator *iterator) | |
1720 | { | |
1721 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1722 | return bt_self_component_port_input_message_iterator_seek_beginning( | |
1723 | borrow_output_port_message_iterator_upstream_iterator( | |
1724 | iterator)); | |
1725 | } | |
1726 | ||
d6e69534 PP |
1727 | void bt_port_output_message_iterator_get_ref( |
1728 | const struct bt_port_output_message_iterator *iterator) | |
c5b9b441 PP |
1729 | { |
1730 | bt_object_get_ref(iterator); | |
1731 | } | |
1732 | ||
d6e69534 PP |
1733 | void bt_port_output_message_iterator_put_ref( |
1734 | const struct bt_port_output_message_iterator *iterator) | |
c5b9b441 PP |
1735 | { |
1736 | bt_object_put_ref(iterator); | |
1737 | } | |
1738 | ||
d6e69534 PP |
1739 | void bt_self_component_port_input_message_iterator_get_ref( |
1740 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1741 | { |
1742 | bt_object_get_ref(iterator); | |
1743 | } | |
1744 | ||
d6e69534 PP |
1745 | void bt_self_component_port_input_message_iterator_put_ref( |
1746 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1747 | { |
1748 | bt_object_put_ref(iterator); | |
1749 | } |