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 | |
d6e69534 | 433 | bt_self_component_port_input_message_iterator * |
87ec3926 | 434 | create_msg_iter_on_input_port(struct muxer_comp *muxer_comp, |
ca02df0a | 435 | struct muxer_msg_iter *muxer_msg_iter, |
87ec3926 | 436 | bt_self_component_port_input *self_port) |
958f7d11 | 437 | { |
b19ff26f | 438 | const bt_port *port = bt_self_component_port_as_port( |
707b7d35 | 439 | bt_self_component_port_input_as_self_component_port( |
d94d92ac | 440 | self_port)); |
d6e69534 | 441 | bt_self_component_port_input_message_iterator *msg_iter = |
d94d92ac | 442 | NULL; |
958f7d11 | 443 | |
f6ccaed9 PP |
444 | BT_ASSERT(port); |
445 | BT_ASSERT(bt_port_is_connected(port)); | |
958f7d11 | 446 | |
ab11110e | 447 | // TODO: Advance the iterator to >= the time of the latest |
d6e69534 | 448 | // returned message by the muxer message |
ab11110e | 449 | // iterator which creates it. |
ca02df0a PP |
450 | msg_iter = bt_self_component_port_input_message_iterator_create_from_message_iterator( |
451 | muxer_msg_iter->self_msg_iter, self_port); | |
d6e69534 | 452 | if (!msg_iter) { |
5b6473ec | 453 | BT_COMP_LOGE("Cannot create upstream message iterator on input port: " |
d94d92ac PP |
454 | "port-addr=%p, port-name=\"%s\"", |
455 | port, bt_port_get_name(port)); | |
958f7d11 PP |
456 | goto end; |
457 | } | |
458 | ||
5b6473ec | 459 | BT_COMP_LOGI("Created upstream message iterator on input port: " |
d6e69534 PP |
460 | "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p", |
461 | port, bt_port_get_name(port), msg_iter); | |
fed72692 | 462 | |
958f7d11 | 463 | end: |
d6e69534 | 464 | return msg_iter; |
958f7d11 PP |
465 | } |
466 | ||
ab11110e | 467 | static |
d24d5663 | 468 | bt_component_class_message_iterator_next_method_status muxer_upstream_msg_iter_next( |
54bdc1f7 PP |
469 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
470 | bool *is_ended) | |
ab11110e | 471 | { |
87ec3926 PP |
472 | struct muxer_comp *muxer_comp = |
473 | muxer_upstream_msg_iter->muxer_comp; | |
d24d5663 PP |
474 | bt_component_class_message_iterator_next_method_status status; |
475 | bt_message_iterator_next_status input_port_iter_status; | |
d6e69534 | 476 | bt_message_array_const msgs; |
d4393e08 PP |
477 | uint64_t i; |
478 | uint64_t count; | |
ab11110e | 479 | |
5b6473ec | 480 | BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: " |
d6e69534 PP |
481 | "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p", |
482 | muxer_upstream_msg_iter, | |
483 | muxer_upstream_msg_iter->msg_iter); | |
fdf0b89e | 484 | input_port_iter_status = bt_self_component_port_input_message_iterator_next( |
d6e69534 | 485 | muxer_upstream_msg_iter->msg_iter, &msgs, &count); |
5b6473ec | 486 | BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: " |
d24d5663 PP |
487 | "status=%s", |
488 | bt_common_func_status_string(input_port_iter_status)); | |
ab11110e | 489 | |
fdf0b89e | 490 | switch (input_port_iter_status) { |
d24d5663 | 491 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: |
089717de | 492 | /* |
d6e69534 | 493 | * Message iterator's current message is |
d4393e08 | 494 | * valid: it must be considered for muxing operations. |
089717de | 495 | */ |
5b6473ec | 496 | BT_COMP_LOGD_STR("Validated upstream message iterator wrapper."); |
d4393e08 PP |
497 | BT_ASSERT(count > 0); |
498 | ||
d6e69534 | 499 | /* Move messages to our queue */ |
d4393e08 PP |
500 | for (i = 0; i < count; i++) { |
501 | /* | |
502 | * Push to tail in order; other side | |
d6e69534 | 503 | * (muxer_msg_iter_do_next_one()) consumes |
d4393e08 PP |
504 | * from the head first. |
505 | */ | |
d6e69534 PP |
506 | g_queue_push_tail(muxer_upstream_msg_iter->msgs, |
507 | (void *) msgs[i]); | |
d4393e08 | 508 | } |
d24d5663 | 509 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; |
ab11110e | 510 | break; |
d24d5663 | 511 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
089717de | 512 | /* |
d6e69534 | 513 | * Message iterator's current message is not |
089717de | 514 | * valid anymore. Return |
d24d5663 | 515 | * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately. |
089717de | 516 | */ |
d24d5663 | 517 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN; |
ab11110e | 518 | break; |
d24d5663 | 519 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: /* Fall-through. */ |
ab11110e | 520 | /* |
d6e69534 | 521 | * Message iterator reached the end: release it. It |
ab11110e | 522 | * won't be considered again to find the youngest |
d6e69534 | 523 | * message. |
ab11110e | 524 | */ |
54bdc1f7 | 525 | *is_ended = true; |
d24d5663 | 526 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; |
089717de | 527 | break; |
ab11110e PP |
528 | default: |
529 | /* Error or unsupported status code */ | |
5b6473ec | 530 | BT_COMP_LOGE("Error or unsupported status code: " |
fdf0b89e | 531 | "status-code=%d", input_port_iter_status); |
d24d5663 | 532 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
089717de | 533 | break; |
ab11110e PP |
534 | } |
535 | ||
089717de | 536 | return status; |
ab11110e PP |
537 | } |
538 | ||
958f7d11 | 539 | static |
d6e69534 PP |
540 | int get_msg_ts_ns(struct muxer_comp *muxer_comp, |
541 | struct muxer_msg_iter *muxer_msg_iter, | |
542 | const bt_message *msg, int64_t last_returned_ts_ns, | |
958f7d11 PP |
543 | int64_t *ts_ns) |
544 | { | |
605e1019 | 545 | const bt_clock_snapshot *clock_snapshot = NULL; |
958f7d11 | 546 | int ret = 0; |
649934d2 PP |
547 | const bt_stream_class *stream_class = NULL; |
548 | bt_message_type msg_type; | |
958f7d11 | 549 | |
d6e69534 | 550 | BT_ASSERT(msg); |
f6ccaed9 | 551 | BT_ASSERT(ts_ns); |
5b6473ec | 552 | BT_COMP_LOGD("Getting message's timestamp: " |
d6e69534 | 553 | "muxer-msg-iter-addr=%p, msg-addr=%p, " |
fed72692 | 554 | "last-returned-ts=%" PRId64, |
d6e69534 | 555 | muxer_msg_iter, msg, last_returned_ts_ns); |
fed72692 | 556 | |
91d81473 | 557 | if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation == |
60f3d027 PP |
558 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) { |
559 | *ts_ns = last_returned_ts_ns; | |
560 | goto end; | |
561 | } | |
562 | ||
649934d2 PP |
563 | msg_type = bt_message_get_type(msg); |
564 | ||
91d81473 | 565 | if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) { |
649934d2 PP |
566 | stream_class = bt_stream_borrow_class_const( |
567 | bt_packet_borrow_stream_const( | |
568 | bt_message_packet_beginning_borrow_packet_const( | |
569 | msg))); | |
91d81473 | 570 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) { |
649934d2 PP |
571 | stream_class = bt_stream_borrow_class_const( |
572 | bt_packet_borrow_stream_const( | |
573 | bt_message_packet_end_borrow_packet_const( | |
574 | msg))); | |
91d81473 | 575 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) { |
2e90378a PP |
576 | stream_class = bt_stream_borrow_class_const( |
577 | bt_message_discarded_events_borrow_stream_const(msg)); | |
91d81473 | 578 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) { |
2e90378a PP |
579 | stream_class = bt_stream_borrow_class_const( |
580 | bt_message_discarded_packets_borrow_stream_const(msg)); | |
649934d2 PP |
581 | } |
582 | ||
583 | switch (msg_type) { | |
d6e69534 | 584 | case BT_MESSAGE_TYPE_EVENT: |
60f3d027 PP |
585 | BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const( |
586 | msg)); | |
0cbc2c33 PP |
587 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
588 | msg); | |
958f7d11 | 589 | break; |
5366eb53 | 590 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
9b24b6aa | 591 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot( |
649934d2 PP |
592 | stream_class)) { |
593 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const( | |
594 | msg); | |
595 | } else { | |
596 | goto no_clock_snapshot; | |
597 | } | |
598 | ||
5366eb53 PP |
599 | break; |
600 | case BT_MESSAGE_TYPE_PACKET_END: | |
9b24b6aa | 601 | if (bt_stream_class_packets_have_end_default_clock_snapshot( |
649934d2 PP |
602 | stream_class)) { |
603 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const( | |
604 | msg); | |
605 | } else { | |
606 | goto no_clock_snapshot; | |
607 | } | |
608 | ||
5366eb53 | 609 | break; |
c47138bf FD |
610 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
611 | { | |
612 | enum bt_message_stream_clock_snapshot_state snapshot_state = | |
613 | bt_message_stream_beginning_borrow_default_clock_snapshot_const( | |
614 | msg, &clock_snapshot); | |
615 | if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) { | |
616 | goto no_clock_snapshot; | |
617 | } | |
618 | ||
619 | break; | |
620 | } | |
621 | case BT_MESSAGE_TYPE_STREAM_END: | |
622 | { | |
623 | enum bt_message_stream_clock_snapshot_state snapshot_state = | |
624 | bt_message_stream_end_borrow_default_clock_snapshot_const( | |
625 | msg, &clock_snapshot); | |
626 | if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) { | |
627 | goto no_clock_snapshot; | |
628 | } | |
629 | ||
630 | break; | |
631 | } | |
5366eb53 | 632 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
2e90378a PP |
633 | if (bt_stream_class_discarded_events_have_default_clock_snapshots( |
634 | stream_class)) { | |
9b24b6aa | 635 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
2e90378a PP |
636 | msg); |
637 | } else { | |
638 | goto no_clock_snapshot; | |
639 | } | |
640 | ||
5366eb53 PP |
641 | break; |
642 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
2e90378a PP |
643 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots( |
644 | stream_class)) { | |
9b24b6aa | 645 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
2e90378a PP |
646 | msg); |
647 | } else { | |
648 | goto no_clock_snapshot; | |
649 | } | |
650 | ||
5366eb53 | 651 | break; |
b9fd9cbb | 652 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
0cbc2c33 PP |
653 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
654 | msg); | |
958f7d11 PP |
655 | break; |
656 | default: | |
d6e69534 | 657 | /* All the other messages have a higher priority */ |
5b6473ec | 658 | BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp."); |
958f7d11 PP |
659 | *ts_ns = last_returned_ts_ns; |
660 | goto end; | |
661 | } | |
662 | ||
60f3d027 PP |
663 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
664 | if (ret) { | |
5b6473ec | 665 | BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: " |
60f3d027 PP |
666 | "clock-snapshot-addr=%p", clock_snapshot); |
667 | goto error; | |
668 | } | |
669 | ||
670 | goto end; | |
671 | ||
672 | no_clock_snapshot: | |
5b6473ec | 673 | BT_COMP_LOGD_STR("Message's default clock snapshot is missing: " |
60f3d027 PP |
674 | "using the last returned timestamp."); |
675 | *ts_ns = last_returned_ts_ns; | |
676 | goto end; | |
677 | ||
678 | error: | |
679 | ret = -1; | |
680 | ||
681 | end: | |
682 | if (ret == 0) { | |
5b6473ec | 683 | BT_COMP_LOGD("Found message's timestamp: " |
60f3d027 PP |
684 | "muxer-msg-iter-addr=%p, msg-addr=%p, " |
685 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, | |
686 | muxer_msg_iter, msg, last_returned_ts_ns, | |
687 | *ts_ns); | |
44c440bc PP |
688 | } |
689 | ||
60f3d027 PP |
690 | return ret; |
691 | } | |
692 | ||
693 | static inline | |
694 | int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter, | |
695 | struct muxer_comp *muxer_comp, | |
696 | const bt_clock_class *clock_class) | |
697 | { | |
698 | int ret = 0; | |
6162e6b7 | 699 | const uint8_t *cc_uuid; |
60f3d027 PP |
700 | const char *cc_name; |
701 | ||
e22b45d0 | 702 | BT_ASSERT(clock_class); |
50842bdc PP |
703 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
704 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 | 705 | |
d6e69534 PP |
706 | if (muxer_msg_iter->clock_class_expectation == |
707 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
282c8cd0 PP |
708 | /* |
709 | * This is the first clock class that this muxer | |
d6e69534 | 710 | * message iterator encounters. Its properties |
282c8cd0 PP |
711 | * determine what to expect for the whole lifetime of |
712 | * the iterator without a true | |
713 | * `assume-absolute-clock-classes` parameter. | |
714 | */ | |
5552377a | 715 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
282c8cd0 | 716 | /* Expect absolute clock classes */ |
d6e69534 PP |
717 | muxer_msg_iter->clock_class_expectation = |
718 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
282c8cd0 PP |
719 | } else { |
720 | if (cc_uuid) { | |
721 | /* | |
722 | * Expect non-absolute clock classes | |
723 | * with a specific UUID. | |
724 | */ | |
d6e69534 PP |
725 | muxer_msg_iter->clock_class_expectation = |
726 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; | |
6162e6b7 | 727 | bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid); |
282c8cd0 PP |
728 | } else { |
729 | /* | |
730 | * Expect non-absolute clock classes | |
731 | * with no UUID. | |
732 | */ | |
d6e69534 PP |
733 | muxer_msg_iter->clock_class_expectation = |
734 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
282c8cd0 PP |
735 | } |
736 | } | |
737 | } | |
738 | ||
739 | if (!muxer_comp->assume_absolute_clock_classes) { | |
d6e69534 PP |
740 | switch (muxer_msg_iter->clock_class_expectation) { |
741 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
5552377a | 742 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { |
5b6473ec | 743 | BT_COMP_LOGE("Expecting an absolute clock class, " |
fed72692 PP |
744 | "but got a non-absolute one: " |
745 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
746 | clock_class, cc_name); | |
282c8cd0 PP |
747 | goto error; |
748 | } | |
749 | break; | |
d6e69534 | 750 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: |
5552377a | 751 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
5b6473ec | 752 | BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, " |
fed72692 PP |
753 | "but got an absolute one: " |
754 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
755 | clock_class, cc_name); | |
282c8cd0 PP |
756 | goto error; |
757 | } | |
758 | ||
759 | if (cc_uuid) { | |
5b6473ec | 760 | BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, " |
fed72692 PP |
761 | "but got one with a UUID: " |
762 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
6162e6b7 MJ |
763 | "uuid=\"" BT_UUID_FMT "\"", |
764 | clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid)); | |
282c8cd0 PP |
765 | goto error; |
766 | } | |
767 | break; | |
d6e69534 | 768 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: |
5552377a | 769 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
5b6473ec | 770 | BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
fed72692 PP |
771 | "but got an absolute one: " |
772 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
773 | clock_class, cc_name); | |
282c8cd0 PP |
774 | goto error; |
775 | } | |
776 | ||
777 | if (!cc_uuid) { | |
5b6473ec | 778 | BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
fed72692 PP |
779 | "but got one with no UUID: " |
780 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
781 | clock_class, cc_name); | |
282c8cd0 PP |
782 | goto error; |
783 | } | |
784 | ||
6162e6b7 | 785 | if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) { |
5b6473ec | 786 | BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
fed72692 PP |
787 | "but got one with different UUID: " |
788 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
6162e6b7 MJ |
789 | "expected-uuid=\"" BT_UUID_FMT "\", " |
790 | "uuid=\"" BT_UUID_FMT "\"", | |
fed72692 | 791 | clock_class, cc_name, |
6162e6b7 MJ |
792 | BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid), |
793 | BT_UUID_FMT_VALUES(cc_uuid)); | |
282c8cd0 PP |
794 | goto error; |
795 | } | |
796 | break; | |
60f3d027 | 797 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE: |
5b6473ec | 798 | BT_COMP_LOGE("Expecting no clock class, but got one: " |
60f3d027 PP |
799 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
800 | clock_class, cc_name); | |
801 | goto error; | |
282c8cd0 PP |
802 | default: |
803 | /* Unexpected */ | |
5b6473ec | 804 | BT_COMP_LOGF("Unexpected clock class expectation: " |
fed72692 | 805 | "expectation-code=%d", |
d6e69534 | 806 | muxer_msg_iter->clock_class_expectation); |
282c8cd0 PP |
807 | abort(); |
808 | } | |
958f7d11 PP |
809 | } |
810 | ||
4c0f1c8c PP |
811 | goto end; |
812 | ||
958f7d11 PP |
813 | error: |
814 | ret = -1; | |
815 | ||
816 | end: | |
60f3d027 PP |
817 | return ret; |
818 | } | |
819 | ||
820 | static inline | |
821 | int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter, | |
822 | struct muxer_comp *muxer_comp, const bt_stream *stream) | |
823 | { | |
824 | int ret = 0; | |
825 | const bt_stream_class *stream_class = | |
826 | bt_stream_borrow_class_const(stream); | |
827 | const bt_clock_class *clock_class = | |
828 | bt_stream_class_borrow_default_clock_class_const(stream_class); | |
829 | ||
830 | if (!clock_class) { | |
831 | if (muxer_msg_iter->clock_class_expectation == | |
832 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
833 | /* Expect no clock class */ | |
834 | muxer_msg_iter->clock_class_expectation = | |
835 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE; | |
836 | } else { | |
5b6473ec | 837 | BT_COMP_LOGE("Expecting stream class with a default clock class: " |
60f3d027 PP |
838 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
839 | "stream-class-id=%" PRIu64, | |
840 | stream_class, bt_stream_class_get_name(stream_class), | |
841 | bt_stream_class_get_id(stream_class)); | |
842 | ret = -1; | |
843 | } | |
844 | ||
845 | goto end; | |
fed72692 PP |
846 | } |
847 | ||
60f3d027 PP |
848 | ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class); |
849 | ||
850 | end: | |
958f7d11 PP |
851 | return ret; |
852 | } | |
853 | ||
ab11110e | 854 | /* |
d6e69534 PP |
855 | * This function finds the youngest available message amongst the |
856 | * non-ended upstream message iterators and returns the upstream | |
857 | * message iterator which has it, or | |
858 | * BT_MESSAGE_ITERATOR_STATUS_END if there's no available | |
859 | * message. | |
ab11110e PP |
860 | * |
861 | * This function does NOT: | |
862 | * | |
d6e69534 | 863 | * * Update any upstream message iterator. |
d6e69534 | 864 | * * Check the upstream message iterators to retry. |
ab11110e | 865 | * |
d6e69534 PP |
866 | * On sucess, this function sets *muxer_upstream_msg_iter to the |
867 | * upstream message iterator of which the current message is | |
ab11110e PP |
868 | * the youngest, and sets *ts_ns to its time. |
869 | */ | |
958f7d11 | 870 | static |
d24d5663 | 871 | bt_component_class_message_iterator_next_method_status |
d6e69534 | 872 | muxer_msg_iter_youngest_upstream_msg_iter( |
958f7d11 | 873 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
874 | struct muxer_msg_iter *muxer_msg_iter, |
875 | struct muxer_upstream_msg_iter **muxer_upstream_msg_iter, | |
958f7d11 PP |
876 | int64_t *ts_ns) |
877 | { | |
878 | size_t i; | |
879 | int ret; | |
880 | int64_t youngest_ts_ns = INT64_MAX; | |
d24d5663 PP |
881 | bt_component_class_message_iterator_next_method_status status = |
882 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
958f7d11 | 883 | |
f6ccaed9 | 884 | BT_ASSERT(muxer_comp); |
d6e69534 PP |
885 | BT_ASSERT(muxer_msg_iter); |
886 | BT_ASSERT(muxer_upstream_msg_iter); | |
887 | *muxer_upstream_msg_iter = NULL; | |
888 | ||
54bdc1f7 PP |
889 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
890 | i++) { | |
d6e69534 PP |
891 | const bt_message *msg; |
892 | struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter = | |
54bdc1f7 PP |
893 | g_ptr_array_index( |
894 | muxer_msg_iter->active_muxer_upstream_msg_iters, | |
895 | i); | |
d6e69534 PP |
896 | int64_t msg_ts_ns; |
897 | ||
898 | if (!cur_muxer_upstream_msg_iter->msg_iter) { | |
899 | /* This upstream message iterator is ended */ | |
ef267d12 | 900 | BT_COMP_LOGT("Skipping ended upstream message iterator: " |
d6e69534 PP |
901 | "muxer-upstream-msg-iter-wrap-addr=%p", |
902 | cur_muxer_upstream_msg_iter); | |
958f7d11 PP |
903 | continue; |
904 | } | |
905 | ||
d6e69534 PP |
906 | BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0); |
907 | msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs); | |
908 | BT_ASSERT(msg); | |
60f3d027 | 909 | |
91d81473 | 910 | if (G_UNLIKELY(bt_message_get_type(msg) == |
60f3d027 PP |
911 | BT_MESSAGE_TYPE_STREAM_BEGINNING)) { |
912 | ret = validate_new_stream_clock_class( | |
913 | muxer_msg_iter, muxer_comp, | |
914 | bt_message_stream_beginning_borrow_stream_const( | |
915 | msg)); | |
916 | if (ret) { | |
917 | /* | |
918 | * validate_new_stream_clock_class() logs | |
919 | * errors. | |
920 | */ | |
d24d5663 | 921 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
60f3d027 PP |
922 | goto end; |
923 | } | |
91d81473 | 924 | } else if (G_UNLIKELY(bt_message_get_type(msg) == |
60f3d027 PP |
925 | BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) { |
926 | const bt_clock_snapshot *cs; | |
60f3d027 | 927 | |
0cbc2c33 PP |
928 | cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
929 | msg); | |
60f3d027 PP |
930 | ret = validate_clock_class(muxer_msg_iter, muxer_comp, |
931 | bt_clock_snapshot_borrow_clock_class_const(cs)); | |
932 | if (ret) { | |
933 | /* validate_clock_class() logs errors */ | |
d24d5663 | 934 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
60f3d027 PP |
935 | goto end; |
936 | } | |
937 | } | |
938 | ||
d6e69534 PP |
939 | ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg, |
940 | muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns); | |
958f7d11 | 941 | if (ret) { |
d6e69534 PP |
942 | /* get_msg_ts_ns() logs errors */ |
943 | *muxer_upstream_msg_iter = NULL; | |
d24d5663 | 944 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
958f7d11 PP |
945 | goto end; |
946 | } | |
947 | ||
6915f47a | 948 | if (msg_ts_ns < youngest_ts_ns) { |
d6e69534 PP |
949 | *muxer_upstream_msg_iter = |
950 | cur_muxer_upstream_msg_iter; | |
951 | youngest_ts_ns = msg_ts_ns; | |
958f7d11 | 952 | *ts_ns = youngest_ts_ns; |
6915f47a FD |
953 | } else if (msg_ts_ns == youngest_ts_ns) { |
954 | /* | |
955 | * The currently selected message to be sent downstream | |
956 | * next has the exact same timestamp that of the | |
957 | * current candidate message. We must break the tie | |
958 | * in a predictable manner. | |
959 | */ | |
960 | const bt_message *selected_msg = g_queue_peek_head( | |
961 | (*muxer_upstream_msg_iter)->msgs); | |
962 | BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically."); | |
963 | ||
964 | /* | |
965 | * Order the messages in an arbitrary but determinitic | |
966 | * way. | |
967 | */ | |
1aca5200 | 968 | ret = common_muxing_compare_messages(msg, selected_msg); |
6915f47a FD |
969 | if (ret < 0) { |
970 | /* | |
971 | * The `msg` should go first. Update the next | |
972 | * iterator and the current timestamp. | |
973 | */ | |
974 | *muxer_upstream_msg_iter = | |
975 | cur_muxer_upstream_msg_iter; | |
976 | youngest_ts_ns = msg_ts_ns; | |
977 | *ts_ns = youngest_ts_ns; | |
978 | } else if (ret == 0) { | |
979 | /* Unable to pick which one should go first. */ | |
980 | BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: " | |
981 | "muxer-upstream-msg-iter-wrap-addr=%p" | |
982 | "cur-muxer-upstream-msg-iter-wrap-addr=%p", | |
983 | *muxer_upstream_msg_iter, | |
984 | cur_muxer_upstream_msg_iter); | |
985 | } | |
958f7d11 PP |
986 | } |
987 | } | |
988 | ||
d6e69534 | 989 | if (!*muxer_upstream_msg_iter) { |
d24d5663 | 990 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
958f7d11 PP |
991 | *ts_ns = INT64_MIN; |
992 | } | |
993 | ||
994 | end: | |
995 | return status; | |
996 | } | |
997 | ||
998 | static | |
d24d5663 PP |
999 | bt_component_class_message_iterator_next_method_status |
1000 | validate_muxer_upstream_msg_iter( | |
54bdc1f7 PP |
1001 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
1002 | bool *is_ended) | |
958f7d11 | 1003 | { |
87ec3926 PP |
1004 | struct muxer_comp *muxer_comp = |
1005 | muxer_upstream_msg_iter->muxer_comp; | |
d24d5663 PP |
1006 | bt_component_class_message_iterator_next_method_status status = |
1007 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
958f7d11 | 1008 | |
5b6473ec | 1009 | BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: " |
d6e69534 PP |
1010 | "muxer-upstream-msg-iter-wrap-addr=%p", |
1011 | muxer_upstream_msg_iter); | |
fed72692 | 1012 | |
d6e69534 PP |
1013 | if (muxer_upstream_msg_iter->msgs->length > 0 || |
1014 | !muxer_upstream_msg_iter->msg_iter) { | |
5b6473ec | 1015 | BT_COMP_LOGD("Already valid or not considered: " |
d6e69534 PP |
1016 | "queue-len=%u, upstream-msg-iter-addr=%p", |
1017 | muxer_upstream_msg_iter->msgs->length, | |
1018 | muxer_upstream_msg_iter->msg_iter); | |
ab11110e PP |
1019 | goto end; |
1020 | } | |
1021 | ||
d6e69534 | 1022 | /* muxer_upstream_msg_iter_next() logs details/errors */ |
54bdc1f7 PP |
1023 | status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter, |
1024 | is_ended); | |
089717de PP |
1025 | |
1026 | end: | |
1027 | return status; | |
1028 | } | |
1029 | ||
1030 | static | |
d24d5663 PP |
1031 | bt_component_class_message_iterator_next_method_status |
1032 | validate_muxer_upstream_msg_iters( | |
d6e69534 | 1033 | struct muxer_msg_iter *muxer_msg_iter) |
089717de | 1034 | { |
87ec3926 | 1035 | struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp; |
d24d5663 PP |
1036 | bt_component_class_message_iterator_next_method_status status = |
1037 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
089717de PP |
1038 | size_t i; |
1039 | ||
5b6473ec | 1040 | BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: " |
d6e69534 | 1041 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
fed72692 | 1042 | |
54bdc1f7 PP |
1043 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
1044 | i++) { | |
6bf2abbd | 1045 | bool is_ended = false; |
d6e69534 | 1046 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
089717de | 1047 | g_ptr_array_index( |
54bdc1f7 | 1048 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
089717de PP |
1049 | i); |
1050 | ||
d6e69534 | 1051 | status = validate_muxer_upstream_msg_iter( |
54bdc1f7 | 1052 | muxer_upstream_msg_iter, &is_ended); |
d24d5663 | 1053 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
fed72692 | 1054 | if (status < 0) { |
5b6473ec | 1055 | BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: " |
d6e69534 PP |
1056 | "muxer-msg-iter-addr=%p, " |
1057 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1058 | muxer_msg_iter, | |
1059 | muxer_upstream_msg_iter); | |
fed72692 | 1060 | } else { |
5b6473ec | 1061 | BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: " |
d6e69534 PP |
1062 | "muxer-msg-iter-addr=%p, " |
1063 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1064 | muxer_msg_iter, | |
1065 | muxer_upstream_msg_iter); | |
fed72692 PP |
1066 | } |
1067 | ||
089717de PP |
1068 | goto end; |
1069 | } | |
744ba28b PP |
1070 | |
1071 | /* | |
54bdc1f7 PP |
1072 | * Move this muxer upstream message iterator to the |
1073 | * array of ended iterators if it's ended. | |
744ba28b | 1074 | */ |
91d81473 | 1075 | if (G_UNLIKELY(is_ended)) { |
5b6473ec | 1076 | BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: " |
54bdc1f7 PP |
1077 | "muxer-msg-iter-addr=%p, " |
1078 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1079 | muxer_msg_iter, muxer_upstream_msg_iter); | |
1080 | g_ptr_array_add( | |
1081 | muxer_msg_iter->ended_muxer_upstream_msg_iters, | |
1082 | muxer_upstream_msg_iter); | |
1083 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL; | |
1084 | ||
744ba28b PP |
1085 | /* |
1086 | * Use g_ptr_array_remove_fast() because the | |
1087 | * order of those elements is not important. | |
1088 | */ | |
1089 | g_ptr_array_remove_index_fast( | |
54bdc1f7 | 1090 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
744ba28b PP |
1091 | i); |
1092 | i--; | |
1093 | } | |
089717de PP |
1094 | } |
1095 | ||
1096 | end: | |
1097 | return status; | |
1098 | } | |
1099 | ||
d4393e08 | 1100 | static inline |
d24d5663 | 1101 | bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next_one( |
089717de | 1102 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
1103 | struct muxer_msg_iter *muxer_msg_iter, |
1104 | const bt_message **msg) | |
089717de | 1105 | { |
d24d5663 | 1106 | bt_component_class_message_iterator_next_method_status status; |
d6e69534 | 1107 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL; |
089717de PP |
1108 | int64_t next_return_ts; |
1109 | ||
5badd463 | 1110 | status = validate_muxer_upstream_msg_iters(muxer_msg_iter); |
d24d5663 | 1111 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
5badd463 PP |
1112 | /* validate_muxer_upstream_msg_iters() logs details */ |
1113 | goto end; | |
958f7d11 PP |
1114 | } |
1115 | ||
1116 | /* | |
089717de | 1117 | * At this point we know that all the existing upstream |
d6e69534 PP |
1118 | * message iterators are valid. We can find the one, |
1119 | * amongst those, of which the current message is the | |
089717de | 1120 | * youngest. |
958f7d11 | 1121 | */ |
d6e69534 PP |
1122 | status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp, |
1123 | muxer_msg_iter, &muxer_upstream_msg_iter, | |
089717de | 1124 | &next_return_ts); |
d24d5663 | 1125 | if (status < 0 || status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) { |
d4393e08 | 1126 | if (status < 0) { |
5b6473ec | 1127 | BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1128 | "status=%s", |
d24d5663 | 1129 | bt_common_func_status_string(status)); |
fed72692 | 1130 | } else { |
5b6473ec | 1131 | BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1132 | "status=%s", |
d24d5663 | 1133 | bt_common_func_status_string(status)); |
fed72692 PP |
1134 | } |
1135 | ||
958f7d11 PP |
1136 | goto end; |
1137 | } | |
1138 | ||
d6e69534 | 1139 | if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) { |
5b6473ec | 1140 | BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: " |
d6e69534 | 1141 | "muxer-msg-iter-addr=%p, ts=%" PRId64 ", " |
fed72692 | 1142 | "last-returned-ts=%" PRId64, |
d6e69534 PP |
1143 | muxer_msg_iter, next_return_ts, |
1144 | muxer_msg_iter->last_returned_ts_ns); | |
d24d5663 | 1145 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
958f7d11 PP |
1146 | goto end; |
1147 | } | |
1148 | ||
5b6473ec | 1149 | BT_COMP_LOGD("Found youngest upstream message iterator wrapper: " |
d6e69534 PP |
1150 | "muxer-msg-iter-addr=%p, " |
1151 | "muxer-upstream-msg-iter-wrap-addr=%p, " | |
fed72692 | 1152 | "ts=%" PRId64, |
d6e69534 | 1153 | muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts); |
d24d5663 | 1154 | BT_ASSERT(status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK); |
d6e69534 | 1155 | BT_ASSERT(muxer_upstream_msg_iter); |
958f7d11 PP |
1156 | |
1157 | /* | |
d4393e08 | 1158 | * Consume from the queue's head: other side |
d6e69534 | 1159 | * (muxer_upstream_msg_iter_next()) writes to the tail. |
958f7d11 | 1160 | */ |
d6e69534 PP |
1161 | *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs); |
1162 | BT_ASSERT(*msg); | |
1163 | muxer_msg_iter->last_returned_ts_ns = next_return_ts; | |
958f7d11 PP |
1164 | |
1165 | end: | |
d4393e08 PP |
1166 | return status; |
1167 | } | |
1168 | ||
1169 | static | |
d24d5663 | 1170 | bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next( |
d4393e08 | 1171 | struct muxer_comp *muxer_comp, |
d6e69534 PP |
1172 | struct muxer_msg_iter *muxer_msg_iter, |
1173 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
1174 | uint64_t *count) |
1175 | { | |
d24d5663 PP |
1176 | bt_component_class_message_iterator_next_method_status status = |
1177 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
d4393e08 PP |
1178 | uint64_t i = 0; |
1179 | ||
d24d5663 | 1180 | while (i < capacity && status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
d6e69534 PP |
1181 | status = muxer_msg_iter_do_next_one(muxer_comp, |
1182 | muxer_msg_iter, &msgs[i]); | |
d24d5663 | 1183 | if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
d4393e08 PP |
1184 | i++; |
1185 | } | |
1186 | } | |
1187 | ||
1188 | if (i > 0) { | |
1189 | /* | |
d6e69534 | 1190 | * Even if muxer_msg_iter_do_next_one() returned |
d4393e08 | 1191 | * something else than |
d6e69534 PP |
1192 | * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated |
1193 | * message objects in the output message | |
d4393e08 | 1194 | * array, so we need to return |
d6e69534 | 1195 | * BT_MESSAGE_ITERATOR_STATUS_OK so that they are |
d4393e08 | 1196 | * transfered to downstream. This other status occurs |
d6e69534 | 1197 | * again the next time muxer_msg_iter_do_next() is |
d4393e08 | 1198 | * called, possibly without any accumulated |
d6e69534 | 1199 | * message, in which case we'll return it. |
d4393e08 PP |
1200 | */ |
1201 | *count = i; | |
d24d5663 | 1202 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; |
d4393e08 PP |
1203 | } |
1204 | ||
1205 | return status; | |
958f7d11 PP |
1206 | } |
1207 | ||
1208 | static | |
d6e69534 | 1209 | void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter) |
ab11110e | 1210 | { |
87ec3926 PP |
1211 | struct muxer_comp *muxer_comp; |
1212 | ||
d6e69534 | 1213 | if (!muxer_msg_iter) { |
ab11110e PP |
1214 | return; |
1215 | } | |
1216 | ||
87ec3926 | 1217 | muxer_comp = muxer_msg_iter->muxer_comp; |
5b6473ec | 1218 | BT_COMP_LOGD("Destroying muxer component's message iterator: " |
d6e69534 | 1219 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
fed72692 | 1220 | |
54bdc1f7 | 1221 | if (muxer_msg_iter->active_muxer_upstream_msg_iters) { |
5b6473ec | 1222 | BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers."); |
54bdc1f7 PP |
1223 | g_ptr_array_free( |
1224 | muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE); | |
1225 | } | |
1226 | ||
1227 | if (muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
5b6473ec | 1228 | BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers."); |
ab11110e | 1229 | g_ptr_array_free( |
54bdc1f7 | 1230 | muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE); |
ab11110e PP |
1231 | } |
1232 | ||
d6e69534 | 1233 | g_free(muxer_msg_iter); |
ab11110e PP |
1234 | } |
1235 | ||
1236 | static | |
5badd463 | 1237 | int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp, |
d6e69534 | 1238 | struct muxer_msg_iter *muxer_msg_iter) |
958f7d11 | 1239 | { |
544d0515 PP |
1240 | int64_t count; |
1241 | int64_t i; | |
ab11110e | 1242 | int ret = 0; |
958f7d11 | 1243 | |
d94d92ac | 1244 | count = bt_component_filter_get_input_port_count( |
707b7d35 | 1245 | bt_self_component_filter_as_component_filter( |
5b6473ec | 1246 | muxer_comp->self_comp_flt)); |
544d0515 | 1247 | if (count < 0) { |
5b6473ec | 1248 | BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: " |
d6e69534 PP |
1249 | "muxer-comp-addr=%p, muxer-msg-iter-addr=%p", |
1250 | muxer_comp, muxer_msg_iter); | |
ab11110e PP |
1251 | goto end; |
1252 | } | |
958f7d11 PP |
1253 | |
1254 | for (i = 0; i < count; i++) { | |
5badd463 | 1255 | bt_self_component_port_input_message_iterator *upstream_msg_iter; |
b19ff26f | 1256 | bt_self_component_port_input *self_port = |
d94d92ac | 1257 | bt_self_component_filter_borrow_input_port_by_index( |
5b6473ec | 1258 | muxer_comp->self_comp_flt, i); |
b19ff26f | 1259 | const bt_port *port; |
958f7d11 | 1260 | |
d94d92ac | 1261 | BT_ASSERT(self_port); |
707b7d35 PP |
1262 | port = bt_self_component_port_as_port( |
1263 | bt_self_component_port_input_as_self_component_port( | |
d94d92ac | 1264 | self_port)); |
f6ccaed9 | 1265 | BT_ASSERT(port); |
958f7d11 PP |
1266 | |
1267 | if (!bt_port_is_connected(port)) { | |
5badd463 | 1268 | /* Skip non-connected port */ |
958f7d11 PP |
1269 | continue; |
1270 | } | |
1271 | ||
87ec3926 | 1272 | upstream_msg_iter = create_msg_iter_on_input_port(muxer_comp, |
ca02df0a | 1273 | muxer_msg_iter, self_port); |
5badd463 PP |
1274 | if (!upstream_msg_iter) { |
1275 | /* create_msg_iter_on_input_port() logs errors */ | |
1276 | BT_ASSERT(!upstream_msg_iter); | |
ab11110e PP |
1277 | ret = -1; |
1278 | goto end; | |
958f7d11 | 1279 | } |
fed72692 | 1280 | |
c61018b9 FD |
1281 | ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter, |
1282 | upstream_msg_iter); | |
5badd463 PP |
1283 | bt_self_component_port_input_message_iterator_put_ref( |
1284 | upstream_msg_iter); | |
c61018b9 | 1285 | if (ret) { |
5badd463 | 1286 | /* muxer_msg_iter_add_upstream_msg_iter() logs errors */ |
5badd463 PP |
1287 | goto end; |
1288 | } | |
958f7d11 PP |
1289 | } |
1290 | ||
1291 | end: | |
958f7d11 PP |
1292 | return ret; |
1293 | } | |
1294 | ||
958f7d11 | 1295 | BT_HIDDEN |
d24d5663 | 1296 | bt_component_class_message_iterator_init_method_status muxer_msg_iter_init( |
d6e69534 | 1297 | bt_self_message_iterator *self_msg_iter, |
b19ff26f PP |
1298 | bt_self_component_filter *self_comp, |
1299 | bt_self_component_port_output *port) | |
958f7d11 PP |
1300 | { |
1301 | struct muxer_comp *muxer_comp = NULL; | |
d6e69534 | 1302 | struct muxer_msg_iter *muxer_msg_iter = NULL; |
d24d5663 PP |
1303 | bt_component_class_message_iterator_init_method_status status = |
1304 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK; | |
958f7d11 PP |
1305 | int ret; |
1306 | ||
d94d92ac | 1307 | muxer_comp = bt_self_component_get_data( |
707b7d35 | 1308 | bt_self_component_filter_as_self_component(self_comp)); |
f6ccaed9 | 1309 | BT_ASSERT(muxer_comp); |
5b6473ec | 1310 | BT_COMP_LOGD("Initializing muxer component's message iterator: " |
d6e69534 PP |
1311 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", |
1312 | self_comp, muxer_comp, self_msg_iter); | |
a09c6b95 | 1313 | |
d6e69534 | 1314 | if (muxer_comp->initializing_muxer_msg_iter) { |
a09c6b95 | 1315 | /* |
089717de | 1316 | * Weird, unhandled situation detected: downstream |
d6e69534 PP |
1317 | * creates a muxer message iterator while creating |
1318 | * another muxer message iterator (same component). | |
a09c6b95 | 1319 | */ |
5b6473ec | 1320 | BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: " |
d6e69534 PP |
1321 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", |
1322 | self_comp, muxer_comp, self_msg_iter); | |
958f7d11 PP |
1323 | goto error; |
1324 | } | |
1325 | ||
d6e69534 PP |
1326 | muxer_comp->initializing_muxer_msg_iter = true; |
1327 | muxer_msg_iter = g_new0(struct muxer_msg_iter, 1); | |
1328 | if (!muxer_msg_iter) { | |
5b6473ec | 1329 | BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator."); |
ab11110e PP |
1330 | goto error; |
1331 | } | |
1332 | ||
87ec3926 | 1333 | muxer_msg_iter->muxer_comp = muxer_comp; |
ca02df0a | 1334 | muxer_msg_iter->self_msg_iter = self_msg_iter; |
d6e69534 | 1335 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
54bdc1f7 | 1336 | muxer_msg_iter->active_muxer_upstream_msg_iters = |
958f7d11 | 1337 | g_ptr_array_new_with_free_func( |
d6e69534 | 1338 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); |
54bdc1f7 | 1339 | if (!muxer_msg_iter->active_muxer_upstream_msg_iters) { |
5b6473ec | 1340 | BT_COMP_LOGE_STR("Failed to allocate a GPtrArray."); |
54bdc1f7 PP |
1341 | goto error; |
1342 | } | |
1343 | ||
1344 | muxer_msg_iter->ended_muxer_upstream_msg_iters = | |
1345 | g_ptr_array_new_with_free_func( | |
1346 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); | |
1347 | if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
5b6473ec | 1348 | BT_COMP_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1349 | goto error; |
1350 | } | |
1351 | ||
5badd463 | 1352 | ret = muxer_msg_iter_init_upstream_iterators(muxer_comp, |
d6e69534 | 1353 | muxer_msg_iter); |
a09c6b95 | 1354 | if (ret) { |
5b6473ec | 1355 | BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: " |
fed72692 | 1356 | "comp-addr=%p, muxer-comp-addr=%p, " |
d6e69534 PP |
1357 | "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d", |
1358 | self_comp, muxer_comp, muxer_msg_iter, | |
1359 | self_msg_iter, ret); | |
a09c6b95 PP |
1360 | goto error; |
1361 | } | |
1362 | ||
5badd463 | 1363 | bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter); |
5b6473ec | 1364 | BT_COMP_LOGD("Initialized muxer component's message iterator: " |
d6e69534 PP |
1365 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
1366 | "msg-iter-addr=%p", | |
1367 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 PP |
1368 | goto end; |
1369 | ||
1370 | error: | |
d6e69534 | 1371 | destroy_muxer_msg_iter(muxer_msg_iter); |
5badd463 | 1372 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
d24d5663 | 1373 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR; |
958f7d11 PP |
1374 | |
1375 | end: | |
d6e69534 | 1376 | muxer_comp->initializing_muxer_msg_iter = false; |
958f7d11 PP |
1377 | return status; |
1378 | } | |
1379 | ||
1380 | BT_HIDDEN | |
54bdc1f7 | 1381 | void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
958f7d11 | 1382 | { |
d6e69534 PP |
1383 | struct muxer_msg_iter *muxer_msg_iter = |
1384 | bt_self_message_iterator_get_data(self_msg_iter); | |
b19ff26f | 1385 | bt_self_component *self_comp = NULL; |
958f7d11 PP |
1386 | struct muxer_comp *muxer_comp = NULL; |
1387 | ||
d6e69534 PP |
1388 | self_comp = bt_self_message_iterator_borrow_component( |
1389 | self_msg_iter); | |
d94d92ac PP |
1390 | BT_ASSERT(self_comp); |
1391 | muxer_comp = bt_self_component_get_data(self_comp); | |
5b6473ec | 1392 | BT_COMP_LOGD("Finalizing muxer component's message iterator: " |
d6e69534 PP |
1393 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
1394 | "msg-iter-addr=%p", | |
1395 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 | 1396 | |
5badd463 | 1397 | if (muxer_msg_iter) { |
d6e69534 | 1398 | destroy_muxer_msg_iter(muxer_msg_iter); |
958f7d11 | 1399 | } |
958f7d11 PP |
1400 | } |
1401 | ||
1402 | BT_HIDDEN | |
d24d5663 | 1403 | bt_component_class_message_iterator_next_method_status muxer_msg_iter_next( |
d6e69534 PP |
1404 | bt_self_message_iterator *self_msg_iter, |
1405 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 | 1406 | uint64_t *count) |
958f7d11 | 1407 | { |
d24d5663 | 1408 | bt_component_class_message_iterator_next_method_status status; |
d6e69534 PP |
1409 | struct muxer_msg_iter *muxer_msg_iter = |
1410 | bt_self_message_iterator_get_data(self_msg_iter); | |
b19ff26f | 1411 | bt_self_component *self_comp = NULL; |
958f7d11 | 1412 | struct muxer_comp *muxer_comp = NULL; |
958f7d11 | 1413 | |
d6e69534 PP |
1414 | BT_ASSERT(muxer_msg_iter); |
1415 | self_comp = bt_self_message_iterator_borrow_component( | |
1416 | self_msg_iter); | |
d94d92ac PP |
1417 | BT_ASSERT(self_comp); |
1418 | muxer_comp = bt_self_component_get_data(self_comp); | |
f6ccaed9 | 1419 | BT_ASSERT(muxer_comp); |
ef267d12 | 1420 | BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: " |
d6e69534 PP |
1421 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
1422 | "msg-iter-addr=%p", | |
1423 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
fed72692 | 1424 | |
d6e69534 PP |
1425 | status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter, |
1426 | msgs, capacity, count); | |
d4393e08 | 1427 | if (status < 0) { |
5b6473ec | 1428 | BT_COMP_LOGE("Cannot get next message: " |
d6e69534 PP |
1429 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
1430 | "msg-iter-addr=%p, status=%s", | |
1431 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter, | |
d24d5663 | 1432 | bt_common_func_status_string(status)); |
fed72692 | 1433 | } else { |
ef267d12 | 1434 | BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: " |
d4393e08 | 1435 | "status=%s", |
d24d5663 | 1436 | bt_common_func_status_string(status)); |
fed72692 | 1437 | } |
958f7d11 | 1438 | |
d4393e08 | 1439 | return status; |
958f7d11 PP |
1440 | } |
1441 | ||
1442 | BT_HIDDEN | |
d24d5663 | 1443 | bt_component_class_port_connected_method_status muxer_input_port_connected( |
b19ff26f PP |
1444 | bt_self_component_filter *self_comp, |
1445 | bt_self_component_port_input *self_port, | |
1446 | const bt_port_output *other_port) | |
958f7d11 | 1447 | { |
d24d5663 PP |
1448 | bt_component_class_port_connected_method_status status = |
1449 | BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; | |
1450 | bt_self_component_add_port_status add_port_status; | |
87ec3926 PP |
1451 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
1452 | bt_self_component_filter_as_self_component(self_comp)); | |
958f7d11 | 1453 | |
d24d5663 PP |
1454 | add_port_status = add_available_input_port(self_comp); |
1455 | if (add_port_status) { | |
5b6473ec | 1456 | BT_COMP_LOGE("Cannot add one muxer component's input port: " |
5badd463 | 1457 | "status=%s", |
d24d5663 PP |
1458 | bt_common_func_status_string(status)); |
1459 | ||
1460 | if (add_port_status == | |
1461 | BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) { | |
1462 | status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR; | |
1463 | } else { | |
1464 | status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; | |
1465 | } | |
1466 | ||
06a2cb0d PP |
1467 | goto end; |
1468 | } | |
1469 | ||
958f7d11 | 1470 | end: |
bf55043c | 1471 | return status; |
958f7d11 | 1472 | } |
b5443165 | 1473 | |
54bdc1f7 PP |
1474 | static inline |
1475 | bt_bool muxer_upstream_msg_iters_can_all_seek_beginning( | |
1476 | GPtrArray *muxer_upstream_msg_iters) | |
b5443165 | 1477 | { |
b5443165 PP |
1478 | uint64_t i; |
1479 | bt_bool ret = BT_TRUE; | |
1480 | ||
54bdc1f7 | 1481 | for (i = 0; i < muxer_upstream_msg_iters->len; i++) { |
b5443165 | 1482 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
54bdc1f7 | 1483 | muxer_upstream_msg_iters->pdata[i]; |
b5443165 PP |
1484 | |
1485 | if (!bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1486 | upstream_msg_iter->msg_iter)) { | |
1487 | ret = BT_FALSE; | |
1488 | goto end; | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | end: | |
1493 | return ret; | |
1494 | } | |
1495 | ||
54bdc1f7 PP |
1496 | BT_HIDDEN |
1497 | bt_bool muxer_msg_iter_can_seek_beginning( | |
1498 | bt_self_message_iterator *self_msg_iter) | |
1499 | { | |
1500 | struct muxer_msg_iter *muxer_msg_iter = | |
1501 | bt_self_message_iterator_get_data(self_msg_iter); | |
1502 | bt_bool ret = BT_TRUE; | |
1503 | ||
1504 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1505 | muxer_msg_iter->active_muxer_upstream_msg_iters)) { | |
1506 | ret = BT_FALSE; | |
1507 | goto end; | |
1508 | } | |
1509 | ||
1510 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1511 | muxer_msg_iter->ended_muxer_upstream_msg_iters)) { | |
1512 | ret = BT_FALSE; | |
1513 | goto end; | |
1514 | } | |
1515 | ||
1516 | end: | |
1517 | return ret; | |
1518 | } | |
1519 | ||
b5443165 | 1520 | BT_HIDDEN |
d24d5663 | 1521 | bt_component_class_message_iterator_seek_beginning_method_status muxer_msg_iter_seek_beginning( |
b5443165 PP |
1522 | bt_self_message_iterator *self_msg_iter) |
1523 | { | |
1524 | struct muxer_msg_iter *muxer_msg_iter = | |
1525 | bt_self_message_iterator_get_data(self_msg_iter); | |
d24d5663 PP |
1526 | bt_component_class_message_iterator_seek_beginning_method_status status = |
1527 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK; | |
1528 | bt_message_iterator_seek_beginning_status seek_beg_status; | |
b5443165 PP |
1529 | uint64_t i; |
1530 | ||
54bdc1f7 PP |
1531 | /* Seek all ended upstream iterators first */ |
1532 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1533 | i++) { | |
b5443165 | 1534 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
54bdc1f7 | 1535 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; |
b5443165 | 1536 | |
d24d5663 | 1537 | seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning( |
b5443165 | 1538 | upstream_msg_iter->msg_iter); |
d24d5663 PP |
1539 | if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) { |
1540 | status = (int) seek_beg_status; | |
b5443165 PP |
1541 | goto end; |
1542 | } | |
54bdc1f7 PP |
1543 | |
1544 | empty_message_queue(upstream_msg_iter); | |
1545 | } | |
1546 | ||
1547 | /* Seek all previously active upstream iterators */ | |
1548 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; | |
1549 | i++) { | |
1550 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1551 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i]; | |
1552 | ||
d24d5663 | 1553 | seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning( |
54bdc1f7 | 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; | |
54bdc1f7 PP |
1557 | goto end; |
1558 | } | |
1559 | ||
1560 | empty_message_queue(upstream_msg_iter); | |
1561 | } | |
1562 | ||
1563 | /* Make them all active */ | |
1564 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1565 | i++) { | |
1566 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1567 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; | |
1568 | ||
1569 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, | |
1570 | upstream_msg_iter); | |
1571 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL; | |
b5443165 PP |
1572 | } |
1573 | ||
3625612b MJ |
1574 | /* |
1575 | * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is | |
1576 | * called on an empty array. | |
1577 | */ | |
1578 | if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) { | |
1579 | g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters, | |
1580 | 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len); | |
1581 | } | |
b5443165 | 1582 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
54bdc1f7 PP |
1583 | muxer_msg_iter->clock_class_expectation = |
1584 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY; | |
b5443165 PP |
1585 | |
1586 | end: | |
d24d5663 | 1587 | return status; |
b5443165 | 1588 | } |