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