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" | |
d24d5663 PP |
37 | #include <babeltrace2/trace-ir/clock-class-const.h> |
38 | #include <babeltrace2/trace-ir/stream-class-const.h> | |
39 | #include <babeltrace2/trace-ir/stream-const.h> | |
3fadfbc0 | 40 | #include <babeltrace2/graph/connection-const.h> |
3fadfbc0 | 41 | #include <babeltrace2/graph/component-const.h> |
3fadfbc0 | 42 | #include <babeltrace2/graph/component-sink-const.h> |
3fadfbc0 | 43 | #include <babeltrace2/graph/message-const.h> |
d24d5663 | 44 | #include <babeltrace2/graph/message-iterator.h> |
3fadfbc0 | 45 | #include <babeltrace2/graph/self-component-port-input-message-iterator.h> |
3fadfbc0 | 46 | #include <babeltrace2/graph/message-event-const.h> |
5b7b55be SM |
47 | #include <babeltrace2/graph/message-message-iterator-inactivity-const.h> |
48 | #include <babeltrace2/graph/message-packet-beginning.h> | |
3fadfbc0 MJ |
49 | #include <babeltrace2/graph/message-packet-beginning-const.h> |
50 | #include <babeltrace2/graph/message-packet-end-const.h> | |
5b7b55be | 51 | #include <babeltrace2/graph/message-stream-beginning.h> |
3fadfbc0 MJ |
52 | #include <babeltrace2/graph/message-stream-beginning-const.h> |
53 | #include <babeltrace2/graph/message-stream-end-const.h> | |
3fadfbc0 MJ |
54 | #include <babeltrace2/graph/port-const.h> |
55 | #include <babeltrace2/graph/graph.h> | |
56 | #include <babeltrace2/graph/graph-const.h> | |
3fadfbc0 | 57 | #include <babeltrace2/types.h> |
578e048b MJ |
58 | #include "common/assert.h" |
59 | #include "lib/assert-pre.h" | |
f6f301d7 | 60 | #include "lib/assert-post.h" |
fa054faf | 61 | #include <stdint.h> |
2ec84d26 | 62 | #include <inttypes.h> |
c4f23e30 | 63 | #include <stdbool.h> |
0fbb9a9f | 64 | #include <stdlib.h> |
3230ee6b | 65 | |
578e048b | 66 | #include "component-class.h" |
578e048b MJ |
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" | |
d24d5663 | 79 | #include "lib/func-status.h" |
578e048b | 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 | { | |
98b15851 | 99 | BT_ASSERT_DBG(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 | ||
47e5a032 | 105 | static |
d6e69534 | 106 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
47e5a032 | 107 | { |
d6e69534 | 108 | struct bt_self_component_port_input_message_iterator *iterator; |
8738a040 | 109 | |
f6ccaed9 | 110 | BT_ASSERT(obj); |
d3eb6e8f | 111 | |
bd14d768 | 112 | /* |
d6e69534 | 113 | * The message iterator's reference count is 0 if we're |
bd14d768 PP |
114 | * here. Increment it to avoid a double-destroy (possibly |
115 | * infinitely recursive). This could happen for example if the | |
d6e69534 | 116 | * message iterator's finalization function does |
d94d92ac PP |
117 | * bt_object_get_ref() (or anything that causes |
118 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
119 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
120 | * count would go from 1 to 0 again and this function would be | |
121 | * called again. | |
bd14d768 | 122 | */ |
3fea54f6 | 123 | obj->ref_count++; |
07245ac2 | 124 | iterator = (void *) obj; |
3f7d4d90 | 125 | BT_LIB_LOGI("Destroying self component input port message iterator object: " |
d94d92ac | 126 | "%!+i", iterator); |
d0fea130 | 127 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
d3eb6e8f | 128 | |
bd14d768 PP |
129 | if (iterator->connection) { |
130 | /* | |
131 | * Remove ourself from the originating connection so | |
132 | * that it does not try to finalize a dangling pointer | |
133 | * later. | |
134 | */ | |
135 | bt_connection_remove_iterator(iterator->connection, iterator); | |
d94d92ac | 136 | iterator->connection = NULL; |
bd14d768 PP |
137 | } |
138 | ||
da9c4c52 SM |
139 | if (iterator->auto_seek.msgs) { |
140 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { | |
6871026b | 141 | bt_object_put_ref_no_null_check( |
da9c4c52 | 142 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
7474e7d3 PP |
143 | } |
144 | ||
da9c4c52 SM |
145 | g_queue_free(iterator->auto_seek.msgs); |
146 | iterator->auto_seek.msgs = NULL; | |
7474e7d3 PP |
147 | } |
148 | ||
ca02df0a PP |
149 | if (iterator->upstream_msg_iters) { |
150 | /* | |
151 | * At this point the message iterator is finalized, so | |
152 | * it's detached from any upstream message iterator. | |
153 | */ | |
154 | BT_ASSERT(iterator->upstream_msg_iters->len == 0); | |
155 | g_ptr_array_free(iterator->upstream_msg_iters, TRUE); | |
156 | iterator->upstream_msg_iters = NULL; | |
157 | } | |
158 | ||
6c373cc9 PP |
159 | if (iterator->msgs) { |
160 | g_ptr_array_free(iterator->msgs, TRUE); | |
161 | iterator->msgs = NULL; | |
162 | } | |
163 | ||
164 | g_free(iterator); | |
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 | { |
ca02df0a | 171 | uint64_t i; |
d94d92ac PP |
172 | typedef void (*method_t)(void *); |
173 | ||
bd14d768 | 174 | struct bt_component_class *comp_class = NULL; |
d94d92ac | 175 | method_t method = NULL; |
bd14d768 | 176 | |
f6ccaed9 | 177 | BT_ASSERT(iterator); |
bd14d768 PP |
178 | |
179 | switch (iterator->state) { | |
d6e69534 | 180 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 | 181 | /* Skip user finalization if user initialization failed */ |
d6e69534 | 182 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
d94d92ac | 183 | "%!+i", iterator); |
d0fea130 | 184 | goto end; |
d6e69534 | 185 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
bd14d768 | 186 | /* Already finalized */ |
d6e69534 | 187 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
d94d92ac | 188 | "%!+i", iterator); |
d0fea130 PP |
189 | goto end; |
190 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: | |
870631a2 | 191 | /* Finalizing */ |
d0fea130 PP |
192 | BT_LIB_LOGF("Message iterator is already being finalized: " |
193 | "%!+i", iterator); | |
498e7994 | 194 | bt_common_abort(); |
bd14d768 PP |
195 | default: |
196 | break; | |
197 | } | |
198 | ||
d6e69534 | 199 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
d0fea130 PP |
200 | set_self_comp_port_input_msg_iterator_state(iterator, |
201 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); | |
f6ccaed9 | 202 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
203 | comp_class = iterator->upstream_component->class; |
204 | ||
205 | /* Call user-defined destroy method */ | |
206 | switch (comp_class->type) { | |
207 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
208 | { | |
d94d92ac PP |
209 | struct bt_component_class_source *src_comp_cls = |
210 | (void *) comp_class; | |
bd14d768 | 211 | |
d6e69534 | 212 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
213 | break; |
214 | } | |
215 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
216 | { | |
d94d92ac PP |
217 | struct bt_component_class_filter *flt_comp_cls = |
218 | (void *) comp_class; | |
bd14d768 | 219 | |
d6e69534 | 220 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
221 | break; |
222 | } | |
223 | default: | |
224 | /* Unreachable */ | |
498e7994 | 225 | bt_common_abort(); |
bd14d768 PP |
226 | } |
227 | ||
d94d92ac PP |
228 | if (method) { |
229 | BT_LIB_LOGD("Calling user's finalization method: %!+i", | |
5af447e5 | 230 | iterator); |
d94d92ac | 231 | method(iterator); |
bd14d768 PP |
232 | } |
233 | ||
ca02df0a PP |
234 | /* Detach upstream message iterators */ |
235 | for (i = 0; i < iterator->upstream_msg_iters->len; i++) { | |
236 | struct bt_self_component_port_input_message_iterator *upstream_msg_iter = | |
237 | iterator->upstream_msg_iters->pdata[i]; | |
238 | ||
239 | upstream_msg_iter->downstream_msg_iter = NULL; | |
240 | } | |
241 | ||
242 | g_ptr_array_set_size(iterator->upstream_msg_iters, 0); | |
243 | ||
244 | /* Detach downstream message iterator */ | |
245 | if (iterator->downstream_msg_iter) { | |
246 | gboolean existed; | |
247 | ||
248 | BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters); | |
249 | existed = g_ptr_array_remove_fast( | |
250 | iterator->downstream_msg_iter->upstream_msg_iters, | |
251 | iterator); | |
252 | BT_ASSERT(existed); | |
253 | } | |
254 | ||
bd14d768 PP |
255 | iterator->upstream_component = NULL; |
256 | iterator->upstream_port = NULL; | |
d0fea130 PP |
257 | set_self_comp_port_input_msg_iterator_state(iterator, |
258 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); | |
d6e69534 | 259 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
d0fea130 PP |
260 | |
261 | end: | |
262 | return; | |
bd14d768 PP |
263 | } |
264 | ||
265 | BT_HIDDEN | |
d6e69534 PP |
266 | void bt_self_component_port_input_message_iterator_set_connection( |
267 | struct bt_self_component_port_input_message_iterator *iterator, | |
bd14d768 PP |
268 | struct bt_connection *connection) |
269 | { | |
f6ccaed9 | 270 | BT_ASSERT(iterator); |
bd14d768 | 271 | iterator->connection = connection; |
3f7d4d90 | 272 | BT_LIB_LOGI("Set message iterator's connection: " |
d94d92ac | 273 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
bd14d768 PP |
274 | } |
275 | ||
7474e7d3 | 276 | static |
f2fb1b32 | 277 | enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true( |
7474e7d3 | 278 | struct bt_self_component_port_input_message_iterator *iterator, |
f2fb1b32 | 279 | int64_t ns_from_origin, bt_bool *can_seek) |
7474e7d3 | 280 | { |
f2fb1b32 SM |
281 | *can_seek = BT_TRUE; |
282 | ||
283 | return BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
284 | } |
285 | ||
286 | static | |
f2fb1b32 SM |
287 | enum bt_message_iterator_can_seek_beginning_status can_seek_beginning_true( |
288 | struct bt_self_component_port_input_message_iterator *iterator, | |
289 | bt_bool *can_seek) | |
7474e7d3 | 290 | { |
f2fb1b32 SM |
291 | *can_seek = BT_TRUE; |
292 | ||
293 | return BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
294 | } |
295 | ||
d94d92ac | 296 | static |
e803df70 | 297 | int create_self_component_input_port_message_iterator( |
ca02df0a | 298 | struct bt_self_message_iterator *self_downstream_msg_iter, |
e803df70 SM |
299 | struct bt_self_component_port_input *self_port, |
300 | struct bt_self_component_port_input_message_iterator **message_iterator) | |
47e5a032 | 301 | { |
21a9f056 | 302 | typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t)( |
8d8b141d | 303 | void *, void *, void *, void *); |
ca02df0a | 304 | |
ca02df0a PP |
305 | init_method_t init_method = NULL; |
306 | struct bt_self_component_port_input_message_iterator *iterator = | |
307 | NULL; | |
308 | struct bt_self_component_port_input_message_iterator *downstream_msg_iter = | |
309 | (void *) self_downstream_msg_iter; | |
310 | struct bt_port *port = (void *) self_port; | |
311 | struct bt_port *upstream_port; | |
312 | struct bt_component *comp; | |
313 | struct bt_component *upstream_comp; | |
314 | struct bt_component_class *upstream_comp_cls; | |
e803df70 | 315 | int status; |
47e5a032 | 316 | |
e803df70 | 317 | BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator"); |
ca02df0a PP |
318 | BT_ASSERT_PRE_NON_NULL(port, "Input port"); |
319 | comp = bt_port_borrow_component_inline(port); | |
320 | BT_ASSERT_PRE(bt_port_is_connected(port), | |
321 | "Input port is not connected: %![port-]+p", port); | |
322 | BT_ASSERT_PRE(comp, "Input port is not part of a component: %![port-]+p", | |
323 | port); | |
ca02df0a PP |
324 | BT_ASSERT(port->connection); |
325 | upstream_port = port->connection->upstream_port; | |
f6ccaed9 | 326 | BT_ASSERT(upstream_port); |
ca02df0a PP |
327 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
328 | BT_ASSERT(upstream_comp); | |
329 | BT_ASSERT_PRE( | |
9b4f9b42 PP |
330 | bt_component_borrow_graph(upstream_comp)->config_state == |
331 | BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED || | |
332 | bt_component_borrow_graph(upstream_comp)->config_state == | |
333 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURED, | |
ca02df0a PP |
334 | "Graph is not configured: %!+g", |
335 | bt_component_borrow_graph(upstream_comp)); | |
336 | upstream_comp_cls = upstream_comp->class; | |
337 | BT_ASSERT(upstream_comp->class->type == | |
d94d92ac | 338 | BT_COMPONENT_CLASS_TYPE_SOURCE || |
ca02df0a | 339 | upstream_comp->class->type == |
d94d92ac | 340 | BT_COMPONENT_CLASS_TYPE_FILTER); |
ca02df0a PP |
341 | BT_LIB_LOGI("Creating message iterator on self component input port: " |
342 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); | |
d94d92ac | 343 | iterator = g_new0( |
d6e69534 | 344 | struct bt_self_component_port_input_message_iterator, 1); |
47e5a032 | 345 | if (!iterator) { |
870631a2 PP |
346 | BT_LIB_LOGE_APPEND_CAUSE( |
347 | "Failed to allocate one self component input port " | |
d6e69534 | 348 | "message iterator."); |
e803df70 | 349 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
870631a2 | 350 | goto error; |
47e5a032 JG |
351 | } |
352 | ||
6c373cc9 | 353 | bt_object_init_shared(&iterator->base, |
d6e69534 | 354 | bt_self_component_port_input_message_iterator_destroy); |
6c373cc9 PP |
355 | iterator->msgs = g_ptr_array_new(); |
356 | if (!iterator->msgs) { | |
357 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
e803df70 | 358 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
870631a2 | 359 | goto error; |
d4393e08 | 360 | } |
3230ee6b | 361 | |
6c373cc9 | 362 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
54b135a0 | 363 | iterator->last_ns_from_origin = INT64_MIN; |
da9c4c52 SM |
364 | iterator->auto_seek.msgs = g_queue_new(); |
365 | if (!iterator->auto_seek.msgs) { | |
870631a2 | 366 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue."); |
e803df70 | 367 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
ca02df0a PP |
368 | goto error; |
369 | } | |
370 | ||
371 | iterator->upstream_msg_iters = g_ptr_array_new(); | |
372 | if (!iterator->upstream_msg_iters) { | |
373 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
e803df70 | 374 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
ca02df0a | 375 | goto error; |
3230ee6b PP |
376 | } |
377 | ||
bd14d768 PP |
378 | iterator->upstream_component = upstream_comp; |
379 | iterator->upstream_port = upstream_port; | |
d94d92ac | 380 | iterator->connection = iterator->upstream_port->connection; |
5c563278 | 381 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
d0fea130 PP |
382 | set_self_comp_port_input_msg_iterator_state(iterator, |
383 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); | |
7474e7d3 PP |
384 | |
385 | switch (iterator->upstream_component->class->type) { | |
386 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
387 | { | |
388 | struct bt_component_class_source *src_comp_cls = | |
389 | (void *) iterator->upstream_component->class; | |
390 | ||
391 | iterator->methods.next = | |
392 | (bt_self_component_port_input_message_iterator_next_method) | |
393 | src_comp_cls->methods.msg_iter_next; | |
394 | iterator->methods.seek_ns_from_origin = | |
395 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
396 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
397 | iterator->methods.seek_beginning = | |
398 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
399 | src_comp_cls->methods.msg_iter_seek_beginning; | |
400 | iterator->methods.can_seek_ns_from_origin = | |
401 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
402 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
403 | iterator->methods.can_seek_beginning = | |
404 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
405 | src_comp_cls->methods.msg_iter_can_seek_beginning; | |
406 | break; | |
407 | } | |
408 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
409 | { | |
410 | struct bt_component_class_filter *flt_comp_cls = | |
411 | (void *) iterator->upstream_component->class; | |
412 | ||
413 | iterator->methods.next = | |
414 | (bt_self_component_port_input_message_iterator_next_method) | |
415 | flt_comp_cls->methods.msg_iter_next; | |
416 | iterator->methods.seek_ns_from_origin = | |
417 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
418 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
419 | iterator->methods.seek_beginning = | |
420 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
421 | flt_comp_cls->methods.msg_iter_seek_beginning; | |
422 | iterator->methods.can_seek_ns_from_origin = | |
423 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
424 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
425 | iterator->methods.can_seek_beginning = | |
426 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
427 | flt_comp_cls->methods.msg_iter_can_seek_beginning; | |
428 | break; | |
429 | } | |
430 | default: | |
498e7994 | 431 | bt_common_abort(); |
7474e7d3 PP |
432 | } |
433 | ||
434 | if (iterator->methods.seek_ns_from_origin && | |
435 | !iterator->methods.can_seek_ns_from_origin) { | |
436 | iterator->methods.can_seek_ns_from_origin = | |
437 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
438 | can_seek_ns_from_origin_true; | |
439 | } | |
440 | ||
441 | if (iterator->methods.seek_beginning && | |
442 | !iterator->methods.can_seek_beginning) { | |
443 | iterator->methods.can_seek_beginning = | |
86751779 | 444 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) |
7474e7d3 PP |
445 | can_seek_beginning_true; |
446 | } | |
447 | ||
d94d92ac PP |
448 | switch (upstream_comp_cls->type) { |
449 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
450 | { | |
451 | struct bt_component_class_source *src_comp_cls = | |
452 | (void *) upstream_comp_cls; | |
453 | ||
454 | init_method = | |
21a9f056 | 455 | (init_method_t) src_comp_cls->methods.msg_iter_initialize; |
d94d92ac PP |
456 | break; |
457 | } | |
458 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
459 | { | |
460 | struct bt_component_class_filter *flt_comp_cls = | |
461 | (void *) upstream_comp_cls; | |
462 | ||
463 | init_method = | |
21a9f056 | 464 | (init_method_t) flt_comp_cls->methods.msg_iter_initialize; |
d94d92ac PP |
465 | break; |
466 | } | |
467 | default: | |
468 | /* Unreachable */ | |
498e7994 | 469 | bt_common_abort(); |
d94d92ac PP |
470 | } |
471 | ||
472 | if (init_method) { | |
21a9f056 | 473 | enum bt_component_class_message_iterator_initialize_method_status iter_status; |
d94d92ac PP |
474 | |
475 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); | |
8d8b141d | 476 | iter_status = init_method(iterator, &iterator->config, upstream_comp, |
d94d92ac PP |
477 | upstream_port); |
478 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
479 | bt_common_func_status_string(iter_status)); |
480 | if (iter_status != BT_FUNC_STATUS_OK) { | |
870631a2 PP |
481 | BT_LIB_LOGW_APPEND_CAUSE( |
482 | "Component input port message iterator initialization method failed: " | |
483 | "%![iter-]+i, status=%s", | |
484 | iterator, | |
485 | bt_common_func_status_string(iter_status)); | |
e803df70 | 486 | status = iter_status; |
870631a2 | 487 | goto error; |
d94d92ac | 488 | } |
8d8b141d SM |
489 | |
490 | iterator->config.frozen = true; | |
d94d92ac PP |
491 | } |
492 | ||
ca02df0a PP |
493 | if (downstream_msg_iter) { |
494 | /* Set this message iterator's downstream message iterator */ | |
495 | iterator->downstream_msg_iter = downstream_msg_iter; | |
496 | ||
497 | /* | |
498 | * Add this message iterator to the downstream message | |
499 | * iterator's array of upstream message iterators. | |
500 | */ | |
501 | g_ptr_array_add(downstream_msg_iter->upstream_msg_iters, | |
502 | iterator); | |
503 | } | |
504 | ||
d0fea130 PP |
505 | set_self_comp_port_input_msg_iterator_state(iterator, |
506 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
d94d92ac | 507 | g_ptr_array_add(port->connection->iterators, iterator); |
3f7d4d90 | 508 | BT_LIB_LOGI("Created message iterator on self component input port: " |
d94d92ac PP |
509 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
510 | upstream_port, upstream_comp, iterator); | |
e803df70 SM |
511 | |
512 | *message_iterator = iterator; | |
513 | status = BT_FUNC_STATUS_OK; | |
870631a2 PP |
514 | goto end; |
515 | ||
516 | error: | |
517 | BT_OBJECT_PUT_REF_AND_RESET(iterator); | |
d94d92ac PP |
518 | |
519 | end: | |
e803df70 | 520 | return status; |
ea8d3e58 JG |
521 | } |
522 | ||
e803df70 | 523 | bt_self_component_port_input_message_iterator_create_from_message_iterator_status |
ca02df0a PP |
524 | bt_self_component_port_input_message_iterator_create_from_message_iterator( |
525 | struct bt_self_message_iterator *self_msg_iter, | |
e803df70 SM |
526 | struct bt_self_component_port_input *input_port, |
527 | struct bt_self_component_port_input_message_iterator **message_iterator) | |
ca02df0a PP |
528 | { |
529 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); | |
530 | return create_self_component_input_port_message_iterator(self_msg_iter, | |
e803df70 | 531 | input_port, message_iterator); |
ca02df0a PP |
532 | } |
533 | ||
e803df70 | 534 | bt_self_component_port_input_message_iterator_create_from_sink_component_status |
ca02df0a PP |
535 | bt_self_component_port_input_message_iterator_create_from_sink_component( |
536 | struct bt_self_component_sink *self_comp, | |
e803df70 SM |
537 | struct bt_self_component_port_input *input_port, |
538 | struct bt_self_component_port_input_message_iterator **message_iterator) | |
ca02df0a PP |
539 | { |
540 | BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component"); | |
541 | return create_self_component_input_port_message_iterator(NULL, | |
e803df70 | 542 | input_port, message_iterator); |
ca02df0a PP |
543 | } |
544 | ||
d6e69534 PP |
545 | void *bt_self_message_iterator_get_data( |
546 | const struct bt_self_message_iterator *self_iterator) | |
ea8d3e58 | 547 | { |
d6e69534 | 548 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 549 | (void *) self_iterator; |
ea8d3e58 | 550 | |
bdb288b3 | 551 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 552 | return iterator->user_data; |
8738a040 | 553 | } |
413bc2c4 | 554 | |
d6e69534 PP |
555 | void bt_self_message_iterator_set_data( |
556 | struct bt_self_message_iterator *self_iterator, void *data) | |
5c563278 | 557 | { |
d6e69534 | 558 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 559 | (void *) self_iterator; |
5c563278 | 560 | |
bdb288b3 | 561 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 562 | iterator->user_data = data; |
3f7d4d90 | 563 | BT_LIB_LOGD("Set message iterator's user data: " |
d94d92ac | 564 | "%!+i, user-data-addr=%p", iterator, data); |
5c563278 PP |
565 | } |
566 | ||
8d8b141d SM |
567 | void bt_self_message_iterator_configuration_set_can_seek_forward( |
568 | bt_self_message_iterator_configuration *config, | |
569 | bt_bool can_seek_forward) | |
570 | { | |
571 | BT_ASSERT_PRE_NON_NULL(config, "Message iterator configuration"); | |
572 | BT_ASSERT_PRE_DEV_HOT(config, "Message iterator configuration", ""); | |
573 | ||
574 | config->can_seek_forward = can_seek_forward; | |
575 | } | |
576 | ||
54b135a0 SM |
577 | /* |
578 | * Validate that the default clock snapshot in `msg` doesn't make us go back in | |
579 | * time. | |
580 | */ | |
581 | ||
bdb288b3 | 582 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
583 | static |
584 | bool clock_snapshots_are_monotonic_one( | |
585 | struct bt_self_component_port_input_message_iterator *iterator, | |
586 | const bt_message *msg) | |
587 | { | |
588 | const struct bt_clock_snapshot *clock_snapshot = NULL; | |
589 | bt_message_type message_type = bt_message_get_type(msg); | |
590 | int64_t ns_from_origin; | |
d24d5663 | 591 | enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status; |
54b135a0 SM |
592 | |
593 | /* | |
594 | * The default is true: if we can't figure out the clock snapshot | |
595 | * (or there is none), assume it is fine. | |
596 | */ | |
597 | bool result = true; | |
598 | ||
599 | switch (message_type) { | |
600 | case BT_MESSAGE_TYPE_EVENT: | |
601 | { | |
602 | struct bt_message_event *event_msg = (struct bt_message_event *) msg; | |
603 | clock_snapshot = event_msg->default_cs; | |
604 | break; | |
605 | } | |
606 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
607 | { | |
608 | struct bt_message_message_iterator_inactivity *inactivity_msg = | |
609 | (struct bt_message_message_iterator_inactivity *) msg; | |
610 | clock_snapshot = inactivity_msg->default_cs; | |
611 | break; | |
612 | } | |
613 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
614 | case BT_MESSAGE_TYPE_PACKET_END: | |
615 | { | |
616 | struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg; | |
617 | clock_snapshot = packet_msg->default_cs; | |
618 | break; | |
619 | } | |
188edac1 SM |
620 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
621 | case BT_MESSAGE_TYPE_STREAM_END: | |
54b135a0 | 622 | { |
188edac1 SM |
623 | struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; |
624 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
625 | goto end; | |
54b135a0 | 626 | } |
188edac1 SM |
627 | |
628 | clock_snapshot = stream_msg->default_cs; | |
54b135a0 SM |
629 | break; |
630 | } | |
54b135a0 SM |
631 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
632 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
633 | { | |
634 | struct bt_message_discarded_items *discarded_msg = | |
635 | (struct bt_message_discarded_items *) msg; | |
636 | ||
637 | clock_snapshot = discarded_msg->default_begin_cs; | |
638 | break; | |
639 | } | |
640 | } | |
641 | ||
642 | if (!clock_snapshot) { | |
643 | goto end; | |
644 | } | |
645 | ||
646 | clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ns_from_origin); | |
d24d5663 | 647 | if (clock_snapshot_status != BT_FUNC_STATUS_OK) { |
54b135a0 SM |
648 | goto end; |
649 | } | |
650 | ||
651 | result = ns_from_origin >= iterator->last_ns_from_origin; | |
652 | iterator->last_ns_from_origin = ns_from_origin; | |
653 | end: | |
654 | return result; | |
655 | } | |
656 | ||
bdb288b3 | 657 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
658 | static |
659 | bool clock_snapshots_are_monotonic( | |
660 | struct bt_self_component_port_input_message_iterator *iterator, | |
661 | bt_message_array_const msgs, uint64_t msg_count) | |
662 | { | |
663 | uint64_t i; | |
664 | bool result; | |
665 | ||
666 | for (i = 0; i < msg_count; i++) { | |
667 | if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) { | |
668 | result = false; | |
669 | goto end; | |
670 | } | |
671 | } | |
672 | ||
673 | result = true; | |
674 | ||
675 | end: | |
676 | return result; | |
677 | } | |
678 | ||
679 | /* | |
680 | * When a new stream begins, verify that the clock class tied to this | |
681 | * stream is compatible with what we've seen before. | |
682 | */ | |
683 | ||
bdb288b3 | 684 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
685 | static |
686 | bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator *iterator, | |
687 | const struct bt_message *msg) | |
688 | { | |
689 | enum bt_message_type message_type = bt_message_get_type(msg); | |
690 | bool result; | |
691 | ||
692 | if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) { | |
693 | const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; | |
694 | const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class; | |
695 | bt_uuid clock_class_uuid = NULL; | |
696 | ||
697 | if (clock_class) { | |
698 | clock_class_uuid = bt_clock_class_get_uuid(clock_class); | |
699 | } | |
700 | ||
701 | switch (iterator->clock_expectation.type) { | |
702 | case CLOCK_EXPECTATION_UNSET: | |
703 | /* | |
704 | * This is the first time we see a message with a clock | |
705 | * snapshot: record the properties of that clock, against | |
706 | * which we'll compare the clock properties of the following | |
707 | * messages. | |
708 | */ | |
709 | ||
710 | if (!clock_class) { | |
711 | iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE; | |
712 | } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
713 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX; | |
714 | } else if (clock_class_uuid) { | |
715 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID; | |
6162e6b7 | 716 | bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid); |
54b135a0 SM |
717 | } else { |
718 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID; | |
719 | } | |
720 | break; | |
721 | ||
722 | case CLOCK_EXPECTATION_NONE: | |
723 | if (clock_class) { | |
bdb288b3 PP |
724 | BT_ASSERT_POST_DEV_MSG( |
725 | "Expecting no clock class, got one: %![cc-]+K", | |
54b135a0 SM |
726 | clock_class); |
727 | result = false; | |
728 | goto end; | |
729 | } | |
730 | ||
731 | break; | |
732 | ||
733 | case CLOCK_EXPECTATION_ORIGIN_UNIX: | |
734 | if (!clock_class) { | |
bdb288b3 PP |
735 | BT_ASSERT_POST_DEV_MSG( |
736 | "Expecting a clock class, got none."); | |
54b135a0 SM |
737 | result = false; |
738 | goto end; | |
739 | } | |
740 | ||
741 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
742 | BT_ASSERT_POST_DEV_MSG( |
743 | "Expecting a clock class with Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
744 | clock_class); |
745 | result = false; | |
746 | goto end; | |
747 | } | |
748 | break; | |
749 | ||
750 | case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID: | |
751 | if (!clock_class) { | |
bdb288b3 PP |
752 | BT_ASSERT_POST_DEV_MSG( |
753 | "Expecting a clock class, got none."); | |
54b135a0 SM |
754 | result = false; |
755 | goto end; | |
756 | } | |
757 | ||
758 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
759 | BT_ASSERT_POST_DEV_MSG( |
760 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
761 | clock_class); |
762 | result = false; | |
763 | goto end; | |
764 | } | |
765 | ||
766 | if (!clock_class_uuid) { | |
bdb288b3 PP |
767 | BT_ASSERT_POST_DEV_MSG( |
768 | "Expecting a clock class with UUID: %![cc-]+K", | |
54b135a0 SM |
769 | clock_class); |
770 | result = false; | |
771 | goto end; | |
772 | } | |
773 | ||
774 | if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) { | |
bdb288b3 PP |
775 | BT_ASSERT_POST_DEV_MSG( |
776 | "Expecting a clock class with UUID, got one " | |
54b135a0 SM |
777 | "with a different UUID: %![cc-]+K, expected-uuid=%!u", |
778 | clock_class, iterator->clock_expectation.uuid); | |
779 | result = false; | |
780 | goto end; | |
781 | } | |
782 | break; | |
783 | ||
784 | case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID: | |
785 | if (!clock_class) { | |
bdb288b3 PP |
786 | BT_ASSERT_POST_DEV_MSG( |
787 | "Expecting a clock class, got none."); | |
54b135a0 SM |
788 | result = false; |
789 | goto end; | |
790 | } | |
791 | ||
792 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
793 | BT_ASSERT_POST_DEV_MSG( |
794 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
795 | clock_class); |
796 | result = false; | |
797 | goto end; | |
798 | } | |
799 | ||
800 | if (clock_class_uuid) { | |
bdb288b3 PP |
801 | BT_ASSERT_POST_DEV_MSG( |
802 | "Expecting a clock class without UUID: %![cc-]+K", | |
54b135a0 SM |
803 | clock_class); |
804 | result = false; | |
805 | goto end; | |
806 | } | |
807 | break; | |
808 | } | |
809 | } | |
810 | ||
811 | result = true; | |
812 | ||
813 | end: | |
814 | return result; | |
815 | } | |
816 | ||
bdb288b3 | 817 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
818 | static |
819 | bool clock_classes_are_compatible( | |
820 | struct bt_self_component_port_input_message_iterator *iterator, | |
821 | bt_message_array_const msgs, uint64_t msg_count) | |
822 | { | |
823 | uint64_t i; | |
824 | bool result; | |
825 | ||
826 | for (i = 0; i < msg_count; i++) { | |
827 | if (!clock_classes_are_compatible_one(iterator, msgs[i])) { | |
828 | result = false; | |
829 | goto end; | |
830 | } | |
831 | } | |
832 | ||
833 | result = true; | |
834 | ||
835 | end: | |
836 | return result; | |
837 | } | |
838 | ||
839 | /* | |
840 | * Call the `next` method of the iterator. Do some validation on the returned | |
841 | * messages. | |
842 | */ | |
843 | ||
844 | static | |
d24d5663 PP |
845 | enum bt_component_class_message_iterator_next_method_status |
846 | call_iterator_next_method( | |
54b135a0 SM |
847 | struct bt_self_component_port_input_message_iterator *iterator, |
848 | bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count) | |
849 | { | |
d24d5663 | 850 | enum bt_component_class_message_iterator_next_method_status status; |
54b135a0 | 851 | |
98b15851 | 852 | BT_ASSERT_DBG(iterator->methods.next); |
54b135a0 | 853 | BT_LOGD_STR("Calling user's \"next\" method."); |
54b135a0 | 854 | status = iterator->methods.next(iterator, msgs, capacity, user_count); |
f6f301d7 | 855 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
d24d5663 | 856 | bt_common_func_status_string(status), *user_count); |
54b135a0 | 857 | |
d24d5663 | 858 | if (status == BT_FUNC_STATUS_OK) { |
bdb288b3 | 859 | BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count), |
54b135a0 | 860 | "Clocks are not compatible"); |
bdb288b3 | 861 | BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count), |
54b135a0 SM |
862 | "Clock snapshots are not monotonic"); |
863 | } | |
864 | ||
865 | return status; | |
866 | } | |
867 | ||
d24d5663 | 868 | enum bt_message_iterator_next_status |
d6e69534 PP |
869 | bt_self_component_port_input_message_iterator_next( |
870 | struct bt_self_component_port_input_message_iterator *iterator, | |
871 | bt_message_array_const *msgs, uint64_t *user_count) | |
3230ee6b | 872 | { |
d24d5663 | 873 | enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK; |
d94d92ac | 874 | |
bdb288b3 PP |
875 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
876 | BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)"); | |
877 | BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)"); | |
878 | BT_ASSERT_PRE_DEV(iterator->state == | |
d6e69534 PP |
879 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
880 | "Message iterator's \"next\" called, but " | |
7474e7d3 | 881 | "message iterator is in the wrong state: %!+i", iterator); |
98b15851 PP |
882 | BT_ASSERT_DBG(iterator->upstream_component); |
883 | BT_ASSERT_DBG(iterator->upstream_component->class); | |
bdb288b3 | 884 | BT_ASSERT_PRE_DEV( |
5badd463 PP |
885 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
886 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
887 | "Graph is not configured: %!+g", |
888 | bt_component_borrow_graph(iterator->upstream_component)); | |
d94d92ac | 889 | BT_LIB_LOGD("Getting next self component input port " |
3f7d4d90 PP |
890 | "message iterator's messages: %!+i, batch-size=%u", |
891 | iterator, MSG_BATCH_SIZE); | |
d3eb6e8f | 892 | |
3230ee6b | 893 | /* |
d6e69534 | 894 | * Call the user's "next" method to get the next messages |
fa054faf | 895 | * and status. |
3230ee6b | 896 | */ |
3f7d4d90 | 897 | *user_count = 0; |
d24d5663 | 898 | status = (int) call_iterator_next_method(iterator, |
6c373cc9 | 899 | (void *) iterator->msgs->pdata, MSG_BATCH_SIZE, |
7474e7d3 | 900 | user_count); |
870631a2 PP |
901 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
902 | bt_common_func_status_string(status), *user_count); | |
d4393e08 | 903 | if (status < 0) { |
870631a2 PP |
904 | BT_LIB_LOGW_APPEND_CAUSE( |
905 | "Component input port message iterator's \"next\" method failed: " | |
906 | "%![iter-]+i, status=%s", | |
907 | iterator, bt_common_func_status_string(status)); | |
f42867e2 PP |
908 | goto end; |
909 | } | |
3230ee6b | 910 | |
d0fea130 PP |
911 | /* |
912 | * There is no way that this iterator could have been finalized | |
913 | * during its "next" method, as the only way to do this is to | |
914 | * put the last iterator's reference, and this can only be done | |
915 | * by its downstream owner. | |
7474e7d3 PP |
916 | * |
917 | * For the same reason, there is no way that this iterator could | |
918 | * have seeked (cannot seek a self message iterator). | |
d0fea130 | 919 | */ |
98b15851 | 920 | BT_ASSERT_DBG(iterator->state == |
d0fea130 | 921 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
8cf27cc5 | 922 | |
d4393e08 | 923 | switch (status) { |
d24d5663 | 924 | case BT_FUNC_STATUS_OK: |
bdb288b3 | 925 | BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE, |
7474e7d3 PP |
926 | "Invalid returned message count: greater than " |
927 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
928 | *user_count, MSG_BATCH_SIZE); | |
6c373cc9 | 929 | *msgs = (void *) iterator->msgs->pdata; |
d4393e08 | 930 | break; |
d24d5663 | 931 | case BT_FUNC_STATUS_AGAIN: |
d4393e08 | 932 | goto end; |
d24d5663 | 933 | case BT_FUNC_STATUS_END: |
d0fea130 PP |
934 | set_self_comp_port_input_msg_iterator_state(iterator, |
935 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); | |
f42867e2 | 936 | goto end; |
f42867e2 PP |
937 | default: |
938 | /* Unknown non-error status */ | |
498e7994 | 939 | bt_common_abort(); |
41a2b7ae PP |
940 | } |
941 | ||
942 | end: | |
3230ee6b PP |
943 | return status; |
944 | } | |
945 | ||
7474e7d3 PP |
946 | struct bt_component * |
947 | bt_self_component_port_input_message_iterator_borrow_component( | |
d6e69534 | 948 | struct bt_self_component_port_input_message_iterator *iterator) |
d94d92ac | 949 | { |
bdb288b3 | 950 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac PP |
951 | return iterator->upstream_component; |
952 | } | |
953 | ||
d6e69534 PP |
954 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
955 | struct bt_self_message_iterator *self_iterator) | |
413bc2c4 | 956 | { |
d6e69534 | 957 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 958 | (void *) self_iterator; |
90157d89 | 959 | |
bdb288b3 | 960 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 961 | return (void *) iterator->upstream_component; |
413bc2c4 JG |
962 | } |
963 | ||
50e763f6 | 964 | struct bt_self_component_port_output *bt_self_message_iterator_borrow_port( |
d6e69534 | 965 | struct bt_self_message_iterator *self_iterator) |
91457551 | 966 | { |
d6e69534 | 967 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac PP |
968 | (void *) self_iterator; |
969 | ||
bdb288b3 | 970 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 971 | return (void *) iterator->upstream_port; |
91457551 | 972 | } |
8ed535b5 | 973 | |
f2fb1b32 SM |
974 | enum bt_message_iterator_can_seek_ns_from_origin_status |
975 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
7474e7d3 | 976 | struct bt_self_component_port_input_message_iterator *iterator, |
f2fb1b32 | 977 | int64_t ns_from_origin, bt_bool *can_seek) |
7474e7d3 | 978 | { |
f2fb1b32 | 979 | enum bt_message_iterator_can_seek_ns_from_origin_status status; |
7474e7d3 PP |
980 | |
981 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
f2fb1b32 | 982 | BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)"); |
7474e7d3 | 983 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
5badd463 PP |
984 | BT_ASSERT_PRE( |
985 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
986 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
987 | "Graph is not configured: %!+g", |
988 | bt_component_borrow_graph(iterator->upstream_component)); | |
989 | ||
990 | if (iterator->methods.can_seek_ns_from_origin) { | |
f2fb1b32 SM |
991 | /* |
992 | * Initialize to an invalid value, so we can post-assert that | |
993 | * the method returned a valid value. | |
994 | */ | |
995 | *can_seek = -1; | |
996 | ||
c0e46a7c SM |
997 | BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i", |
998 | iterator); | |
999 | ||
f2fb1b32 SM |
1000 | status = (int) iterator->methods.can_seek_ns_from_origin(iterator, |
1001 | ns_from_origin, can_seek); | |
1002 | ||
c0e46a7c SM |
1003 | if (status != BT_FUNC_STATUS_OK) { |
1004 | BT_LIB_LOGW_APPEND_CAUSE( | |
1005 | "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: " | |
1006 | "%![iter-]+i, status=%s", | |
1007 | iterator, bt_common_func_status_string(status)); | |
1008 | goto end; | |
1009 | } | |
1010 | ||
1011 | BT_ASSERT_POST(*can_seek == BT_TRUE || *can_seek == BT_FALSE, | |
f2fb1b32 SM |
1012 | "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i", |
1013 | *can_seek, iterator); | |
1014 | ||
c0e46a7c SM |
1015 | BT_LIB_LOGD( |
1016 | "User's \"can seek nanoseconds from origin\" returned successfully: " | |
1017 | "%![iter-]+i, can-seek=%d", | |
1018 | iterator, *can_seek); | |
1019 | ||
1020 | if (*can_seek) { | |
1021 | goto end; | |
1022 | } | |
7474e7d3 PP |
1023 | } |
1024 | ||
1025 | /* | |
c0e46a7c SM |
1026 | * Automatic seeking fall back: if we can seek to the beginning and the |
1027 | * iterator supports forward seeking then we can automatically seek to | |
1028 | * any timestamp. | |
7474e7d3 | 1029 | */ |
f2fb1b32 SM |
1030 | status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning( |
1031 | iterator, can_seek); | |
c0e46a7c SM |
1032 | if (status != BT_FUNC_STATUS_OK) { |
1033 | goto end; | |
1034 | } | |
1035 | ||
1036 | *can_seek = *can_seek && iterator->config.can_seek_forward; | |
7474e7d3 PP |
1037 | |
1038 | end: | |
f2fb1b32 | 1039 | return status; |
7474e7d3 PP |
1040 | } |
1041 | ||
f2fb1b32 SM |
1042 | enum bt_message_iterator_can_seek_beginning_status |
1043 | bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1044 | struct bt_self_component_port_input_message_iterator *iterator, | |
1045 | bt_bool *can_seek) | |
7474e7d3 | 1046 | { |
f2fb1b32 | 1047 | enum bt_message_iterator_can_seek_beginning_status status; |
7474e7d3 PP |
1048 | |
1049 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
f2fb1b32 | 1050 | BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)"); |
7474e7d3 | 1051 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
5badd463 PP |
1052 | BT_ASSERT_PRE( |
1053 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1054 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1055 | "Graph is not configured: %!+g", |
1056 | bt_component_borrow_graph(iterator->upstream_component)); | |
1057 | ||
1058 | if (iterator->methods.can_seek_beginning) { | |
f2fb1b32 SM |
1059 | /* |
1060 | * Initialize to an invalid value, so we can post-assert that | |
1061 | * the method returned a valid value. | |
1062 | */ | |
1063 | *can_seek = -1; | |
1064 | ||
1065 | status = (int) iterator->methods.can_seek_beginning(iterator, can_seek); | |
1066 | ||
1067 | BT_ASSERT_POST( | |
1068 | status != BT_FUNC_STATUS_OK || | |
1069 | *can_seek == BT_TRUE || | |
1070 | *can_seek == BT_FALSE, | |
1071 | "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i", | |
1072 | *can_seek, iterator); | |
1073 | } else { | |
1074 | *can_seek = BT_FALSE; | |
1075 | status = BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
1076 | } |
1077 | ||
f2fb1b32 | 1078 | return status; |
7474e7d3 PP |
1079 | } |
1080 | ||
1081 | static inline | |
003e713f | 1082 | void set_iterator_state_after_seeking( |
7474e7d3 | 1083 | struct bt_self_component_port_input_message_iterator *iterator, |
d24d5663 | 1084 | int status) |
7474e7d3 PP |
1085 | { |
1086 | enum bt_self_component_port_input_message_iterator_state new_state = 0; | |
1087 | ||
1088 | /* Set iterator's state depending on seeking status */ | |
1089 | switch (status) { | |
d24d5663 | 1090 | case BT_FUNC_STATUS_OK: |
7474e7d3 PP |
1091 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; |
1092 | break; | |
d24d5663 | 1093 | case BT_FUNC_STATUS_AGAIN: |
7474e7d3 PP |
1094 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; |
1095 | break; | |
d24d5663 PP |
1096 | case BT_FUNC_STATUS_ERROR: |
1097 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
7474e7d3 PP |
1098 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; |
1099 | break; | |
d24d5663 | 1100 | case BT_FUNC_STATUS_END: |
7474e7d3 PP |
1101 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; |
1102 | break; | |
1103 | default: | |
498e7994 | 1104 | bt_common_abort(); |
7474e7d3 PP |
1105 | } |
1106 | ||
1107 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); | |
1108 | } | |
1109 | ||
54b135a0 SM |
1110 | static |
1111 | void reset_iterator_expectations( | |
1112 | struct bt_self_component_port_input_message_iterator *iterator) | |
1113 | { | |
1114 | iterator->last_ns_from_origin = INT64_MIN; | |
1115 | iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET; | |
1116 | } | |
1117 | ||
f2fb1b32 SM |
1118 | static |
1119 | bool message_iterator_can_seek_beginning( | |
1120 | struct bt_self_component_port_input_message_iterator *iterator) | |
1121 | { | |
1122 | enum bt_message_iterator_can_seek_beginning_status status; | |
1123 | bt_bool can_seek; | |
1124 | ||
1125 | status = bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1126 | iterator, &can_seek); | |
1127 | if (status != BT_FUNC_STATUS_OK) { | |
1128 | can_seek = BT_FALSE; | |
1129 | } | |
1130 | ||
1131 | return can_seek; | |
1132 | } | |
1133 | ||
d24d5663 | 1134 | enum bt_message_iterator_seek_beginning_status |
7474e7d3 PP |
1135 | bt_self_component_port_input_message_iterator_seek_beginning( |
1136 | struct bt_self_component_port_input_message_iterator *iterator) | |
1137 | { | |
9275bef4 | 1138 | int status; |
7474e7d3 PP |
1139 | |
1140 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1141 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1142 | BT_ASSERT_PRE( |
1143 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1144 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1145 | "Graph is not configured: %!+g", |
1146 | bt_component_borrow_graph(iterator->upstream_component)); | |
f2fb1b32 | 1147 | BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator), |
7474e7d3 | 1148 | "Message iterator cannot seek beginning: %!+i", iterator); |
54b135a0 SM |
1149 | |
1150 | /* | |
1151 | * We are seeking, reset our expectations about how the following | |
1152 | * messages should look like. | |
1153 | */ | |
1154 | reset_iterator_expectations(iterator); | |
1155 | ||
7474e7d3 PP |
1156 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); |
1157 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1158 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
1159 | status = iterator->methods.seek_beginning(iterator); | |
1160 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1161 | bt_common_func_status_string(status)); |
1162 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1163 | status == BT_FUNC_STATUS_ERROR || | |
1164 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1165 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1166 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1167 | iterator, bt_common_func_status_string(status)); |
870631a2 PP |
1168 | if (status < 0) { |
1169 | BT_LIB_LOGW_APPEND_CAUSE( | |
1170 | "Component input port message iterator's \"seek beginning\" method failed: " | |
1171 | "%![iter-]+i, status=%s", | |
1172 | iterator, bt_common_func_status_string(status)); | |
1173 | } | |
1174 | ||
7474e7d3 PP |
1175 | set_iterator_state_after_seeking(iterator, status); |
1176 | return status; | |
1177 | } | |
1178 | ||
8d8b141d SM |
1179 | bt_bool |
1180 | bt_self_component_port_input_message_iterator_can_seek_forward( | |
1181 | bt_self_component_port_input_message_iterator *iterator) | |
1182 | { | |
1183 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1184 | ||
1185 | return iterator->config.can_seek_forward; | |
1186 | } | |
1187 | ||
5b7b55be SM |
1188 | /* |
1189 | * Structure used to record the state of a given stream during the fast-forward | |
1190 | * phase of an auto-seek. | |
1191 | */ | |
1192 | struct auto_seek_stream_state { | |
1193 | /* | |
1194 | * Value representing which step of this timeline we are at. | |
1195 | * | |
1196 | * time ---> | |
188edac1 | 1197 | * [SB] 1 [PB] 2 [PE] 1 [SE] |
5b7b55be SM |
1198 | * |
1199 | * At each point in the timeline, the messages we need to replicate are: | |
1200 | * | |
1201 | * 1: Stream beginning | |
188edac1 | 1202 | * 2: Stream beginning, packet beginning |
5b7b55be SM |
1203 | * |
1204 | * Before "Stream beginning" and after "Stream end", we don't need to | |
1205 | * replicate anything as the stream doesn't exist. | |
1206 | */ | |
1207 | enum { | |
1208 | AUTO_SEEK_STREAM_STATE_STREAM_BEGAN, | |
5b7b55be SM |
1209 | AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, |
1210 | } state; | |
1211 | ||
1212 | /* | |
1213 | * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are | |
1214 | * in. This is a weak reference, since the packet will always be | |
1215 | * alive by the time we use it. | |
1216 | */ | |
1217 | struct bt_packet *packet; | |
188edac1 SM |
1218 | |
1219 | /* Have we see a message with a clock snapshot yet? */ | |
1220 | bool seen_clock_snapshot; | |
5b7b55be SM |
1221 | }; |
1222 | ||
1223 | static | |
1224 | struct auto_seek_stream_state *create_auto_seek_stream_state(void) | |
1225 | { | |
1226 | return g_new0(struct auto_seek_stream_state, 1); | |
1227 | } | |
1228 | ||
1229 | static | |
1230 | void destroy_auto_seek_stream_state(void *ptr) | |
1231 | { | |
1232 | g_free(ptr); | |
1233 | } | |
1234 | ||
1235 | static | |
1236 | GHashTable *create_auto_seek_stream_states(void) | |
1237 | { | |
1238 | return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, | |
1239 | destroy_auto_seek_stream_state); | |
1240 | } | |
1241 | ||
1242 | static | |
1243 | void destroy_auto_seek_stream_states(GHashTable *stream_states) | |
1244 | { | |
1245 | g_hash_table_destroy(stream_states); | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * Handle one message while we are in the fast-forward phase of an auto-seek. | |
1250 | * | |
1251 | * Sets `*got_first` to true if the message's timestamp is greater or equal to | |
1252 | * `ns_from_origin`. In other words, if this is the first message after our | |
1253 | * seek point. | |
1254 | * | |
1255 | * `stream_states` is an hash table of `bt_stream *` (weak reference) to | |
1256 | * `struct auto_seek_stream_state` used to keep the state of each stream | |
1257 | * during the fast-forward. | |
1258 | */ | |
1259 | ||
7474e7d3 | 1260 | static inline |
d24d5663 | 1261 | int auto_seek_handle_message( |
5b9e151d PP |
1262 | struct bt_self_component_port_input_message_iterator *iterator, |
1263 | int64_t ns_from_origin, const struct bt_message *msg, | |
5b7b55be | 1264 | bool *got_first, GHashTable *stream_states) |
7474e7d3 | 1265 | { |
d24d5663 | 1266 | int status = BT_FUNC_STATUS_OK; |
5b9e151d | 1267 | int64_t msg_ns_from_origin; |
7474e7d3 | 1268 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
5b9e151d PP |
1269 | int ret; |
1270 | ||
98b15851 PP |
1271 | BT_ASSERT_DBG(msg); |
1272 | BT_ASSERT_DBG(got_first); | |
7474e7d3 PP |
1273 | |
1274 | switch (msg->type) { | |
1275 | case BT_MESSAGE_TYPE_EVENT: | |
1276 | { | |
1277 | const struct bt_message_event *event_msg = | |
1278 | (const void *) msg; | |
1279 | ||
2c091c04 | 1280 | clk_snapshot = event_msg->default_cs; |
bdb288b3 | 1281 | BT_ASSERT_POST_DEV(clk_snapshot, |
c7072d5a PP |
1282 | "Event message has no default clock snapshot: %!+n", |
1283 | event_msg); | |
7474e7d3 PP |
1284 | break; |
1285 | } | |
b9fd9cbb | 1286 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
7474e7d3 | 1287 | { |
b9fd9cbb | 1288 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
7474e7d3 PP |
1289 | (const void *) msg; |
1290 | ||
7474e7d3 | 1291 | clk_snapshot = inactivity_msg->default_cs; |
98b15851 | 1292 | BT_ASSERT_DBG(clk_snapshot); |
7474e7d3 PP |
1293 | break; |
1294 | } | |
16663a5e PP |
1295 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
1296 | case BT_MESSAGE_TYPE_PACKET_END: | |
c7072d5a PP |
1297 | { |
1298 | const struct bt_message_packet *packet_msg = | |
1299 | (const void *) msg; | |
1300 | ||
1301 | clk_snapshot = packet_msg->default_cs; | |
bdb288b3 | 1302 | BT_ASSERT_POST_DEV(clk_snapshot, |
c7072d5a PP |
1303 | "Packet message has no default clock snapshot: %!+n", |
1304 | packet_msg); | |
1305 | break; | |
1306 | } | |
16663a5e PP |
1307 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1308 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
7474e7d3 | 1309 | { |
5b9e151d PP |
1310 | struct bt_message_discarded_items *msg_disc_items = |
1311 | (void *) msg; | |
1312 | ||
bdb288b3 | 1313 | BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs && |
5b9e151d PP |
1314 | msg_disc_items->default_end_cs, |
1315 | "Discarded events/packets message has no default clock snapshots: %!+n", | |
1316 | msg_disc_items); | |
1317 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1318 | msg_disc_items->default_begin_cs, | |
1319 | &msg_ns_from_origin); | |
1320 | if (ret) { | |
d24d5663 | 1321 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1322 | goto end; |
1323 | } | |
7474e7d3 | 1324 | |
5b9e151d PP |
1325 | if (msg_ns_from_origin >= ns_from_origin) { |
1326 | *got_first = true; | |
1327 | goto push_msg; | |
1328 | } | |
1329 | ||
1330 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1331 | msg_disc_items->default_end_cs, | |
1332 | &msg_ns_from_origin); | |
1333 | if (ret) { | |
d24d5663 | 1334 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1335 | goto end; |
1336 | } | |
1337 | ||
1338 | if (msg_ns_from_origin >= ns_from_origin) { | |
1339 | /* | |
1340 | * The discarded items message's beginning time | |
1341 | * is before the requested seeking time, but its | |
1342 | * end time is after. Modify the message so as | |
1343 | * to set its beginning time to the requested | |
1344 | * seeking time, and make its item count unknown | |
1345 | * as we don't know if items were really | |
1346 | * discarded within the new time range. | |
1347 | */ | |
4af85094 | 1348 | uint64_t new_begin_raw_value = 0; |
5b9e151d PP |
1349 | |
1350 | ret = bt_clock_class_clock_value_from_ns_from_origin( | |
1351 | msg_disc_items->default_end_cs->clock_class, | |
1352 | ns_from_origin, &new_begin_raw_value); | |
1353 | if (ret) { | |
d24d5663 | 1354 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1355 | goto end; |
1356 | } | |
1357 | ||
1358 | bt_clock_snapshot_set_raw_value( | |
1359 | msg_disc_items->default_begin_cs, | |
1360 | new_begin_raw_value); | |
1361 | msg_disc_items->count.base.avail = | |
1362 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; | |
1363 | ||
1364 | /* | |
1365 | * It is safe to push it because its beginning | |
1366 | * time is exactly the requested seeking time. | |
1367 | */ | |
1368 | goto push_msg; | |
1369 | } else { | |
1370 | goto skip_msg; | |
1371 | } | |
7474e7d3 | 1372 | } |
188edac1 SM |
1373 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
1374 | case BT_MESSAGE_TYPE_STREAM_END: | |
7474e7d3 | 1375 | { |
188edac1 SM |
1376 | struct bt_message_stream *stream_msg = |
1377 | (struct bt_message_stream *) msg; | |
7474e7d3 | 1378 | |
188edac1 SM |
1379 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
1380 | /* Ignore */ | |
5b9e151d | 1381 | goto skip_msg; |
16663a5e PP |
1382 | } |
1383 | ||
188edac1 | 1384 | clk_snapshot = stream_msg->default_cs; |
7474e7d3 PP |
1385 | break; |
1386 | } | |
1387 | default: | |
498e7994 | 1388 | bt_common_abort(); |
7474e7d3 PP |
1389 | } |
1390 | ||
98b15851 | 1391 | BT_ASSERT_DBG(clk_snapshot); |
5b9e151d PP |
1392 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, |
1393 | &msg_ns_from_origin); | |
1394 | if (ret) { | |
d24d5663 | 1395 | status = BT_FUNC_STATUS_ERROR; |
7474e7d3 PP |
1396 | goto end; |
1397 | } | |
1398 | ||
5b9e151d PP |
1399 | if (msg_ns_from_origin >= ns_from_origin) { |
1400 | *got_first = true; | |
1401 | goto push_msg; | |
1402 | } | |
1403 | ||
1404 | skip_msg: | |
5b7b55be SM |
1405 | /* This message won't be sent downstream. */ |
1406 | switch (msg->type) { | |
1407 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1408 | { | |
1409 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1410 | struct auto_seek_stream_state *stream_state; | |
5b7b55be SM |
1411 | |
1412 | /* Update stream's state: stream began. */ | |
1413 | stream_state = create_auto_seek_stream_state(); | |
1414 | if (!stream_state) { | |
d24d5663 | 1415 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1416 | goto end; |
1417 | } | |
1418 | ||
1419 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; | |
e74dbb33 | 1420 | |
188edac1 SM |
1421 | if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
1422 | stream_state->seen_clock_snapshot = true; | |
1423 | } | |
1424 | ||
98b15851 | 1425 | BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream)); |
e74dbb33 | 1426 | g_hash_table_insert(stream_states, stream_msg->stream, stream_state); |
5b7b55be SM |
1427 | break; |
1428 | } | |
188edac1 | 1429 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
5b7b55be | 1430 | { |
188edac1 | 1431 | const struct bt_message_packet *packet_msg = |
5b7b55be SM |
1432 | (const void *) msg; |
1433 | struct auto_seek_stream_state *stream_state; | |
1434 | ||
188edac1 SM |
1435 | /* Update stream's state: packet began. */ |
1436 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
98b15851 PP |
1437 | BT_ASSERT_DBG(stream_state); |
1438 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
188edac1 | 1439 | stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN; |
98b15851 | 1440 | BT_ASSERT_DBG(!stream_state->packet); |
188edac1 SM |
1441 | stream_state->packet = packet_msg->packet; |
1442 | ||
1443 | if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) { | |
1444 | stream_state->seen_clock_snapshot = true; | |
1445 | } | |
1446 | ||
5b7b55be SM |
1447 | break; |
1448 | } | |
188edac1 | 1449 | case BT_MESSAGE_TYPE_EVENT: |
5b7b55be | 1450 | { |
188edac1 | 1451 | const struct bt_message_event *event_msg = (const void *) msg; |
5b7b55be SM |
1452 | struct auto_seek_stream_state *stream_state; |
1453 | ||
188edac1 SM |
1454 | stream_state = g_hash_table_lookup(stream_states, |
1455 | event_msg->event->packet->stream); | |
98b15851 | 1456 | BT_ASSERT_DBG(stream_state); |
5b7b55be | 1457 | |
188edac1 SM |
1458 | // HELPME: are we sure that event messages have clock snapshots at this point? |
1459 | stream_state->seen_clock_snapshot = true; | |
1460 | ||
5b7b55be SM |
1461 | break; |
1462 | } | |
1463 | case BT_MESSAGE_TYPE_PACKET_END: | |
1464 | { | |
1465 | const struct bt_message_packet *packet_msg = | |
1466 | (const void *) msg; | |
1467 | struct auto_seek_stream_state *stream_state; | |
1468 | ||
1469 | /* Update stream's state: packet ended. */ | |
1470 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
98b15851 PP |
1471 | BT_ASSERT_DBG(stream_state); |
1472 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN); | |
188edac1 | 1473 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; |
98b15851 | 1474 | BT_ASSERT_DBG(stream_state->packet); |
5b7b55be | 1475 | stream_state->packet = NULL; |
5b7b55be | 1476 | |
188edac1 SM |
1477 | if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) { |
1478 | stream_state->seen_clock_snapshot = true; | |
1479 | } | |
5b7b55be | 1480 | |
5b7b55be SM |
1481 | break; |
1482 | } | |
1483 | case BT_MESSAGE_TYPE_STREAM_END: | |
1484 | { | |
1485 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1486 | struct auto_seek_stream_state *stream_state; | |
1487 | ||
1488 | stream_state = g_hash_table_lookup(stream_states, stream_msg->stream); | |
98b15851 PP |
1489 | BT_ASSERT_DBG(stream_state); |
1490 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
1491 | BT_ASSERT_DBG(!stream_state->packet); | |
5b7b55be SM |
1492 | |
1493 | /* Update stream's state: this stream doesn't exist anymore. */ | |
1494 | g_hash_table_remove(stream_states, stream_msg->stream); | |
1495 | break; | |
1496 | } | |
188edac1 SM |
1497 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1498 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1499 | { | |
1500 | const struct bt_message_discarded_items *discarded_msg = | |
1501 | (const void *) msg; | |
1502 | struct auto_seek_stream_state *stream_state; | |
1503 | ||
1504 | stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream); | |
98b15851 | 1505 | BT_ASSERT_DBG(stream_state); |
188edac1 SM |
1506 | |
1507 | if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) || | |
1508 | (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) { | |
1509 | stream_state->seen_clock_snapshot = true; | |
1510 | } | |
1511 | ||
1512 | break; | |
1513 | } | |
5b7b55be SM |
1514 | default: |
1515 | break; | |
1516 | } | |
1517 | ||
6871026b | 1518 | bt_object_put_ref_no_null_check(msg); |
04c0cec6 | 1519 | msg = NULL; |
5b9e151d PP |
1520 | goto end; |
1521 | ||
1522 | push_msg: | |
da9c4c52 | 1523 | g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg); |
5b9e151d | 1524 | msg = NULL; |
7474e7d3 PP |
1525 | |
1526 | end: | |
98b15851 | 1527 | BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK); |
5b9e151d | 1528 | return status; |
7474e7d3 PP |
1529 | } |
1530 | ||
1531 | static | |
d24d5663 | 1532 | int find_message_ge_ns_from_origin( |
7474e7d3 | 1533 | struct bt_self_component_port_input_message_iterator *iterator, |
5b7b55be | 1534 | int64_t ns_from_origin, GHashTable *stream_states) |
7474e7d3 | 1535 | { |
c70a96a6 | 1536 | int status = BT_FUNC_STATUS_OK; |
7474e7d3 PP |
1537 | enum bt_self_component_port_input_message_iterator_state init_state = |
1538 | iterator->state; | |
1539 | const struct bt_message *messages[MSG_BATCH_SIZE]; | |
1540 | uint64_t user_count = 0; | |
1541 | uint64_t i; | |
5b9e151d | 1542 | bool got_first = false; |
7474e7d3 | 1543 | |
98b15851 | 1544 | BT_ASSERT_DBG(iterator); |
7474e7d3 PP |
1545 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); |
1546 | ||
1547 | /* | |
1548 | * Make this iterator temporarily active (not seeking) to call | |
1549 | * the "next" method. | |
1550 | */ | |
1551 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1552 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1553 | ||
98b15851 | 1554 | BT_ASSERT_DBG(iterator->methods.next); |
7474e7d3 | 1555 | |
e0dade92 | 1556 | while (!got_first) { |
7474e7d3 PP |
1557 | /* |
1558 | * Call the user's "next" method to get the next | |
1559 | * messages and status. | |
1560 | */ | |
54b135a0 | 1561 | status = call_iterator_next_method(iterator, |
7474e7d3 | 1562 | &messages[0], MSG_BATCH_SIZE, &user_count); |
870631a2 PP |
1563 | BT_LOGD("User method returned: status=%s", |
1564 | bt_common_func_status_string(status)); | |
1565 | if (status < 0) { | |
1566 | BT_LIB_LOGW_APPEND_CAUSE( | |
1567 | "Component input port message iterator's \"next\" method failed: " | |
1568 | "%![iter-]+i, status=%s", | |
1569 | iterator, bt_common_func_status_string(status)); | |
1570 | } | |
7474e7d3 | 1571 | |
7474e7d3 PP |
1572 | /* |
1573 | * The user's "next" method must not do any action which | |
1574 | * would change the iterator's state. | |
1575 | */ | |
98b15851 | 1576 | BT_ASSERT_DBG(iterator->state == |
7474e7d3 | 1577 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
7474e7d3 PP |
1578 | |
1579 | switch (status) { | |
d24d5663 | 1580 | case BT_FUNC_STATUS_OK: |
bdb288b3 | 1581 | BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE, |
7474e7d3 PP |
1582 | "Invalid returned message count: greater than " |
1583 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
1584 | user_count, MSG_BATCH_SIZE); | |
1585 | break; | |
d24d5663 PP |
1586 | case BT_FUNC_STATUS_AGAIN: |
1587 | case BT_FUNC_STATUS_ERROR: | |
1588 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1589 | case BT_FUNC_STATUS_END: | |
7474e7d3 PP |
1590 | goto end; |
1591 | default: | |
498e7994 | 1592 | bt_common_abort(); |
7474e7d3 PP |
1593 | } |
1594 | ||
7474e7d3 | 1595 | for (i = 0; i < user_count; i++) { |
5b9e151d | 1596 | if (got_first) { |
da9c4c52 | 1597 | g_queue_push_tail(iterator->auto_seek.msgs, |
5b9e151d PP |
1598 | (void *) messages[i]); |
1599 | messages[i] = NULL; | |
7474e7d3 PP |
1600 | continue; |
1601 | } | |
1602 | ||
5b9e151d | 1603 | status = auto_seek_handle_message(iterator, |
5b7b55be SM |
1604 | ns_from_origin, messages[i], &got_first, |
1605 | stream_states); | |
d24d5663 | 1606 | if (status == BT_FUNC_STATUS_OK) { |
e0dade92 | 1607 | /* Message was either pushed or moved */ |
5b9e151d PP |
1608 | messages[i] = NULL; |
1609 | } else { | |
7474e7d3 PP |
1610 | goto end; |
1611 | } | |
7474e7d3 PP |
1612 | } |
1613 | } | |
1614 | ||
1615 | end: | |
1616 | for (i = 0; i < user_count; i++) { | |
1617 | if (messages[i]) { | |
6871026b | 1618 | bt_object_put_ref_no_null_check(messages[i]); |
7474e7d3 PP |
1619 | } |
1620 | } | |
1621 | ||
1622 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); | |
1623 | return status; | |
1624 | } | |
1625 | ||
5b7b55be SM |
1626 | /* |
1627 | * This function is installed as the iterator's next callback after we have | |
1628 | * auto-seeked (seeked to the beginning and fast-forwarded) to send the | |
1629 | * messages saved in iterator->auto_seek.msgs. Once this is done, the original | |
1630 | * next callback is put back. | |
1631 | */ | |
1632 | ||
7474e7d3 | 1633 | static |
d24d5663 | 1634 | enum bt_component_class_message_iterator_next_method_status post_auto_seek_next( |
7474e7d3 PP |
1635 | struct bt_self_component_port_input_message_iterator *iterator, |
1636 | bt_message_array_const msgs, uint64_t capacity, | |
1637 | uint64_t *count) | |
1638 | { | |
da9c4c52 | 1639 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs)); |
5b9e151d | 1640 | *count = 0; |
7474e7d3 PP |
1641 | |
1642 | /* | |
1643 | * Move auto-seek messages to the output array (which is this | |
5b9e151d | 1644 | * iterator's base message array). |
7474e7d3 | 1645 | */ |
da9c4c52 SM |
1646 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) { |
1647 | msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs); | |
5b9e151d PP |
1648 | capacity--; |
1649 | (*count)++; | |
7474e7d3 | 1650 | } |
7474e7d3 | 1651 | |
5b9e151d PP |
1652 | BT_ASSERT(*count > 0); |
1653 | ||
da9c4c52 | 1654 | if (g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1655 | /* No more auto-seek messages, restore user's next callback. */ |
1656 | BT_ASSERT(iterator->auto_seek.original_next_callback); | |
1657 | iterator->methods.next = iterator->auto_seek.original_next_callback; | |
1658 | iterator->auto_seek.original_next_callback = NULL; | |
7474e7d3 PP |
1659 | } |
1660 | ||
d24d5663 | 1661 | return BT_FUNC_STATUS_OK; |
7474e7d3 PP |
1662 | } |
1663 | ||
5b7b55be SM |
1664 | static inline |
1665 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, | |
1666 | int64_t ns_from_origin, uint64_t *raw_value) | |
1667 | { | |
1668 | ||
1669 | int64_t cc_offset_s = clock_class->offset_seconds; | |
1670 | uint64_t cc_offset_cycles = clock_class->offset_cycles; | |
1671 | uint64_t cc_freq = clock_class->frequency; | |
1672 | ||
1673 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, | |
1674 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); | |
1675 | } | |
1676 | ||
f2fb1b32 SM |
1677 | static |
1678 | bool message_iterator_can_seek_ns_from_origin( | |
1679 | struct bt_self_component_port_input_message_iterator *iterator, | |
1680 | int64_t ns_from_origin) | |
1681 | { | |
1682 | enum bt_message_iterator_can_seek_ns_from_origin_status status; | |
1683 | bt_bool can_seek; | |
1684 | ||
1685 | status = bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1686 | iterator, ns_from_origin, &can_seek); | |
1687 | if (status != BT_FUNC_STATUS_OK) { | |
1688 | can_seek = BT_FALSE; | |
1689 | } | |
1690 | ||
1691 | return can_seek; | |
1692 | } | |
5b7b55be | 1693 | |
d24d5663 | 1694 | enum bt_message_iterator_seek_ns_from_origin_status |
7474e7d3 PP |
1695 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
1696 | struct bt_self_component_port_input_message_iterator *iterator, | |
1697 | int64_t ns_from_origin) | |
1698 | { | |
1699 | int status; | |
5b7b55be | 1700 | GHashTable *stream_states = NULL; |
c0e46a7c | 1701 | bt_bool can_seek_by_itself; |
7474e7d3 PP |
1702 | |
1703 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1704 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1705 | BT_ASSERT_PRE( |
1706 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1707 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1708 | "Graph is not configured: %!+g", |
1709 | bt_component_borrow_graph(iterator->upstream_component)); | |
c0e46a7c | 1710 | /* The iterator must be able to seek ns from origin one way or another. */ |
7474e7d3 | 1711 | BT_ASSERT_PRE( |
f2fb1b32 | 1712 | message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin), |
7474e7d3 PP |
1713 | "Message iterator cannot seek nanoseconds from origin: %!+i, " |
1714 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); | |
1715 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1716 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
1717 | ||
54b135a0 SM |
1718 | /* |
1719 | * We are seeking, reset our expectations about how the following | |
1720 | * messages should look like. | |
1721 | */ | |
1722 | reset_iterator_expectations(iterator); | |
1723 | ||
c0e46a7c SM |
1724 | /* Check if the iterator can seek by itself. If not we'll use autoseek. */ |
1725 | if (iterator->methods.can_seek_ns_from_origin) { | |
1726 | bt_component_class_message_iterator_can_seek_ns_from_origin_method_status | |
1727 | can_seek_status; | |
1728 | ||
1729 | can_seek_status = | |
1730 | iterator->methods.can_seek_ns_from_origin( | |
1731 | iterator, ns_from_origin, &can_seek_by_itself); | |
1732 | if (can_seek_status != BT_FUNC_STATUS_OK) { | |
1733 | status = can_seek_status; | |
1734 | goto end; | |
1735 | } | |
1736 | } else { | |
1737 | can_seek_by_itself = false; | |
1738 | } | |
1739 | ||
1740 | if (can_seek_by_itself) { | |
5b7b55be | 1741 | /* The iterator knows how to seek to a particular time, let it handle this. */ |
2e1b5615 | 1742 | BT_ASSERT(iterator->methods.seek_ns_from_origin); |
7474e7d3 PP |
1743 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " |
1744 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); | |
1745 | status = iterator->methods.seek_ns_from_origin(iterator, | |
1746 | ns_from_origin); | |
1747 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1748 | bt_common_func_status_string(status)); |
1749 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1750 | status == BT_FUNC_STATUS_ERROR || | |
1751 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1752 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1753 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1754 | iterator, bt_common_func_status_string(status)); |
870631a2 PP |
1755 | if (status < 0) { |
1756 | BT_LIB_LOGW_APPEND_CAUSE( | |
1757 | "Component input port message iterator's \"seek nanoseconds from origin\" method failed: " | |
1758 | "%![iter-]+i, status=%s", | |
1759 | iterator, bt_common_func_status_string(status)); | |
1760 | } | |
7474e7d3 | 1761 | } else { |
5b7b55be | 1762 | /* |
c0e46a7c SM |
1763 | * The iterator doesn't know how to seek by itself to a |
1764 | * particular time. We will seek to the beginning and fast | |
1765 | * forward to the right place. | |
5b7b55be | 1766 | */ |
f2fb1b32 SM |
1767 | enum bt_component_class_message_iterator_can_seek_beginning_method_status |
1768 | can_seek_status; | |
1769 | bt_bool can_seek_beginning; | |
1770 | ||
1771 | can_seek_status = iterator->methods.can_seek_beginning(iterator, | |
1772 | &can_seek_beginning); | |
1773 | BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK); | |
1774 | BT_ASSERT(can_seek_beginning); | |
7474e7d3 PP |
1775 | BT_ASSERT(iterator->methods.seek_beginning); |
1776 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", | |
1777 | iterator); | |
1778 | status = iterator->methods.seek_beginning(iterator); | |
1779 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1780 | bt_common_func_status_string(status)); |
1781 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1782 | status == BT_FUNC_STATUS_ERROR || | |
1783 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1784 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1785 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1786 | iterator, bt_common_func_status_string(status)); |
870631a2 PP |
1787 | if (status < 0) { |
1788 | BT_LIB_LOGW_APPEND_CAUSE( | |
1789 | "Component input port message iterator's \"seek beginning\" method failed: " | |
1790 | "%![iter-]+i, status=%s", | |
1791 | iterator, bt_common_func_status_string(status)); | |
1792 | } | |
1793 | ||
7474e7d3 | 1794 | switch (status) { |
d24d5663 | 1795 | case BT_FUNC_STATUS_OK: |
7474e7d3 | 1796 | break; |
d24d5663 PP |
1797 | case BT_FUNC_STATUS_ERROR: |
1798 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1799 | case BT_FUNC_STATUS_AGAIN: | |
7474e7d3 PP |
1800 | goto end; |
1801 | default: | |
498e7994 | 1802 | bt_common_abort(); |
7474e7d3 PP |
1803 | } |
1804 | ||
1805 | /* | |
1806 | * Find the first message which has a default clock | |
1807 | * snapshot greater than or equal to the requested | |
5b9e151d PP |
1808 | * seeking time, and move the received messages from |
1809 | * this point in the batch to this iterator's auto-seek | |
1810 | * message queue. | |
7474e7d3 | 1811 | */ |
da9c4c52 | 1812 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
6871026b | 1813 | bt_object_put_ref_no_null_check( |
da9c4c52 | 1814 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
5b9e151d PP |
1815 | } |
1816 | ||
5b7b55be SM |
1817 | stream_states = create_auto_seek_stream_states(); |
1818 | if (!stream_states) { | |
870631a2 PP |
1819 | BT_LIB_LOGE_APPEND_CAUSE( |
1820 | "Failed to allocate one GHashTable."); | |
d24d5663 | 1821 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1822 | goto end; |
1823 | } | |
1824 | ||
7474e7d3 | 1825 | status = find_message_ge_ns_from_origin(iterator, |
5b7b55be | 1826 | ns_from_origin, stream_states); |
7474e7d3 | 1827 | switch (status) { |
d24d5663 PP |
1828 | case BT_FUNC_STATUS_OK: |
1829 | case BT_FUNC_STATUS_END: | |
5b7b55be SM |
1830 | { |
1831 | GHashTableIter iter; | |
1832 | gpointer key, value; | |
1833 | ||
1834 | /* | |
1835 | * If some streams exist at the seek time, prepend the | |
1836 | * required messages to put those streams in the right | |
1837 | * state. | |
1838 | */ | |
1839 | g_hash_table_iter_init(&iter, stream_states); | |
1840 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
1841 | const bt_stream *stream = key; | |
1842 | struct auto_seek_stream_state *stream_state = | |
1843 | (struct auto_seek_stream_state *) value; | |
1844 | bt_message *msg; | |
1845 | const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const( | |
1846 | bt_stream_borrow_class_const(stream)); | |
188edac1 SM |
1847 | /* Initialize to silence maybe-uninitialized warning. */ |
1848 | uint64_t raw_value = 0; | |
1849 | ||
1850 | /* | |
1851 | * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within | |
1852 | * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value. | |
1853 | * | |
1854 | * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its | |
1855 | * clock snapshot, because we don't really know if the stream existed at that time. If we have | |
1856 | * seen a message with a clock snapshot in our seeking, then we are sure that the | |
1857 | * seek time is not below the clock range, and we know the stream was active at that | |
1858 | * time (and that we cut it short). | |
1859 | */ | |
1860 | if (stream_state->seen_clock_snapshot) { | |
1861 | if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) { | |
1862 | BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K", | |
1863 | ns_from_origin, clock_class); | |
1864 | status = BT_FUNC_STATUS_ERROR; | |
1865 | goto end; | |
1866 | } | |
5b7b55be SM |
1867 | } |
1868 | ||
1869 | switch (stream_state->state) { | |
1870 | case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN: | |
1871 | BT_ASSERT(stream_state->packet); | |
1872 | BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet); | |
188edac1 SM |
1873 | |
1874 | if (stream->class->packets_have_beginning_default_clock_snapshot) { | |
1875 | /* | |
1876 | * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning" | |
1877 | * message. If "packet beginning" packets have clock snapshots, then we must have | |
1878 | * seen a clock snapshot. | |
1879 | */ | |
1880 | BT_ASSERT(stream_state->seen_clock_snapshot); | |
1881 | ||
1882 | msg = bt_message_packet_beginning_create_with_default_clock_snapshot( | |
1883 | (bt_self_message_iterator *) iterator, stream_state->packet, raw_value); | |
1884 | } else { | |
1885 | msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator, | |
1886 | stream_state->packet); | |
5b7b55be SM |
1887 | } |
1888 | ||
5b7b55be | 1889 | if (!msg) { |
d24d5663 | 1890 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1891 | goto end; |
1892 | } | |
1893 | ||
5b7b55be SM |
1894 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
1895 | msg = NULL; | |
1896 | /* fall-thru */ | |
188edac1 | 1897 | |
5b7b55be SM |
1898 | case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN: |
1899 | msg = bt_message_stream_beginning_create( | |
1900 | (bt_self_message_iterator *) iterator, stream); | |
1901 | if (!msg) { | |
d24d5663 | 1902 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1903 | goto end; |
1904 | } | |
1905 | ||
188edac1 SM |
1906 | if (stream_state->seen_clock_snapshot) { |
1907 | bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value); | |
1908 | } | |
1909 | ||
5b7b55be SM |
1910 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
1911 | msg = NULL; | |
1912 | break; | |
1913 | } | |
1914 | } | |
1915 | ||
7474e7d3 | 1916 | /* |
5b9e151d PP |
1917 | * If there are messages in the auto-seek |
1918 | * message queue, replace the user's "next" | |
1919 | * method with a custom, temporary "next" method | |
1920 | * which returns them. | |
7474e7d3 | 1921 | */ |
da9c4c52 | 1922 | if (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1923 | BT_ASSERT(!iterator->auto_seek.original_next_callback); |
1924 | iterator->auto_seek.original_next_callback = iterator->methods.next; | |
1925 | ||
5b9e151d PP |
1926 | iterator->methods.next = |
1927 | (bt_self_component_port_input_message_iterator_next_method) | |
1928 | post_auto_seek_next; | |
1929 | } | |
1930 | ||
1931 | /* | |
d24d5663 PP |
1932 | * `BT_FUNC_STATUS_END` becomes |
1933 | * `BT_FUNC_STATUS_OK`: the next | |
5b9e151d PP |
1934 | * time this iterator's "next" method is called, |
1935 | * it will return | |
d24d5663 | 1936 | * `BT_FUNC_STATUS_END`. |
5b9e151d | 1937 | */ |
d24d5663 | 1938 | status = BT_FUNC_STATUS_OK; |
7474e7d3 | 1939 | break; |
5b7b55be | 1940 | } |
d24d5663 PP |
1941 | case BT_FUNC_STATUS_ERROR: |
1942 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1943 | case BT_FUNC_STATUS_AGAIN: | |
7474e7d3 | 1944 | goto end; |
7474e7d3 | 1945 | default: |
498e7994 | 1946 | bt_common_abort(); |
7474e7d3 PP |
1947 | } |
1948 | } | |
1949 | ||
54b135a0 SM |
1950 | /* |
1951 | * The following messages returned by the next method (including | |
1952 | * post_auto_seek_next) must be after (or at) `ns_from_origin`. | |
1953 | */ | |
1954 | iterator->last_ns_from_origin = ns_from_origin; | |
1955 | ||
7474e7d3 | 1956 | end: |
5b7b55be SM |
1957 | if (stream_states) { |
1958 | destroy_auto_seek_stream_states(stream_states); | |
1959 | stream_states = NULL; | |
1960 | } | |
870631a2 | 1961 | |
7474e7d3 | 1962 | set_iterator_state_after_seeking(iterator, status); |
7474e7d3 PP |
1963 | return status; |
1964 | } | |
1965 | ||
9b4f9b42 PP |
1966 | bt_bool bt_self_message_iterator_is_interrupted( |
1967 | const struct bt_self_message_iterator *self_msg_iter) | |
1968 | { | |
1969 | const struct bt_self_component_port_input_message_iterator *iterator = | |
1970 | (const void *) self_msg_iter; | |
1971 | ||
1972 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1973 | return (bt_bool) bt_graph_is_interrupted(iterator->graph); | |
1974 | } | |
1975 | ||
d6e69534 PP |
1976 | void bt_self_component_port_input_message_iterator_get_ref( |
1977 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1978 | { |
1979 | bt_object_get_ref(iterator); | |
1980 | } | |
1981 | ||
d6e69534 PP |
1982 | void bt_self_component_port_input_message_iterator_put_ref( |
1983 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1984 | { |
1985 | bt_object_put_ref(iterator); | |
1986 | } |