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