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