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