Commit | Line | Data |
---|---|---|
958f7d11 PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
fed72692 PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
24 | #include "logging.h" | |
25 | ||
3fadfbc0 MJ |
26 | #include <babeltrace2/babeltrace-internal.h> |
27 | #include <babeltrace2/compat/uuid-internal.h> | |
28 | #include <babeltrace2/babeltrace.h> | |
29 | #include <babeltrace2/value-internal.h> | |
30 | #include <babeltrace2/graph/component-internal.h> | |
31 | #include <babeltrace2/graph/message-iterator-internal.h> | |
32 | #include <babeltrace2/graph/connection-internal.h> | |
958f7d11 PP |
33 | #include <plugins-common.h> |
34 | #include <glib.h> | |
c55a9f58 | 35 | #include <stdbool.h> |
fed72692 | 36 | #include <inttypes.h> |
3fadfbc0 MJ |
37 | #include <babeltrace2/assert-internal.h> |
38 | #include <babeltrace2/common-internal.h> | |
0fbb9a9f | 39 | #include <stdlib.h> |
282c8cd0 | 40 | #include <string.h> |
958f7d11 | 41 | |
fdf0b89e FD |
42 | #include "muxer.h" |
43 | ||
c3acd5f3 | 44 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
65ee897d | 45 | |
958f7d11 | 46 | struct muxer_comp { |
958f7d11 | 47 | /* Weak ref */ |
b19ff26f | 48 | bt_self_component_filter *self_comp; |
d94d92ac | 49 | |
958f7d11 PP |
50 | unsigned int next_port_num; |
51 | size_t available_input_ports; | |
d6e69534 | 52 | bool initializing_muxer_msg_iter; |
282c8cd0 | 53 | bool assume_absolute_clock_classes; |
958f7d11 PP |
54 | }; |
55 | ||
d6e69534 | 56 | struct muxer_upstream_msg_iter { |
ab11110e | 57 | /* Owned by this, NULL if ended */ |
d6e69534 | 58 | bt_self_component_port_input_message_iterator *msg_iter; |
958f7d11 | 59 | |
d6e69534 PP |
60 | /* Contains `const bt_message *`, owned by this */ |
61 | GQueue *msgs; | |
958f7d11 PP |
62 | }; |
63 | ||
d6e69534 PP |
64 | enum muxer_msg_iter_clock_class_expectation { |
65 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0, | |
60f3d027 | 66 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE, |
d6e69534 PP |
67 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE, |
68 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID, | |
69 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID, | |
282c8cd0 PP |
70 | }; |
71 | ||
d6e69534 | 72 | struct muxer_msg_iter { |
ab11110e | 73 | /* |
d6e69534 | 74 | * Array of struct muxer_upstream_msg_iter * (owned by this). |
ab11110e PP |
75 | * |
76 | * NOTE: This array is searched in linearly to find the youngest | |
d6e69534 | 77 | * current message. Keep this until benchmarks confirm that |
ab11110e PP |
78 | * another data structure is faster than this for our typical |
79 | * use cases. | |
80 | */ | |
54bdc1f7 PP |
81 | GPtrArray *active_muxer_upstream_msg_iters; |
82 | ||
83 | /* | |
84 | * Array of struct muxer_upstream_msg_iter * (owned by this). | |
85 | * | |
86 | * We move ended message iterators from | |
87 | * `active_muxer_upstream_msg_iters` to this array so as to be | |
88 | * able to restore them when seeking. | |
89 | */ | |
90 | GPtrArray *ended_muxer_upstream_msg_iters; | |
958f7d11 | 91 | |
d6e69534 | 92 | /* Last time returned in a message */ |
958f7d11 | 93 | int64_t last_returned_ts_ns; |
282c8cd0 PP |
94 | |
95 | /* Clock class expectation state */ | |
d6e69534 | 96 | enum muxer_msg_iter_clock_class_expectation clock_class_expectation; |
282c8cd0 PP |
97 | |
98 | /* | |
99 | * Expected clock class UUID, only valid when | |
100 | * clock_class_expectation is | |
d6e69534 | 101 | * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. |
282c8cd0 PP |
102 | */ |
103 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; | |
958f7d11 PP |
104 | }; |
105 | ||
54bdc1f7 PP |
106 | static |
107 | void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter) | |
108 | { | |
109 | const bt_message *msg; | |
110 | ||
111 | while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) { | |
112 | bt_message_put_ref(msg); | |
113 | } | |
114 | } | |
115 | ||
ab11110e | 116 | static |
d6e69534 PP |
117 | void destroy_muxer_upstream_msg_iter( |
118 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) | |
ab11110e | 119 | { |
d6e69534 | 120 | if (!muxer_upstream_msg_iter) { |
ab11110e PP |
121 | return; |
122 | } | |
123 | ||
d6e69534 PP |
124 | BT_LOGD("Destroying muxer's upstream message iterator wrapper: " |
125 | "addr=%p, msg-iter-addr=%p, queue-len=%u", | |
126 | muxer_upstream_msg_iter, | |
127 | muxer_upstream_msg_iter->msg_iter, | |
128 | muxer_upstream_msg_iter->msgs->length); | |
54bdc1f7 PP |
129 | bt_self_component_port_input_message_iterator_put_ref( |
130 | muxer_upstream_msg_iter->msg_iter); | |
d4393e08 | 131 | |
d6e69534 | 132 | if (muxer_upstream_msg_iter->msgs) { |
54bdc1f7 | 133 | empty_message_queue(muxer_upstream_msg_iter); |
d6e69534 | 134 | g_queue_free(muxer_upstream_msg_iter->msgs); |
d4393e08 PP |
135 | } |
136 | ||
d6e69534 | 137 | g_free(muxer_upstream_msg_iter); |
ab11110e PP |
138 | } |
139 | ||
958f7d11 | 140 | static |
c61018b9 | 141 | int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter, |
d6e69534 | 142 | bt_self_component_port_input_message_iterator *self_msg_iter) |
958f7d11 | 143 | { |
c61018b9 | 144 | int ret = 0; |
d6e69534 PP |
145 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
146 | g_new0(struct muxer_upstream_msg_iter, 1); | |
958f7d11 | 147 | |
d6e69534 PP |
148 | if (!muxer_upstream_msg_iter) { |
149 | BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper."); | |
6c20f4a0 | 150 | goto error; |
958f7d11 PP |
151 | } |
152 | ||
d6e69534 PP |
153 | muxer_upstream_msg_iter->msg_iter = self_msg_iter; |
154 | bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter); | |
155 | muxer_upstream_msg_iter->msgs = g_queue_new(); | |
156 | if (!muxer_upstream_msg_iter->msgs) { | |
d4393e08 | 157 | BT_LOGE_STR("Failed to allocate a GQueue."); |
6c20f4a0 | 158 | goto error; |
d4393e08 PP |
159 | } |
160 | ||
54bdc1f7 | 161 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, |
d6e69534 PP |
162 | muxer_upstream_msg_iter); |
163 | BT_LOGD("Added muxer's upstream message iterator wrapper: " | |
164 | "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p", | |
165 | muxer_upstream_msg_iter, muxer_msg_iter, | |
166 | self_msg_iter); | |
958f7d11 | 167 | |
6c20f4a0 FD |
168 | goto end; |
169 | ||
170 | error: | |
171 | g_free(muxer_upstream_msg_iter); | |
c61018b9 | 172 | ret = -1; |
6c20f4a0 | 173 | |
958f7d11 | 174 | end: |
c61018b9 | 175 | return ret; |
958f7d11 PP |
176 | } |
177 | ||
958f7d11 | 178 | static |
5badd463 | 179 | bt_self_component_status add_available_input_port( |
b19ff26f | 180 | bt_self_component_filter *self_comp) |
958f7d11 | 181 | { |
d94d92ac | 182 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
707b7d35 | 183 | bt_self_component_filter_as_self_component(self_comp)); |
4cdfc5e8 | 184 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 | 185 | GString *port_name = NULL; |
958f7d11 | 186 | |
f6ccaed9 | 187 | BT_ASSERT(muxer_comp); |
958f7d11 PP |
188 | port_name = g_string_new("in"); |
189 | if (!port_name) { | |
fed72692 | 190 | BT_LOGE_STR("Failed to allocate a GString."); |
d94d92ac | 191 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
958f7d11 PP |
192 | goto end; |
193 | } | |
194 | ||
195 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
d94d92ac PP |
196 | status = bt_self_component_filter_add_input_port( |
197 | self_comp, port_name->str, NULL, NULL); | |
198 | if (status != BT_SELF_COMPONENT_STATUS_OK) { | |
fed72692 PP |
199 | BT_LOGE("Cannot add input port to muxer component: " |
200 | "port-name=\"%s\", comp-addr=%p, status=%s", | |
d94d92ac PP |
201 | port_name->str, self_comp, |
202 | bt_self_component_status_string(status)); | |
958f7d11 PP |
203 | goto end; |
204 | } | |
205 | ||
206 | muxer_comp->available_input_ports++; | |
207 | muxer_comp->next_port_num++; | |
fed72692 PP |
208 | BT_LOGD("Added one input port to muxer component: " |
209 | "port-name=\"%s\", comp-addr=%p", | |
d94d92ac | 210 | port_name->str, self_comp); |
5badd463 | 211 | |
958f7d11 PP |
212 | end: |
213 | if (port_name) { | |
214 | g_string_free(port_name, TRUE); | |
215 | } | |
216 | ||
147337a3 | 217 | return status; |
958f7d11 PP |
218 | } |
219 | ||
958f7d11 | 220 | static |
4cdfc5e8 | 221 | bt_self_component_status create_output_port( |
b19ff26f | 222 | bt_self_component_filter *self_comp) |
958f7d11 | 223 | { |
d94d92ac PP |
224 | return bt_self_component_filter_add_output_port( |
225 | self_comp, "out", NULL, NULL); | |
958f7d11 PP |
226 | } |
227 | ||
228 | static | |
229 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
230 | { | |
231 | if (!muxer_comp) { | |
232 | return; | |
233 | } | |
234 | ||
958f7d11 PP |
235 | g_free(muxer_comp); |
236 | } | |
237 | ||
65ee897d | 238 | static |
b19ff26f | 239 | bt_value *get_default_params(void) |
65ee897d | 240 | { |
b19ff26f | 241 | bt_value *params; |
65ee897d PP |
242 | int ret; |
243 | ||
05e21286 | 244 | params = bt_value_map_create(); |
65ee897d | 245 | if (!params) { |
fed72692 | 246 | BT_LOGE_STR("Cannot create a map value object."); |
65ee897d PP |
247 | goto error; |
248 | } | |
249 | ||
05e21286 | 250 | ret = bt_value_map_insert_bool_entry(params, |
c3acd5f3 | 251 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); |
65ee897d | 252 | if (ret) { |
fed72692 | 253 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
65ee897d PP |
254 | goto error; |
255 | } | |
256 | ||
257 | goto end; | |
258 | ||
259 | error: | |
c5b9b441 | 260 | BT_VALUE_PUT_REF_AND_RESET(params); |
65ee897d PP |
261 | |
262 | end: | |
263 | return params; | |
264 | } | |
265 | ||
266 | static | |
05e21286 | 267 | int configure_muxer_comp(struct muxer_comp *muxer_comp, |
b19ff26f | 268 | const bt_value *params) |
65ee897d | 269 | { |
b19ff26f PP |
270 | bt_value *default_params = NULL; |
271 | bt_value *real_params = NULL; | |
272 | const bt_value *assume_absolute_clock_classes = NULL; | |
65ee897d | 273 | int ret = 0; |
c55a9f58 | 274 | bt_bool bool_val; |
65ee897d PP |
275 | |
276 | default_params = get_default_params(); | |
277 | if (!default_params) { | |
fed72692 PP |
278 | BT_LOGE("Cannot get default parameters: " |
279 | "muxer-comp-addr=%p", muxer_comp); | |
65ee897d PP |
280 | goto error; |
281 | } | |
282 | ||
35294e83 | 283 | ret = bt_value_map_extend(default_params, params, &real_params); |
601b0d3c | 284 | if (ret) { |
fed72692 PP |
285 | BT_LOGE("Cannot extend default parameters map value: " |
286 | "muxer-comp-addr=%p, def-params-addr=%p, " | |
287 | "params-addr=%p", muxer_comp, default_params, | |
288 | params); | |
65ee897d PP |
289 | goto error; |
290 | } | |
291 | ||
05e21286 PP |
292 | assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params, |
293 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); | |
f6ccaed9 PP |
294 | if (assume_absolute_clock_classes && |
295 | !bt_value_is_bool(assume_absolute_clock_classes)) { | |
fed72692 PP |
296 | BT_LOGE("Expecting a boolean value for the `%s` parameter: " |
297 | "muxer-comp-addr=%p, value-type=%s", | |
298 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp, | |
da91b29a | 299 | bt_common_value_type_string( |
fed72692 | 300 | bt_value_get_type(assume_absolute_clock_classes))); |
65ee897d PP |
301 | goto error; |
302 | } | |
303 | ||
601b0d3c | 304 | bool_val = bt_value_bool_get(assume_absolute_clock_classes); |
282c8cd0 | 305 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
fed72692 PP |
306 | BT_LOGD("Configured muxer component: muxer-comp-addr=%p, " |
307 | "assume-absolute-clock-classes=%d", | |
308 | muxer_comp, muxer_comp->assume_absolute_clock_classes); | |
65ee897d PP |
309 | goto end; |
310 | ||
311 | error: | |
312 | ret = -1; | |
313 | ||
314 | end: | |
c5b9b441 PP |
315 | bt_value_put_ref(default_params); |
316 | bt_value_put_ref(real_params); | |
65ee897d PP |
317 | return ret; |
318 | } | |
319 | ||
958f7d11 | 320 | BT_HIDDEN |
4cdfc5e8 | 321 | bt_self_component_status muxer_init( |
b19ff26f | 322 | bt_self_component_filter *self_comp, |
fdf0b89e | 323 | const bt_value *params, void *init_data) |
958f7d11 PP |
324 | { |
325 | int ret; | |
4cdfc5e8 | 326 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 PP |
327 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); |
328 | ||
fed72692 | 329 | BT_LOGD("Initializing muxer component: " |
d94d92ac | 330 | "comp-addr=%p, params-addr=%p", self_comp, params); |
fed72692 | 331 | |
958f7d11 | 332 | if (!muxer_comp) { |
fed72692 | 333 | BT_LOGE_STR("Failed to allocate one muxer component."); |
958f7d11 PP |
334 | goto error; |
335 | } | |
336 | ||
65ee897d PP |
337 | ret = configure_muxer_comp(muxer_comp, params); |
338 | if (ret) { | |
fed72692 PP |
339 | BT_LOGE("Cannot configure muxer component: " |
340 | "muxer-comp-addr=%p, params-addr=%p", | |
341 | muxer_comp, params); | |
65ee897d PP |
342 | goto error; |
343 | } | |
344 | ||
d94d92ac PP |
345 | muxer_comp->self_comp = self_comp; |
346 | bt_self_component_set_data( | |
707b7d35 | 347 | bt_self_component_filter_as_self_component(self_comp), |
d94d92ac | 348 | muxer_comp); |
5badd463 | 349 | status = add_available_input_port(self_comp); |
d94d92ac | 350 | if (status != BT_SELF_COMPONENT_STATUS_OK) { |
fed72692 PP |
351 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
352 | "muxer-comp-addr=%p, status=%s", | |
353 | muxer_comp, | |
d94d92ac | 354 | bt_self_component_status_string(status)); |
958f7d11 PP |
355 | goto error; |
356 | } | |
357 | ||
d94d92ac | 358 | status = create_output_port(self_comp); |
fed72692 PP |
359 | if (status) { |
360 | BT_LOGE("Cannot create muxer component's output port: " | |
361 | "muxer-comp-addr=%p, status=%s", | |
362 | muxer_comp, | |
d94d92ac | 363 | bt_self_component_status_string(status)); |
958f7d11 PP |
364 | goto error; |
365 | } | |
366 | ||
fed72692 PP |
367 | BT_LOGD("Initialized muxer component: " |
368 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", | |
d94d92ac | 369 | self_comp, params, muxer_comp); |
fed72692 | 370 | |
958f7d11 PP |
371 | goto end; |
372 | ||
373 | error: | |
374 | destroy_muxer_comp(muxer_comp); | |
d94d92ac | 375 | bt_self_component_set_data( |
707b7d35 | 376 | bt_self_component_filter_as_self_component(self_comp), |
d94d92ac | 377 | NULL); |
147337a3 | 378 | |
d94d92ac PP |
379 | if (status == BT_SELF_COMPONENT_STATUS_OK) { |
380 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
147337a3 | 381 | } |
958f7d11 PP |
382 | |
383 | end: | |
384 | return status; | |
385 | } | |
386 | ||
387 | BT_HIDDEN | |
b19ff26f | 388 | void muxer_finalize(bt_self_component_filter *self_comp) |
958f7d11 | 389 | { |
d94d92ac | 390 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
707b7d35 | 391 | bt_self_component_filter_as_self_component(self_comp)); |
958f7d11 | 392 | |
fed72692 | 393 | BT_LOGD("Finalizing muxer component: comp-addr=%p", |
d94d92ac | 394 | self_comp); |
958f7d11 PP |
395 | destroy_muxer_comp(muxer_comp); |
396 | } | |
397 | ||
398 | static | |
d6e69534 | 399 | bt_self_component_port_input_message_iterator * |
5badd463 | 400 | create_msg_iter_on_input_port(bt_self_component_port_input *self_port) |
958f7d11 | 401 | { |
b19ff26f | 402 | const bt_port *port = bt_self_component_port_as_port( |
707b7d35 | 403 | bt_self_component_port_input_as_self_component_port( |
d94d92ac | 404 | self_port)); |
d6e69534 | 405 | bt_self_component_port_input_message_iterator *msg_iter = |
d94d92ac | 406 | NULL; |
958f7d11 | 407 | |
f6ccaed9 PP |
408 | BT_ASSERT(port); |
409 | BT_ASSERT(bt_port_is_connected(port)); | |
958f7d11 | 410 | |
ab11110e | 411 | // TODO: Advance the iterator to >= the time of the latest |
d6e69534 | 412 | // returned message by the muxer message |
ab11110e | 413 | // iterator which creates it. |
d6e69534 | 414 | msg_iter = bt_self_component_port_input_message_iterator_create( |
d94d92ac | 415 | self_port); |
d6e69534 PP |
416 | if (!msg_iter) { |
417 | BT_LOGE("Cannot create upstream message iterator on input port: " | |
d94d92ac PP |
418 | "port-addr=%p, port-name=\"%s\"", |
419 | port, bt_port_get_name(port)); | |
958f7d11 PP |
420 | goto end; |
421 | } | |
422 | ||
d6e69534 PP |
423 | BT_LOGD("Created upstream message iterator on input port: " |
424 | "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p", | |
425 | port, bt_port_get_name(port), msg_iter); | |
fed72692 | 426 | |
958f7d11 | 427 | end: |
d6e69534 | 428 | return msg_iter; |
958f7d11 PP |
429 | } |
430 | ||
ab11110e | 431 | static |
fdf0b89e | 432 | bt_self_message_iterator_status muxer_upstream_msg_iter_next( |
54bdc1f7 PP |
433 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
434 | bool *is_ended) | |
ab11110e | 435 | { |
fdf0b89e FD |
436 | bt_self_message_iterator_status status; |
437 | bt_message_iterator_status input_port_iter_status; | |
d6e69534 | 438 | bt_message_array_const msgs; |
d4393e08 PP |
439 | uint64_t i; |
440 | uint64_t count; | |
ab11110e | 441 | |
d6e69534 PP |
442 | BT_LOGV("Calling upstream message iterator's \"next\" method: " |
443 | "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p", | |
444 | muxer_upstream_msg_iter, | |
445 | muxer_upstream_msg_iter->msg_iter); | |
fdf0b89e | 446 | input_port_iter_status = bt_self_component_port_input_message_iterator_next( |
d6e69534 PP |
447 | muxer_upstream_msg_iter->msg_iter, &msgs, &count); |
448 | BT_LOGV("Upstream message iterator's \"next\" method returned: " | |
fdf0b89e | 449 | "status=%s", bt_message_iterator_status_string(input_port_iter_status)); |
ab11110e | 450 | |
fdf0b89e | 451 | switch (input_port_iter_status) { |
d6e69534 | 452 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
089717de | 453 | /* |
d6e69534 | 454 | * Message iterator's current message is |
d4393e08 | 455 | * valid: it must be considered for muxing operations. |
089717de | 456 | */ |
d6e69534 | 457 | BT_LOGV_STR("Validated upstream message iterator wrapper."); |
d4393e08 PP |
458 | BT_ASSERT(count > 0); |
459 | ||
d6e69534 | 460 | /* Move messages to our queue */ |
d4393e08 PP |
461 | for (i = 0; i < count; i++) { |
462 | /* | |
463 | * Push to tail in order; other side | |
d6e69534 | 464 | * (muxer_msg_iter_do_next_one()) consumes |
d4393e08 PP |
465 | * from the head first. |
466 | */ | |
d6e69534 PP |
467 | g_queue_push_tail(muxer_upstream_msg_iter->msgs, |
468 | (void *) msgs[i]); | |
d4393e08 | 469 | } |
fdf0b89e | 470 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
ab11110e | 471 | break; |
d6e69534 | 472 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
089717de | 473 | /* |
d6e69534 | 474 | * Message iterator's current message is not |
089717de | 475 | * valid anymore. Return |
d6e69534 | 476 | * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately. |
089717de | 477 | */ |
fdf0b89e | 478 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
ab11110e | 479 | break; |
d6e69534 | 480 | case BT_MESSAGE_ITERATOR_STATUS_END: /* Fall-through. */ |
ab11110e | 481 | /* |
d6e69534 | 482 | * Message iterator reached the end: release it. It |
ab11110e | 483 | * won't be considered again to find the youngest |
d6e69534 | 484 | * message. |
ab11110e | 485 | */ |
54bdc1f7 | 486 | *is_ended = true; |
fdf0b89e | 487 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
089717de | 488 | break; |
ab11110e PP |
489 | default: |
490 | /* Error or unsupported status code */ | |
fed72692 | 491 | BT_LOGE("Error or unsupported status code: " |
fdf0b89e FD |
492 | "status-code=%d", input_port_iter_status); |
493 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
089717de | 494 | break; |
ab11110e PP |
495 | } |
496 | ||
089717de | 497 | return status; |
ab11110e PP |
498 | } |
499 | ||
958f7d11 | 500 | static |
d6e69534 PP |
501 | int get_msg_ts_ns(struct muxer_comp *muxer_comp, |
502 | struct muxer_msg_iter *muxer_msg_iter, | |
503 | const bt_message *msg, int64_t last_returned_ts_ns, | |
958f7d11 PP |
504 | int64_t *ts_ns) |
505 | { | |
605e1019 | 506 | const bt_clock_snapshot *clock_snapshot = NULL; |
958f7d11 | 507 | int ret = 0; |
5366eb53 | 508 | bt_message_stream_activity_clock_snapshot_state sa_cs_state; |
649934d2 PP |
509 | const bt_stream_class *stream_class = NULL; |
510 | bt_message_type msg_type; | |
958f7d11 | 511 | |
d6e69534 | 512 | BT_ASSERT(msg); |
f6ccaed9 | 513 | BT_ASSERT(ts_ns); |
d6e69534 PP |
514 | BT_LOGV("Getting message's timestamp: " |
515 | "muxer-msg-iter-addr=%p, msg-addr=%p, " | |
fed72692 | 516 | "last-returned-ts=%" PRId64, |
d6e69534 | 517 | muxer_msg_iter, msg, last_returned_ts_ns); |
fed72692 | 518 | |
60f3d027 PP |
519 | if (unlikely(muxer_msg_iter->clock_class_expectation == |
520 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) { | |
521 | *ts_ns = last_returned_ts_ns; | |
522 | goto end; | |
523 | } | |
524 | ||
649934d2 PP |
525 | msg_type = bt_message_get_type(msg); |
526 | ||
527 | if (unlikely(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) { | |
528 | stream_class = bt_stream_borrow_class_const( | |
529 | bt_packet_borrow_stream_const( | |
530 | bt_message_packet_beginning_borrow_packet_const( | |
531 | msg))); | |
532 | } else if (unlikely(msg_type == BT_MESSAGE_TYPE_PACKET_END)) { | |
533 | stream_class = bt_stream_borrow_class_const( | |
534 | bt_packet_borrow_stream_const( | |
535 | bt_message_packet_end_borrow_packet_const( | |
536 | msg))); | |
2e90378a PP |
537 | } else if (unlikely(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) { |
538 | stream_class = bt_stream_borrow_class_const( | |
539 | bt_message_discarded_events_borrow_stream_const(msg)); | |
540 | } else if (unlikely(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) { | |
541 | stream_class = bt_stream_borrow_class_const( | |
542 | bt_message_discarded_packets_borrow_stream_const(msg)); | |
649934d2 PP |
543 | } |
544 | ||
545 | switch (msg_type) { | |
d6e69534 | 546 | case BT_MESSAGE_TYPE_EVENT: |
60f3d027 PP |
547 | BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const( |
548 | msg)); | |
0cbc2c33 PP |
549 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
550 | msg); | |
958f7d11 | 551 | break; |
5366eb53 | 552 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
9b24b6aa | 553 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot( |
649934d2 PP |
554 | stream_class)) { |
555 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const( | |
556 | msg); | |
557 | } else { | |
558 | goto no_clock_snapshot; | |
559 | } | |
560 | ||
5366eb53 PP |
561 | break; |
562 | case BT_MESSAGE_TYPE_PACKET_END: | |
9b24b6aa | 563 | if (bt_stream_class_packets_have_end_default_clock_snapshot( |
649934d2 PP |
564 | stream_class)) { |
565 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const( | |
566 | msg); | |
567 | } else { | |
568 | goto no_clock_snapshot; | |
569 | } | |
570 | ||
5366eb53 PP |
571 | break; |
572 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
2e90378a PP |
573 | if (bt_stream_class_discarded_events_have_default_clock_snapshots( |
574 | stream_class)) { | |
9b24b6aa | 575 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
2e90378a PP |
576 | msg); |
577 | } else { | |
578 | goto no_clock_snapshot; | |
579 | } | |
580 | ||
5366eb53 PP |
581 | break; |
582 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
2e90378a PP |
583 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots( |
584 | stream_class)) { | |
9b24b6aa | 585 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
2e90378a PP |
586 | msg); |
587 | } else { | |
588 | goto no_clock_snapshot; | |
589 | } | |
590 | ||
5366eb53 PP |
591 | break; |
592 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: | |
60f3d027 PP |
593 | BT_ASSERT(bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const( |
594 | msg)); | |
5366eb53 PP |
595 | sa_cs_state = bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const( |
596 | msg, &clock_snapshot); | |
597 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
4c0f1c8c | 598 | goto no_clock_snapshot; |
5366eb53 | 599 | } |
958f7d11 | 600 | |
5366eb53 PP |
601 | break; |
602 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: | |
60f3d027 PP |
603 | BT_ASSERT(bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const( |
604 | msg)); | |
5366eb53 PP |
605 | sa_cs_state = bt_message_stream_activity_end_borrow_default_clock_snapshot_const( |
606 | msg, &clock_snapshot); | |
607 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
4c0f1c8c | 608 | goto no_clock_snapshot; |
5366eb53 PP |
609 | } |
610 | ||
611 | break; | |
b9fd9cbb | 612 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
0cbc2c33 PP |
613 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
614 | msg); | |
958f7d11 PP |
615 | break; |
616 | default: | |
d6e69534 PP |
617 | /* All the other messages have a higher priority */ |
618 | BT_LOGV_STR("Message has no timestamp: using the last returned timestamp."); | |
958f7d11 PP |
619 | *ts_ns = last_returned_ts_ns; |
620 | goto end; | |
621 | } | |
622 | ||
60f3d027 PP |
623 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
624 | if (ret) { | |
625 | BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: " | |
626 | "clock-snapshot-addr=%p", clock_snapshot); | |
627 | goto error; | |
628 | } | |
629 | ||
630 | goto end; | |
631 | ||
632 | no_clock_snapshot: | |
633 | BT_LOGV_STR("Message's default clock snapshot is missing: " | |
634 | "using the last returned timestamp."); | |
635 | *ts_ns = last_returned_ts_ns; | |
636 | goto end; | |
637 | ||
638 | error: | |
639 | ret = -1; | |
640 | ||
641 | end: | |
642 | if (ret == 0) { | |
643 | BT_LOGV("Found message's timestamp: " | |
644 | "muxer-msg-iter-addr=%p, msg-addr=%p, " | |
645 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, | |
646 | muxer_msg_iter, msg, last_returned_ts_ns, | |
647 | *ts_ns); | |
44c440bc PP |
648 | } |
649 | ||
60f3d027 PP |
650 | return ret; |
651 | } | |
652 | ||
653 | static inline | |
654 | int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter, | |
655 | struct muxer_comp *muxer_comp, | |
656 | const bt_clock_class *clock_class) | |
657 | { | |
658 | int ret = 0; | |
659 | const unsigned char *cc_uuid; | |
660 | const char *cc_name; | |
661 | ||
e22b45d0 | 662 | BT_ASSERT(clock_class); |
50842bdc PP |
663 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
664 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 | 665 | |
d6e69534 PP |
666 | if (muxer_msg_iter->clock_class_expectation == |
667 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
282c8cd0 PP |
668 | /* |
669 | * This is the first clock class that this muxer | |
d6e69534 | 670 | * message iterator encounters. Its properties |
282c8cd0 PP |
671 | * determine what to expect for the whole lifetime of |
672 | * the iterator without a true | |
673 | * `assume-absolute-clock-classes` parameter. | |
674 | */ | |
5552377a | 675 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
282c8cd0 | 676 | /* Expect absolute clock classes */ |
d6e69534 PP |
677 | muxer_msg_iter->clock_class_expectation = |
678 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
282c8cd0 PP |
679 | } else { |
680 | if (cc_uuid) { | |
681 | /* | |
682 | * Expect non-absolute clock classes | |
683 | * with a specific UUID. | |
684 | */ | |
d6e69534 PP |
685 | muxer_msg_iter->clock_class_expectation = |
686 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; | |
687 | memcpy(muxer_msg_iter->expected_clock_class_uuid, | |
282c8cd0 PP |
688 | cc_uuid, BABELTRACE_UUID_LEN); |
689 | } else { | |
690 | /* | |
691 | * Expect non-absolute clock classes | |
692 | * with no UUID. | |
693 | */ | |
d6e69534 PP |
694 | muxer_msg_iter->clock_class_expectation = |
695 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
282c8cd0 PP |
696 | } |
697 | } | |
698 | } | |
699 | ||
700 | if (!muxer_comp->assume_absolute_clock_classes) { | |
d6e69534 PP |
701 | switch (muxer_msg_iter->clock_class_expectation) { |
702 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
5552377a | 703 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
704 | BT_LOGE("Expecting an absolute clock class, " |
705 | "but got a non-absolute one: " | |
706 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
707 | clock_class, cc_name); | |
282c8cd0 PP |
708 | goto error; |
709 | } | |
710 | break; | |
d6e69534 | 711 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: |
5552377a | 712 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
713 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
714 | "but got an absolute one: " | |
715 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
716 | clock_class, cc_name); | |
282c8cd0 PP |
717 | goto error; |
718 | } | |
719 | ||
720 | if (cc_uuid) { | |
fed72692 PP |
721 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
722 | "but got one with a UUID: " | |
723 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
724 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
725 | clock_class, cc_name, | |
726 | (unsigned int) cc_uuid[0], | |
727 | (unsigned int) cc_uuid[1], | |
728 | (unsigned int) cc_uuid[2], | |
729 | (unsigned int) cc_uuid[3], | |
730 | (unsigned int) cc_uuid[4], | |
731 | (unsigned int) cc_uuid[5], | |
732 | (unsigned int) cc_uuid[6], | |
733 | (unsigned int) cc_uuid[7], | |
734 | (unsigned int) cc_uuid[8], | |
735 | (unsigned int) cc_uuid[9], | |
736 | (unsigned int) cc_uuid[10], | |
737 | (unsigned int) cc_uuid[11], | |
738 | (unsigned int) cc_uuid[12], | |
739 | (unsigned int) cc_uuid[13], | |
740 | (unsigned int) cc_uuid[14], | |
741 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
742 | goto error; |
743 | } | |
744 | break; | |
d6e69534 | 745 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: |
5552377a | 746 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
747 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
748 | "but got an absolute one: " | |
749 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
750 | clock_class, cc_name); | |
282c8cd0 PP |
751 | goto error; |
752 | } | |
753 | ||
754 | if (!cc_uuid) { | |
fed72692 PP |
755 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
756 | "but got one with no UUID: " | |
757 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
758 | clock_class, cc_name); | |
282c8cd0 PP |
759 | goto error; |
760 | } | |
761 | ||
d6e69534 | 762 | if (memcmp(muxer_msg_iter->expected_clock_class_uuid, |
282c8cd0 | 763 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { |
fed72692 PP |
764 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
765 | "but got one with different UUID: " | |
766 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
767 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " | |
768 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
769 | clock_class, cc_name, | |
d6e69534 PP |
770 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[0], |
771 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[1], | |
772 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[2], | |
773 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[3], | |
774 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[4], | |
775 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[5], | |
776 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[6], | |
777 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[7], | |
778 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[8], | |
779 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[9], | |
780 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[10], | |
781 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[11], | |
782 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[12], | |
783 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[13], | |
784 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[14], | |
785 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[15], | |
fed72692 PP |
786 | (unsigned int) cc_uuid[0], |
787 | (unsigned int) cc_uuid[1], | |
788 | (unsigned int) cc_uuid[2], | |
789 | (unsigned int) cc_uuid[3], | |
790 | (unsigned int) cc_uuid[4], | |
791 | (unsigned int) cc_uuid[5], | |
792 | (unsigned int) cc_uuid[6], | |
793 | (unsigned int) cc_uuid[7], | |
794 | (unsigned int) cc_uuid[8], | |
795 | (unsigned int) cc_uuid[9], | |
796 | (unsigned int) cc_uuid[10], | |
797 | (unsigned int) cc_uuid[11], | |
798 | (unsigned int) cc_uuid[12], | |
799 | (unsigned int) cc_uuid[13], | |
800 | (unsigned int) cc_uuid[14], | |
801 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
802 | goto error; |
803 | } | |
804 | break; | |
60f3d027 PP |
805 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE: |
806 | BT_LOGE("Expecting no clock class, but got one: " | |
807 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
808 | clock_class, cc_name); | |
809 | goto error; | |
282c8cd0 PP |
810 | default: |
811 | /* Unexpected */ | |
fed72692 PP |
812 | BT_LOGF("Unexpected clock class expectation: " |
813 | "expectation-code=%d", | |
d6e69534 | 814 | muxer_msg_iter->clock_class_expectation); |
282c8cd0 PP |
815 | abort(); |
816 | } | |
958f7d11 PP |
817 | } |
818 | ||
4c0f1c8c PP |
819 | goto end; |
820 | ||
958f7d11 PP |
821 | error: |
822 | ret = -1; | |
823 | ||
824 | end: | |
60f3d027 PP |
825 | return ret; |
826 | } | |
827 | ||
828 | static inline | |
829 | int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter, | |
830 | struct muxer_comp *muxer_comp, const bt_stream *stream) | |
831 | { | |
832 | int ret = 0; | |
833 | const bt_stream_class *stream_class = | |
834 | bt_stream_borrow_class_const(stream); | |
835 | const bt_clock_class *clock_class = | |
836 | bt_stream_class_borrow_default_clock_class_const(stream_class); | |
837 | ||
838 | if (!clock_class) { | |
839 | if (muxer_msg_iter->clock_class_expectation == | |
840 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
841 | /* Expect no clock class */ | |
842 | muxer_msg_iter->clock_class_expectation = | |
843 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE; | |
844 | } else { | |
845 | BT_LOGE("Expecting stream class with a default clock class: " | |
846 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
847 | "stream-class-id=%" PRIu64, | |
848 | stream_class, bt_stream_class_get_name(stream_class), | |
849 | bt_stream_class_get_id(stream_class)); | |
850 | ret = -1; | |
851 | } | |
852 | ||
853 | goto end; | |
fed72692 PP |
854 | } |
855 | ||
60f3d027 PP |
856 | ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class); |
857 | ||
858 | end: | |
958f7d11 PP |
859 | return ret; |
860 | } | |
861 | ||
ab11110e | 862 | /* |
d6e69534 PP |
863 | * This function finds the youngest available message amongst the |
864 | * non-ended upstream message iterators and returns the upstream | |
865 | * message iterator which has it, or | |
866 | * BT_MESSAGE_ITERATOR_STATUS_END if there's no available | |
867 | * message. | |
ab11110e PP |
868 | * |
869 | * This function does NOT: | |
870 | * | |
d6e69534 | 871 | * * Update any upstream message iterator. |
d6e69534 | 872 | * * Check the upstream message iterators to retry. |
ab11110e | 873 | * |
d6e69534 PP |
874 | * On sucess, this function sets *muxer_upstream_msg_iter to the |
875 | * upstream message iterator of which the current message is | |
ab11110e PP |
876 | * the youngest, and sets *ts_ns to its time. |
877 | */ | |
958f7d11 | 878 | static |
fdf0b89e | 879 | bt_self_message_iterator_status |
d6e69534 | 880 | muxer_msg_iter_youngest_upstream_msg_iter( |
958f7d11 | 881 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
882 | struct muxer_msg_iter *muxer_msg_iter, |
883 | struct muxer_upstream_msg_iter **muxer_upstream_msg_iter, | |
958f7d11 PP |
884 | int64_t *ts_ns) |
885 | { | |
886 | size_t i; | |
887 | int ret; | |
888 | int64_t youngest_ts_ns = INT64_MAX; | |
fdf0b89e FD |
889 | bt_self_message_iterator_status status = |
890 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 891 | |
f6ccaed9 | 892 | BT_ASSERT(muxer_comp); |
d6e69534 PP |
893 | BT_ASSERT(muxer_msg_iter); |
894 | BT_ASSERT(muxer_upstream_msg_iter); | |
895 | *muxer_upstream_msg_iter = NULL; | |
896 | ||
54bdc1f7 PP |
897 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
898 | i++) { | |
d6e69534 PP |
899 | const bt_message *msg; |
900 | struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter = | |
54bdc1f7 PP |
901 | g_ptr_array_index( |
902 | muxer_msg_iter->active_muxer_upstream_msg_iters, | |
903 | i); | |
d6e69534 PP |
904 | int64_t msg_ts_ns; |
905 | ||
906 | if (!cur_muxer_upstream_msg_iter->msg_iter) { | |
907 | /* This upstream message iterator is ended */ | |
908 | BT_LOGV("Skipping ended upstream message iterator: " | |
909 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
910 | cur_muxer_upstream_msg_iter); | |
958f7d11 PP |
911 | continue; |
912 | } | |
913 | ||
d6e69534 PP |
914 | BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0); |
915 | msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs); | |
916 | BT_ASSERT(msg); | |
60f3d027 PP |
917 | |
918 | if (unlikely(bt_message_get_type(msg) == | |
919 | BT_MESSAGE_TYPE_STREAM_BEGINNING)) { | |
920 | ret = validate_new_stream_clock_class( | |
921 | muxer_msg_iter, muxer_comp, | |
922 | bt_message_stream_beginning_borrow_stream_const( | |
923 | msg)); | |
924 | if (ret) { | |
925 | /* | |
926 | * validate_new_stream_clock_class() logs | |
927 | * errors. | |
928 | */ | |
929 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
930 | goto end; | |
931 | } | |
932 | } else if (unlikely(bt_message_get_type(msg) == | |
933 | BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) { | |
934 | const bt_clock_snapshot *cs; | |
60f3d027 | 935 | |
0cbc2c33 PP |
936 | cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
937 | msg); | |
60f3d027 PP |
938 | ret = validate_clock_class(muxer_msg_iter, muxer_comp, |
939 | bt_clock_snapshot_borrow_clock_class_const(cs)); | |
940 | if (ret) { | |
941 | /* validate_clock_class() logs errors */ | |
942 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
943 | goto end; | |
944 | } | |
945 | } | |
946 | ||
d6e69534 PP |
947 | ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg, |
948 | muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns); | |
958f7d11 | 949 | if (ret) { |
d6e69534 PP |
950 | /* get_msg_ts_ns() logs errors */ |
951 | *muxer_upstream_msg_iter = NULL; | |
fdf0b89e | 952 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
953 | goto end; |
954 | } | |
955 | ||
d6e69534 PP |
956 | if (msg_ts_ns <= youngest_ts_ns) { |
957 | *muxer_upstream_msg_iter = | |
958 | cur_muxer_upstream_msg_iter; | |
959 | youngest_ts_ns = msg_ts_ns; | |
958f7d11 PP |
960 | *ts_ns = youngest_ts_ns; |
961 | } | |
962 | } | |
963 | ||
d6e69534 | 964 | if (!*muxer_upstream_msg_iter) { |
fdf0b89e | 965 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
958f7d11 PP |
966 | *ts_ns = INT64_MIN; |
967 | } | |
968 | ||
969 | end: | |
970 | return status; | |
971 | } | |
972 | ||
973 | static | |
fdf0b89e | 974 | bt_self_message_iterator_status validate_muxer_upstream_msg_iter( |
54bdc1f7 PP |
975 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
976 | bool *is_ended) | |
958f7d11 | 977 | { |
fdf0b89e FD |
978 | bt_self_message_iterator_status status = |
979 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 980 | |
d6e69534 PP |
981 | BT_LOGV("Validating muxer's upstream message iterator wrapper: " |
982 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
983 | muxer_upstream_msg_iter); | |
fed72692 | 984 | |
d6e69534 PP |
985 | if (muxer_upstream_msg_iter->msgs->length > 0 || |
986 | !muxer_upstream_msg_iter->msg_iter) { | |
d4393e08 | 987 | BT_LOGV("Already valid or not considered: " |
d6e69534 PP |
988 | "queue-len=%u, upstream-msg-iter-addr=%p", |
989 | muxer_upstream_msg_iter->msgs->length, | |
990 | muxer_upstream_msg_iter->msg_iter); | |
ab11110e PP |
991 | goto end; |
992 | } | |
993 | ||
d6e69534 | 994 | /* muxer_upstream_msg_iter_next() logs details/errors */ |
54bdc1f7 PP |
995 | status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter, |
996 | is_ended); | |
089717de PP |
997 | |
998 | end: | |
999 | return status; | |
1000 | } | |
1001 | ||
1002 | static | |
fdf0b89e | 1003 | bt_self_message_iterator_status validate_muxer_upstream_msg_iters( |
d6e69534 | 1004 | struct muxer_msg_iter *muxer_msg_iter) |
089717de | 1005 | { |
fdf0b89e FD |
1006 | bt_self_message_iterator_status status = |
1007 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
089717de PP |
1008 | size_t i; |
1009 | ||
d6e69534 PP |
1010 | BT_LOGV("Validating muxer's upstream message iterator wrappers: " |
1011 | "muxer-msg-iter-addr=%p", muxer_msg_iter); | |
fed72692 | 1012 | |
54bdc1f7 PP |
1013 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
1014 | i++) { | |
6bf2abbd | 1015 | bool is_ended = false; |
d6e69534 | 1016 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
089717de | 1017 | g_ptr_array_index( |
54bdc1f7 | 1018 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
089717de PP |
1019 | i); |
1020 | ||
d6e69534 | 1021 | status = validate_muxer_upstream_msg_iter( |
54bdc1f7 | 1022 | muxer_upstream_msg_iter, &is_ended); |
fdf0b89e | 1023 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
fed72692 | 1024 | if (status < 0) { |
d6e69534 PP |
1025 | BT_LOGE("Cannot validate muxer's upstream message iterator wrapper: " |
1026 | "muxer-msg-iter-addr=%p, " | |
1027 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1028 | muxer_msg_iter, | |
1029 | muxer_upstream_msg_iter); | |
fed72692 | 1030 | } else { |
d6e69534 PP |
1031 | BT_LOGV("Cannot validate muxer's upstream message iterator wrapper: " |
1032 | "muxer-msg-iter-addr=%p, " | |
1033 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1034 | muxer_msg_iter, | |
1035 | muxer_upstream_msg_iter); | |
fed72692 PP |
1036 | } |
1037 | ||
089717de PP |
1038 | goto end; |
1039 | } | |
744ba28b PP |
1040 | |
1041 | /* | |
54bdc1f7 PP |
1042 | * Move this muxer upstream message iterator to the |
1043 | * array of ended iterators if it's ended. | |
744ba28b | 1044 | */ |
54bdc1f7 PP |
1045 | if (unlikely(is_ended)) { |
1046 | BT_LOGV("Muxer's upstream message iterator wrapper: ended or canceled: " | |
1047 | "muxer-msg-iter-addr=%p, " | |
1048 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1049 | muxer_msg_iter, muxer_upstream_msg_iter); | |
1050 | g_ptr_array_add( | |
1051 | muxer_msg_iter->ended_muxer_upstream_msg_iters, | |
1052 | muxer_upstream_msg_iter); | |
1053 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL; | |
1054 | ||
744ba28b PP |
1055 | /* |
1056 | * Use g_ptr_array_remove_fast() because the | |
1057 | * order of those elements is not important. | |
1058 | */ | |
1059 | g_ptr_array_remove_index_fast( | |
54bdc1f7 | 1060 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
744ba28b PP |
1061 | i); |
1062 | i--; | |
1063 | } | |
089717de PP |
1064 | } |
1065 | ||
1066 | end: | |
1067 | return status; | |
1068 | } | |
1069 | ||
d4393e08 | 1070 | static inline |
fdf0b89e | 1071 | bt_self_message_iterator_status muxer_msg_iter_do_next_one( |
089717de | 1072 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
1073 | struct muxer_msg_iter *muxer_msg_iter, |
1074 | const bt_message **msg) | |
089717de | 1075 | { |
5badd463 | 1076 | bt_self_message_iterator_status status; |
d6e69534 | 1077 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL; |
089717de PP |
1078 | int64_t next_return_ts; |
1079 | ||
5badd463 PP |
1080 | status = validate_muxer_upstream_msg_iters(muxer_msg_iter); |
1081 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
1082 | /* validate_muxer_upstream_msg_iters() logs details */ | |
1083 | goto end; | |
958f7d11 PP |
1084 | } |
1085 | ||
1086 | /* | |
089717de | 1087 | * At this point we know that all the existing upstream |
d6e69534 PP |
1088 | * message iterators are valid. We can find the one, |
1089 | * amongst those, of which the current message is the | |
089717de | 1090 | * youngest. |
958f7d11 | 1091 | */ |
d6e69534 PP |
1092 | status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp, |
1093 | muxer_msg_iter, &muxer_upstream_msg_iter, | |
089717de | 1094 | &next_return_ts); |
fdf0b89e | 1095 | if (status < 0 || status == BT_SELF_MESSAGE_ITERATOR_STATUS_END) { |
d4393e08 | 1096 | if (status < 0) { |
d6e69534 | 1097 | BT_LOGE("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1098 | "status=%s", |
bd1a54fe | 1099 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1100 | } else { |
d6e69534 | 1101 | BT_LOGV("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1102 | "status=%s", |
bd1a54fe | 1103 | bt_common_self_message_iterator_status_string(status)); |
fed72692 PP |
1104 | } |
1105 | ||
958f7d11 PP |
1106 | goto end; |
1107 | } | |
1108 | ||
d6e69534 PP |
1109 | if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) { |
1110 | BT_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: " | |
1111 | "muxer-msg-iter-addr=%p, ts=%" PRId64 ", " | |
fed72692 | 1112 | "last-returned-ts=%" PRId64, |
d6e69534 PP |
1113 | muxer_msg_iter, next_return_ts, |
1114 | muxer_msg_iter->last_returned_ts_ns); | |
fdf0b89e | 1115 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1116 | goto end; |
1117 | } | |
1118 | ||
d6e69534 PP |
1119 | BT_LOGV("Found youngest upstream message iterator wrapper: " |
1120 | "muxer-msg-iter-addr=%p, " | |
1121 | "muxer-upstream-msg-iter-wrap-addr=%p, " | |
fed72692 | 1122 | "ts=%" PRId64, |
d6e69534 | 1123 | muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts); |
fdf0b89e | 1124 | BT_ASSERT(status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK); |
d6e69534 | 1125 | BT_ASSERT(muxer_upstream_msg_iter); |
958f7d11 PP |
1126 | |
1127 | /* | |
d4393e08 | 1128 | * Consume from the queue's head: other side |
d6e69534 | 1129 | * (muxer_upstream_msg_iter_next()) writes to the tail. |
958f7d11 | 1130 | */ |
d6e69534 PP |
1131 | *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs); |
1132 | BT_ASSERT(*msg); | |
1133 | muxer_msg_iter->last_returned_ts_ns = next_return_ts; | |
958f7d11 PP |
1134 | |
1135 | end: | |
d4393e08 PP |
1136 | return status; |
1137 | } | |
1138 | ||
1139 | static | |
fdf0b89e | 1140 | bt_self_message_iterator_status muxer_msg_iter_do_next( |
d4393e08 | 1141 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
1142 | struct muxer_msg_iter *muxer_msg_iter, |
1143 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
1144 | uint64_t *count) |
1145 | { | |
fdf0b89e FD |
1146 | bt_self_message_iterator_status status = |
1147 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
d4393e08 PP |
1148 | uint64_t i = 0; |
1149 | ||
4725a201 | 1150 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
d6e69534 PP |
1151 | status = muxer_msg_iter_do_next_one(muxer_comp, |
1152 | muxer_msg_iter, &msgs[i]); | |
4725a201 | 1153 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
d4393e08 PP |
1154 | i++; |
1155 | } | |
1156 | } | |
1157 | ||
1158 | if (i > 0) { | |
1159 | /* | |
d6e69534 | 1160 | * Even if muxer_msg_iter_do_next_one() returned |
d4393e08 | 1161 | * something else than |
d6e69534 PP |
1162 | * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated |
1163 | * message objects in the output message | |
d4393e08 | 1164 | * array, so we need to return |
d6e69534 | 1165 | * BT_MESSAGE_ITERATOR_STATUS_OK so that they are |
d4393e08 | 1166 | * transfered to downstream. This other status occurs |
d6e69534 | 1167 | * again the next time muxer_msg_iter_do_next() is |
d4393e08 | 1168 | * called, possibly without any accumulated |
d6e69534 | 1169 | * message, in which case we'll return it. |
d4393e08 PP |
1170 | */ |
1171 | *count = i; | |
fdf0b89e | 1172 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
1173 | } |
1174 | ||
1175 | return status; | |
958f7d11 PP |
1176 | } |
1177 | ||
1178 | static | |
d6e69534 | 1179 | void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter) |
ab11110e | 1180 | { |
d6e69534 | 1181 | if (!muxer_msg_iter) { |
ab11110e PP |
1182 | return; |
1183 | } | |
1184 | ||
d6e69534 PP |
1185 | BT_LOGD("Destroying muxer component's message iterator: " |
1186 | "muxer-msg-iter-addr=%p", muxer_msg_iter); | |
fed72692 | 1187 | |
54bdc1f7 PP |
1188 | if (muxer_msg_iter->active_muxer_upstream_msg_iters) { |
1189 | BT_LOGD_STR("Destroying muxer's active upstream message iterator wrappers."); | |
1190 | g_ptr_array_free( | |
1191 | muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE); | |
1192 | } | |
1193 | ||
1194 | if (muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
1195 | BT_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers."); | |
ab11110e | 1196 | g_ptr_array_free( |
54bdc1f7 | 1197 | muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE); |
ab11110e PP |
1198 | } |
1199 | ||
d6e69534 | 1200 | g_free(muxer_msg_iter); |
ab11110e PP |
1201 | } |
1202 | ||
1203 | static | |
5badd463 | 1204 | int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp, |
d6e69534 | 1205 | struct muxer_msg_iter *muxer_msg_iter) |
958f7d11 | 1206 | { |
544d0515 PP |
1207 | int64_t count; |
1208 | int64_t i; | |
ab11110e | 1209 | int ret = 0; |
958f7d11 | 1210 | |
d94d92ac | 1211 | count = bt_component_filter_get_input_port_count( |
707b7d35 | 1212 | bt_self_component_filter_as_component_filter( |
d94d92ac | 1213 | muxer_comp->self_comp)); |
544d0515 | 1214 | if (count < 0) { |
d6e69534 PP |
1215 | BT_LOGD("No input port to initialize for muxer component's message iterator: " |
1216 | "muxer-comp-addr=%p, muxer-msg-iter-addr=%p", | |
1217 | muxer_comp, muxer_msg_iter); | |
ab11110e PP |
1218 | goto end; |
1219 | } | |
958f7d11 PP |
1220 | |
1221 | for (i = 0; i < count; i++) { | |
5badd463 | 1222 | bt_self_component_port_input_message_iterator *upstream_msg_iter; |
b19ff26f | 1223 | bt_self_component_port_input *self_port = |
d94d92ac PP |
1224 | bt_self_component_filter_borrow_input_port_by_index( |
1225 | muxer_comp->self_comp, i); | |
b19ff26f | 1226 | const bt_port *port; |
958f7d11 | 1227 | |
d94d92ac | 1228 | BT_ASSERT(self_port); |
707b7d35 PP |
1229 | port = bt_self_component_port_as_port( |
1230 | bt_self_component_port_input_as_self_component_port( | |
d94d92ac | 1231 | self_port)); |
f6ccaed9 | 1232 | BT_ASSERT(port); |
958f7d11 PP |
1233 | |
1234 | if (!bt_port_is_connected(port)) { | |
5badd463 | 1235 | /* Skip non-connected port */ |
958f7d11 PP |
1236 | continue; |
1237 | } | |
1238 | ||
5badd463 PP |
1239 | upstream_msg_iter = create_msg_iter_on_input_port(self_port); |
1240 | if (!upstream_msg_iter) { | |
1241 | /* create_msg_iter_on_input_port() logs errors */ | |
1242 | BT_ASSERT(!upstream_msg_iter); | |
ab11110e PP |
1243 | ret = -1; |
1244 | goto end; | |
958f7d11 | 1245 | } |
fed72692 | 1246 | |
c61018b9 FD |
1247 | ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter, |
1248 | upstream_msg_iter); | |
5badd463 PP |
1249 | bt_self_component_port_input_message_iterator_put_ref( |
1250 | upstream_msg_iter); | |
c61018b9 | 1251 | if (ret) { |
5badd463 | 1252 | /* muxer_msg_iter_add_upstream_msg_iter() logs errors */ |
5badd463 PP |
1253 | goto end; |
1254 | } | |
958f7d11 PP |
1255 | } |
1256 | ||
1257 | end: | |
958f7d11 PP |
1258 | return ret; |
1259 | } | |
1260 | ||
958f7d11 | 1261 | BT_HIDDEN |
4cdfc5e8 | 1262 | bt_self_message_iterator_status muxer_msg_iter_init( |
d6e69534 | 1263 | bt_self_message_iterator *self_msg_iter, |
b19ff26f PP |
1264 | bt_self_component_filter *self_comp, |
1265 | bt_self_component_port_output *port) | |
958f7d11 PP |
1266 | { |
1267 | struct muxer_comp *muxer_comp = NULL; | |
d6e69534 | 1268 | struct muxer_msg_iter *muxer_msg_iter = NULL; |
4cdfc5e8 | 1269 | bt_self_message_iterator_status status = |
fdf0b89e | 1270 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
958f7d11 PP |
1271 | int ret; |
1272 | ||
d94d92ac | 1273 | muxer_comp = bt_self_component_get_data( |
707b7d35 | 1274 | bt_self_component_filter_as_self_component(self_comp)); |
f6ccaed9 | 1275 | BT_ASSERT(muxer_comp); |
d6e69534 PP |
1276 | BT_LOGD("Initializing muxer component's message iterator: " |
1277 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", | |
1278 | self_comp, muxer_comp, self_msg_iter); | |
a09c6b95 | 1279 | |
d6e69534 | 1280 | if (muxer_comp->initializing_muxer_msg_iter) { |
a09c6b95 | 1281 | /* |
089717de | 1282 | * Weird, unhandled situation detected: downstream |
d6e69534 PP |
1283 | * creates a muxer message iterator while creating |
1284 | * another muxer message iterator (same component). | |
a09c6b95 | 1285 | */ |
d6e69534 PP |
1286 | BT_LOGE("Recursive initialization of muxer component's message iterator: " |
1287 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", | |
1288 | self_comp, muxer_comp, self_msg_iter); | |
958f7d11 PP |
1289 | goto error; |
1290 | } | |
1291 | ||
d6e69534 PP |
1292 | muxer_comp->initializing_muxer_msg_iter = true; |
1293 | muxer_msg_iter = g_new0(struct muxer_msg_iter, 1); | |
1294 | if (!muxer_msg_iter) { | |
1295 | BT_LOGE_STR("Failed to allocate one muxer component's message iterator."); | |
ab11110e PP |
1296 | goto error; |
1297 | } | |
1298 | ||
d6e69534 | 1299 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
54bdc1f7 | 1300 | muxer_msg_iter->active_muxer_upstream_msg_iters = |
958f7d11 | 1301 | g_ptr_array_new_with_free_func( |
d6e69534 | 1302 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); |
54bdc1f7 PP |
1303 | if (!muxer_msg_iter->active_muxer_upstream_msg_iters) { |
1304 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
1305 | goto error; | |
1306 | } | |
1307 | ||
1308 | muxer_msg_iter->ended_muxer_upstream_msg_iters = | |
1309 | g_ptr_array_new_with_free_func( | |
1310 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); | |
1311 | if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
fed72692 | 1312 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1313 | goto error; |
1314 | } | |
1315 | ||
5badd463 | 1316 | ret = muxer_msg_iter_init_upstream_iterators(muxer_comp, |
d6e69534 | 1317 | muxer_msg_iter); |
a09c6b95 | 1318 | if (ret) { |
5badd463 | 1319 | BT_LOGE("Cannot initialize connected input ports for muxer component's message iterator: " |
fed72692 | 1320 | "comp-addr=%p, muxer-comp-addr=%p, " |
d6e69534 PP |
1321 | "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d", |
1322 | self_comp, muxer_comp, muxer_msg_iter, | |
1323 | self_msg_iter, ret); | |
a09c6b95 PP |
1324 | goto error; |
1325 | } | |
1326 | ||
5badd463 | 1327 | bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter); |
d6e69534 PP |
1328 | BT_LOGD("Initialized muxer component's message iterator: " |
1329 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1330 | "msg-iter-addr=%p", | |
1331 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 PP |
1332 | goto end; |
1333 | ||
1334 | error: | |
d6e69534 | 1335 | destroy_muxer_msg_iter(muxer_msg_iter); |
5badd463 | 1336 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
fdf0b89e | 1337 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1338 | |
1339 | end: | |
d6e69534 | 1340 | muxer_comp->initializing_muxer_msg_iter = false; |
958f7d11 PP |
1341 | return status; |
1342 | } | |
1343 | ||
1344 | BT_HIDDEN | |
54bdc1f7 | 1345 | void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
958f7d11 | 1346 | { |
d6e69534 PP |
1347 | struct muxer_msg_iter *muxer_msg_iter = |
1348 | bt_self_message_iterator_get_data(self_msg_iter); | |
b19ff26f | 1349 | bt_self_component *self_comp = NULL; |
958f7d11 PP |
1350 | struct muxer_comp *muxer_comp = NULL; |
1351 | ||
d6e69534 PP |
1352 | self_comp = bt_self_message_iterator_borrow_component( |
1353 | self_msg_iter); | |
d94d92ac PP |
1354 | BT_ASSERT(self_comp); |
1355 | muxer_comp = bt_self_component_get_data(self_comp); | |
d6e69534 PP |
1356 | BT_LOGD("Finalizing muxer component's message iterator: " |
1357 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1358 | "msg-iter-addr=%p", | |
1359 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 | 1360 | |
5badd463 | 1361 | if (muxer_msg_iter) { |
d6e69534 | 1362 | destroy_muxer_msg_iter(muxer_msg_iter); |
958f7d11 | 1363 | } |
958f7d11 PP |
1364 | } |
1365 | ||
1366 | BT_HIDDEN | |
fdf0b89e | 1367 | bt_self_message_iterator_status muxer_msg_iter_next( |
d6e69534 PP |
1368 | bt_self_message_iterator *self_msg_iter, |
1369 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 | 1370 | uint64_t *count) |
958f7d11 | 1371 | { |
fdf0b89e | 1372 | bt_self_message_iterator_status status; |
d6e69534 PP |
1373 | struct muxer_msg_iter *muxer_msg_iter = |
1374 | bt_self_message_iterator_get_data(self_msg_iter); | |
b19ff26f | 1375 | bt_self_component *self_comp = NULL; |
958f7d11 | 1376 | struct muxer_comp *muxer_comp = NULL; |
958f7d11 | 1377 | |
d6e69534 PP |
1378 | BT_ASSERT(muxer_msg_iter); |
1379 | self_comp = bt_self_message_iterator_borrow_component( | |
1380 | self_msg_iter); | |
d94d92ac PP |
1381 | BT_ASSERT(self_comp); |
1382 | muxer_comp = bt_self_component_get_data(self_comp); | |
f6ccaed9 | 1383 | BT_ASSERT(muxer_comp); |
d6e69534 PP |
1384 | BT_LOGV("Muxer component's message iterator's \"next\" method called: " |
1385 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1386 | "msg-iter-addr=%p", | |
1387 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
fed72692 | 1388 | |
d6e69534 PP |
1389 | status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter, |
1390 | msgs, capacity, count); | |
d4393e08 | 1391 | if (status < 0) { |
d6e69534 PP |
1392 | BT_LOGE("Cannot get next message: " |
1393 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1394 | "msg-iter-addr=%p, status=%s", | |
1395 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter, | |
bd1a54fe | 1396 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1397 | } else { |
d6e69534 | 1398 | BT_LOGV("Returning from muxer component's message iterator's \"next\" method: " |
d4393e08 | 1399 | "status=%s", |
bd1a54fe | 1400 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1401 | } |
958f7d11 | 1402 | |
d4393e08 | 1403 | return status; |
958f7d11 PP |
1404 | } |
1405 | ||
1406 | BT_HIDDEN | |
4cdfc5e8 | 1407 | bt_self_component_status muxer_input_port_connected( |
b19ff26f PP |
1408 | bt_self_component_filter *self_comp, |
1409 | bt_self_component_port_input *self_port, | |
1410 | const bt_port_output *other_port) | |
958f7d11 | 1411 | { |
5badd463 | 1412 | bt_self_component_status status; |
958f7d11 | 1413 | |
5badd463 PP |
1414 | status = add_available_input_port(self_comp); |
1415 | if (status) { | |
06a2cb0d PP |
1416 | /* |
1417 | * Only way to report an error later since this | |
1418 | * method does not return anything. | |
1419 | */ | |
5badd463 PP |
1420 | BT_LOGE("Cannot add one muxer component's input port: " |
1421 | "status=%s", | |
1422 | bt_self_component_status_string(status)); | |
06a2cb0d PP |
1423 | goto end; |
1424 | } | |
1425 | ||
958f7d11 | 1426 | end: |
bf55043c | 1427 | return status; |
958f7d11 | 1428 | } |
b5443165 | 1429 | |
54bdc1f7 PP |
1430 | static inline |
1431 | bt_bool muxer_upstream_msg_iters_can_all_seek_beginning( | |
1432 | GPtrArray *muxer_upstream_msg_iters) | |
b5443165 | 1433 | { |
b5443165 PP |
1434 | uint64_t i; |
1435 | bt_bool ret = BT_TRUE; | |
1436 | ||
54bdc1f7 | 1437 | for (i = 0; i < muxer_upstream_msg_iters->len; i++) { |
b5443165 | 1438 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
54bdc1f7 | 1439 | muxer_upstream_msg_iters->pdata[i]; |
b5443165 PP |
1440 | |
1441 | if (!bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1442 | upstream_msg_iter->msg_iter)) { | |
1443 | ret = BT_FALSE; | |
1444 | goto end; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | end: | |
1449 | return ret; | |
1450 | } | |
1451 | ||
54bdc1f7 PP |
1452 | BT_HIDDEN |
1453 | bt_bool muxer_msg_iter_can_seek_beginning( | |
1454 | bt_self_message_iterator *self_msg_iter) | |
1455 | { | |
1456 | struct muxer_msg_iter *muxer_msg_iter = | |
1457 | bt_self_message_iterator_get_data(self_msg_iter); | |
1458 | bt_bool ret = BT_TRUE; | |
1459 | ||
1460 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1461 | muxer_msg_iter->active_muxer_upstream_msg_iters)) { | |
1462 | ret = BT_FALSE; | |
1463 | goto end; | |
1464 | } | |
1465 | ||
1466 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1467 | muxer_msg_iter->ended_muxer_upstream_msg_iters)) { | |
1468 | ret = BT_FALSE; | |
1469 | goto end; | |
1470 | } | |
1471 | ||
1472 | end: | |
1473 | return ret; | |
1474 | } | |
1475 | ||
b5443165 PP |
1476 | BT_HIDDEN |
1477 | bt_self_message_iterator_status muxer_msg_iter_seek_beginning( | |
1478 | bt_self_message_iterator *self_msg_iter) | |
1479 | { | |
1480 | struct muxer_msg_iter *muxer_msg_iter = | |
1481 | bt_self_message_iterator_get_data(self_msg_iter); | |
ffb12c4f | 1482 | bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; |
b5443165 PP |
1483 | uint64_t i; |
1484 | ||
54bdc1f7 PP |
1485 | /* Seek all ended upstream iterators first */ |
1486 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1487 | i++) { | |
b5443165 | 1488 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
54bdc1f7 | 1489 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; |
b5443165 PP |
1490 | |
1491 | status = bt_self_component_port_input_message_iterator_seek_beginning( | |
1492 | upstream_msg_iter->msg_iter); | |
1493 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
1494 | goto end; | |
1495 | } | |
54bdc1f7 PP |
1496 | |
1497 | empty_message_queue(upstream_msg_iter); | |
1498 | } | |
1499 | ||
1500 | /* Seek all previously active upstream iterators */ | |
1501 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; | |
1502 | i++) { | |
1503 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1504 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i]; | |
1505 | ||
1506 | status = bt_self_component_port_input_message_iterator_seek_beginning( | |
1507 | upstream_msg_iter->msg_iter); | |
1508 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
1509 | goto end; | |
1510 | } | |
1511 | ||
1512 | empty_message_queue(upstream_msg_iter); | |
1513 | } | |
1514 | ||
1515 | /* Make them all active */ | |
1516 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1517 | i++) { | |
1518 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1519 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; | |
1520 | ||
1521 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, | |
1522 | upstream_msg_iter); | |
1523 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL; | |
b5443165 PP |
1524 | } |
1525 | ||
54bdc1f7 PP |
1526 | g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters, |
1527 | 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len); | |
b5443165 | 1528 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
54bdc1f7 PP |
1529 | muxer_msg_iter->clock_class_expectation = |
1530 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY; | |
b5443165 PP |
1531 | |
1532 | end: | |
ffb12c4f | 1533 | return (bt_self_message_iterator_status) status; |
b5443165 | 1534 | } |