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 | ||
318fe670 PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
24 | #include "logging.h" | |
25 | ||
85e7137b | 26 | #include "common/macros.h" |
57952005 | 27 | #include "compat/uuid.h" |
71c5da58 | 28 | #include <babeltrace2/babeltrace.h> |
57952005 MJ |
29 | #include "lib/value.h" |
30 | #include "lib/graph/component.h" | |
31 | #include "lib/graph/message/iterator.h" | |
32 | #include "lib/graph/connection.h" | |
958f7d11 | 33 | #include <glib.h> |
c55a9f58 | 34 | #include <stdbool.h> |
318fe670 | 35 | #include <inttypes.h> |
57952005 MJ |
36 | #include "common/assert.h" |
37 | #include "common/common.h" | |
0fbb9a9f | 38 | #include <stdlib.h> |
282c8cd0 | 39 | #include <string.h> |
958f7d11 | 40 | |
3e3ea13e FD |
41 | #include "muxer.h" |
42 | ||
c3acd5f3 | 43 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
65ee897d | 44 | |
958f7d11 | 45 | struct muxer_comp { |
958f7d11 | 46 | /* Weak ref */ |
8eee8ea2 | 47 | bt_self_component_filter *self_comp; |
834e9996 | 48 | |
958f7d11 PP |
49 | unsigned int next_port_num; |
50 | size_t available_input_ports; | |
b09a5592 | 51 | bool initializing_muxer_msg_iter; |
282c8cd0 | 52 | bool assume_absolute_clock_classes; |
958f7d11 PP |
53 | }; |
54 | ||
b09a5592 | 55 | struct muxer_upstream_msg_iter { |
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 { |
ab11110e | 72 | /* |
b09a5592 | 73 | * Array of struct muxer_upstream_msg_iter * (owned by this). |
ab11110e PP |
74 | * |
75 | * NOTE: This array is searched in linearly to find the youngest | |
b09a5592 | 76 | * current message. Keep this until benchmarks confirm that |
ab11110e PP |
77 | * another data structure is faster than this for our typical |
78 | * use cases. | |
79 | */ | |
a71ed05c PP |
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; | |
958f7d11 | 90 | |
b09a5592 | 91 | /* Last time returned in a message */ |
958f7d11 | 92 | int64_t last_returned_ts_ns; |
282c8cd0 PP |
93 | |
94 | /* Clock class expectation state */ | |
b09a5592 | 95 | enum muxer_msg_iter_clock_class_expectation clock_class_expectation; |
282c8cd0 PP |
96 | |
97 | /* | |
98 | * Expected clock class UUID, only valid when | |
99 | * clock_class_expectation is | |
b09a5592 | 100 | * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. |
282c8cd0 PP |
101 | */ |
102 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; | |
958f7d11 PP |
103 | }; |
104 | ||
a71ed05c PP |
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 | ||
ab11110e | 115 | static |
b09a5592 PP |
116 | void destroy_muxer_upstream_msg_iter( |
117 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) | |
ab11110e | 118 | { |
b09a5592 | 119 | if (!muxer_upstream_msg_iter) { |
ab11110e PP |
120 | return; |
121 | } | |
122 | ||
b09a5592 PP |
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); | |
a71ed05c PP |
128 | bt_self_component_port_input_message_iterator_put_ref( |
129 | muxer_upstream_msg_iter->msg_iter); | |
3fd7b79d | 130 | |
b09a5592 | 131 | if (muxer_upstream_msg_iter->msgs) { |
a71ed05c | 132 | empty_message_queue(muxer_upstream_msg_iter); |
b09a5592 | 133 | g_queue_free(muxer_upstream_msg_iter->msgs); |
3fd7b79d PP |
134 | } |
135 | ||
b09a5592 | 136 | g_free(muxer_upstream_msg_iter); |
ab11110e PP |
137 | } |
138 | ||
958f7d11 | 139 | static |
ae644e59 | 140 | int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter, |
b09a5592 | 141 | bt_self_component_port_input_message_iterator *self_msg_iter) |
958f7d11 | 142 | { |
ae644e59 | 143 | int ret = 0; |
b09a5592 PP |
144 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
145 | g_new0(struct muxer_upstream_msg_iter, 1); | |
958f7d11 | 146 | |
b09a5592 PP |
147 | if (!muxer_upstream_msg_iter) { |
148 | BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper."); | |
11603bce | 149 | goto error; |
958f7d11 PP |
150 | } |
151 | ||
b09a5592 PP |
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) { | |
3fd7b79d | 156 | BT_LOGE_STR("Failed to allocate a GQueue."); |
11603bce | 157 | goto error; |
3fd7b79d PP |
158 | } |
159 | ||
a71ed05c | 160 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, |
b09a5592 PP |
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); | |
958f7d11 | 166 | |
11603bce FD |
167 | goto end; |
168 | ||
169 | error: | |
170 | g_free(muxer_upstream_msg_iter); | |
ae644e59 | 171 | ret = -1; |
11603bce | 172 | |
958f7d11 | 173 | end: |
ae644e59 | 174 | return ret; |
958f7d11 PP |
175 | } |
176 | ||
958f7d11 | 177 | static |
1043fdea | 178 | bt_self_component_status add_available_input_port( |
8eee8ea2 | 179 | bt_self_component_filter *self_comp) |
958f7d11 | 180 | { |
834e9996 | 181 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
bb61965b | 182 | bt_self_component_filter_as_self_component(self_comp)); |
ee78f405 | 183 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 | 184 | GString *port_name = NULL; |
958f7d11 | 185 | |
8b45963b | 186 | BT_ASSERT(muxer_comp); |
958f7d11 PP |
187 | port_name = g_string_new("in"); |
188 | if (!port_name) { | |
318fe670 | 189 | BT_LOGE_STR("Failed to allocate a GString."); |
834e9996 | 190 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
958f7d11 PP |
191 | goto end; |
192 | } | |
193 | ||
194 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
834e9996 PP |
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) { | |
318fe670 PP |
198 | BT_LOGE("Cannot add input port to muxer component: " |
199 | "port-name=\"%s\", comp-addr=%p, status=%s", | |
834e9996 PP |
200 | port_name->str, self_comp, |
201 | bt_self_component_status_string(status)); | |
958f7d11 PP |
202 | goto end; |
203 | } | |
204 | ||
205 | muxer_comp->available_input_ports++; | |
206 | muxer_comp->next_port_num++; | |
a684a357 | 207 | BT_LOGI("Added one input port to muxer component: " |
318fe670 | 208 | "port-name=\"%s\", comp-addr=%p", |
834e9996 | 209 | port_name->str, self_comp); |
1043fdea | 210 | |
958f7d11 PP |
211 | end: |
212 | if (port_name) { | |
213 | g_string_free(port_name, TRUE); | |
214 | } | |
215 | ||
147337a3 | 216 | return status; |
958f7d11 PP |
217 | } |
218 | ||
958f7d11 | 219 | static |
ee78f405 | 220 | bt_self_component_status create_output_port( |
8eee8ea2 | 221 | bt_self_component_filter *self_comp) |
958f7d11 | 222 | { |
834e9996 PP |
223 | return bt_self_component_filter_add_output_port( |
224 | self_comp, "out", NULL, NULL); | |
958f7d11 PP |
225 | } |
226 | ||
227 | static | |
228 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
229 | { | |
230 | if (!muxer_comp) { | |
231 | return; | |
232 | } | |
233 | ||
958f7d11 PP |
234 | g_free(muxer_comp); |
235 | } | |
236 | ||
65ee897d | 237 | static |
8eee8ea2 | 238 | bt_value *get_default_params(void) |
65ee897d | 239 | { |
8eee8ea2 | 240 | bt_value *params; |
65ee897d PP |
241 | int ret; |
242 | ||
ce141536 | 243 | params = bt_value_map_create(); |
65ee897d | 244 | if (!params) { |
318fe670 | 245 | BT_LOGE_STR("Cannot create a map value object."); |
65ee897d PP |
246 | goto error; |
247 | } | |
248 | ||
ce141536 | 249 | ret = bt_value_map_insert_bool_entry(params, |
c3acd5f3 | 250 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); |
65ee897d | 251 | if (ret) { |
318fe670 | 252 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
65ee897d PP |
253 | goto error; |
254 | } | |
255 | ||
256 | goto end; | |
257 | ||
258 | error: | |
8c6884d9 | 259 | BT_VALUE_PUT_REF_AND_RESET(params); |
65ee897d PP |
260 | |
261 | end: | |
262 | return params; | |
263 | } | |
264 | ||
265 | static | |
ce141536 | 266 | int configure_muxer_comp(struct muxer_comp *muxer_comp, |
8eee8ea2 | 267 | const bt_value *params) |
65ee897d | 268 | { |
8eee8ea2 PP |
269 | bt_value *default_params = NULL; |
270 | bt_value *real_params = NULL; | |
271 | const bt_value *assume_absolute_clock_classes = NULL; | |
65ee897d | 272 | int ret = 0; |
c55a9f58 | 273 | bt_bool bool_val; |
65ee897d PP |
274 | |
275 | default_params = get_default_params(); | |
276 | if (!default_params) { | |
318fe670 PP |
277 | BT_LOGE("Cannot get default parameters: " |
278 | "muxer-comp-addr=%p", muxer_comp); | |
65ee897d PP |
279 | goto error; |
280 | } | |
281 | ||
7be9d1d3 | 282 | ret = bt_value_map_extend(default_params, params, &real_params); |
b5cdc106 | 283 | if (ret) { |
318fe670 PP |
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); | |
65ee897d PP |
288 | goto error; |
289 | } | |
290 | ||
ce141536 PP |
291 | assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params, |
292 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); | |
8b45963b PP |
293 | if (assume_absolute_clock_classes && |
294 | !bt_value_is_bool(assume_absolute_clock_classes)) { | |
318fe670 PP |
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, | |
17582c6d | 298 | bt_common_value_type_string( |
318fe670 | 299 | bt_value_get_type(assume_absolute_clock_classes))); |
65ee897d PP |
300 | goto error; |
301 | } | |
302 | ||
b5cdc106 | 303 | bool_val = bt_value_bool_get(assume_absolute_clock_classes); |
282c8cd0 | 304 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
a684a357 | 305 | BT_LOGI("Configured muxer component: muxer-comp-addr=%p, " |
318fe670 PP |
306 | "assume-absolute-clock-classes=%d", |
307 | muxer_comp, muxer_comp->assume_absolute_clock_classes); | |
65ee897d PP |
308 | goto end; |
309 | ||
310 | error: | |
311 | ret = -1; | |
312 | ||
313 | end: | |
8c6884d9 PP |
314 | bt_value_put_ref(default_params); |
315 | bt_value_put_ref(real_params); | |
65ee897d PP |
316 | return ret; |
317 | } | |
318 | ||
958f7d11 | 319 | BT_HIDDEN |
ee78f405 | 320 | bt_self_component_status muxer_init( |
8eee8ea2 | 321 | bt_self_component_filter *self_comp, |
3e3ea13e | 322 | const bt_value *params, void *init_data) |
958f7d11 PP |
323 | { |
324 | int ret; | |
ee78f405 | 325 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 PP |
326 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); |
327 | ||
a684a357 | 328 | BT_LOGI("Initializing muxer component: " |
834e9996 | 329 | "comp-addr=%p, params-addr=%p", self_comp, params); |
318fe670 | 330 | |
958f7d11 | 331 | if (!muxer_comp) { |
318fe670 | 332 | BT_LOGE_STR("Failed to allocate one muxer component."); |
958f7d11 PP |
333 | goto error; |
334 | } | |
335 | ||
65ee897d PP |
336 | ret = configure_muxer_comp(muxer_comp, params); |
337 | if (ret) { | |
318fe670 PP |
338 | BT_LOGE("Cannot configure muxer component: " |
339 | "muxer-comp-addr=%p, params-addr=%p", | |
340 | muxer_comp, params); | |
65ee897d PP |
341 | goto error; |
342 | } | |
343 | ||
834e9996 PP |
344 | muxer_comp->self_comp = self_comp; |
345 | bt_self_component_set_data( | |
bb61965b | 346 | bt_self_component_filter_as_self_component(self_comp), |
834e9996 | 347 | muxer_comp); |
1043fdea | 348 | status = add_available_input_port(self_comp); |
834e9996 | 349 | if (status != BT_SELF_COMPONENT_STATUS_OK) { |
318fe670 PP |
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, | |
834e9996 | 353 | bt_self_component_status_string(status)); |
958f7d11 PP |
354 | goto error; |
355 | } | |
356 | ||
834e9996 | 357 | status = create_output_port(self_comp); |
318fe670 PP |
358 | if (status) { |
359 | BT_LOGE("Cannot create muxer component's output port: " | |
360 | "muxer-comp-addr=%p, status=%s", | |
361 | muxer_comp, | |
834e9996 | 362 | bt_self_component_status_string(status)); |
958f7d11 PP |
363 | goto error; |
364 | } | |
365 | ||
a684a357 | 366 | BT_LOGI("Initialized muxer component: " |
318fe670 | 367 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", |
834e9996 | 368 | self_comp, params, muxer_comp); |
318fe670 | 369 | |
958f7d11 PP |
370 | goto end; |
371 | ||
372 | error: | |
373 | destroy_muxer_comp(muxer_comp); | |
834e9996 | 374 | bt_self_component_set_data( |
bb61965b | 375 | bt_self_component_filter_as_self_component(self_comp), |
834e9996 | 376 | NULL); |
147337a3 | 377 | |
834e9996 PP |
378 | if (status == BT_SELF_COMPONENT_STATUS_OK) { |
379 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
147337a3 | 380 | } |
958f7d11 PP |
381 | |
382 | end: | |
383 | return status; | |
384 | } | |
385 | ||
386 | BT_HIDDEN | |
8eee8ea2 | 387 | void muxer_finalize(bt_self_component_filter *self_comp) |
958f7d11 | 388 | { |
834e9996 | 389 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
bb61965b | 390 | bt_self_component_filter_as_self_component(self_comp)); |
958f7d11 | 391 | |
a684a357 | 392 | BT_LOGI("Finalizing muxer component: comp-addr=%p", |
834e9996 | 393 | self_comp); |
958f7d11 PP |
394 | destroy_muxer_comp(muxer_comp); |
395 | } | |
396 | ||
397 | static | |
b09a5592 | 398 | bt_self_component_port_input_message_iterator * |
1043fdea | 399 | create_msg_iter_on_input_port(bt_self_component_port_input *self_port) |
958f7d11 | 400 | { |
8eee8ea2 | 401 | const bt_port *port = bt_self_component_port_as_port( |
bb61965b | 402 | bt_self_component_port_input_as_self_component_port( |
834e9996 | 403 | self_port)); |
b09a5592 | 404 | bt_self_component_port_input_message_iterator *msg_iter = |
834e9996 | 405 | NULL; |
958f7d11 | 406 | |
8b45963b PP |
407 | BT_ASSERT(port); |
408 | BT_ASSERT(bt_port_is_connected(port)); | |
958f7d11 | 409 | |
ab11110e | 410 | // TODO: Advance the iterator to >= the time of the latest |
b09a5592 | 411 | // returned message by the muxer message |
ab11110e | 412 | // iterator which creates it. |
b09a5592 | 413 | msg_iter = bt_self_component_port_input_message_iterator_create( |
834e9996 | 414 | self_port); |
b09a5592 PP |
415 | if (!msg_iter) { |
416 | BT_LOGE("Cannot create upstream message iterator on input port: " | |
834e9996 PP |
417 | "port-addr=%p, port-name=\"%s\"", |
418 | port, bt_port_get_name(port)); | |
958f7d11 PP |
419 | goto end; |
420 | } | |
421 | ||
a684a357 | 422 | BT_LOGI("Created upstream message iterator on input port: " |
b09a5592 PP |
423 | "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p", |
424 | port, bt_port_get_name(port), msg_iter); | |
318fe670 | 425 | |
958f7d11 | 426 | end: |
b09a5592 | 427 | return msg_iter; |
958f7d11 PP |
428 | } |
429 | ||
ab11110e | 430 | static |
3e3ea13e | 431 | bt_self_message_iterator_status muxer_upstream_msg_iter_next( |
a71ed05c PP |
432 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
433 | bool *is_ended) | |
ab11110e | 434 | { |
3e3ea13e FD |
435 | bt_self_message_iterator_status status; |
436 | bt_message_iterator_status input_port_iter_status; | |
b09a5592 | 437 | bt_message_array_const msgs; |
3fd7b79d PP |
438 | uint64_t i; |
439 | uint64_t count; | |
ab11110e | 440 | |
a684a357 | 441 | BT_LOGD("Calling upstream message iterator's \"next\" method: " |
b09a5592 PP |
442 | "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p", |
443 | muxer_upstream_msg_iter, | |
444 | muxer_upstream_msg_iter->msg_iter); | |
3e3ea13e | 445 | input_port_iter_status = bt_self_component_port_input_message_iterator_next( |
b09a5592 | 446 | muxer_upstream_msg_iter->msg_iter, &msgs, &count); |
a684a357 | 447 | BT_LOGD("Upstream message iterator's \"next\" method returned: " |
3e3ea13e | 448 | "status=%s", bt_message_iterator_status_string(input_port_iter_status)); |
ab11110e | 449 | |
3e3ea13e | 450 | switch (input_port_iter_status) { |
b09a5592 | 451 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
089717de | 452 | /* |
b09a5592 | 453 | * Message iterator's current message is |
3fd7b79d | 454 | * valid: it must be considered for muxing operations. |
089717de | 455 | */ |
a684a357 | 456 | BT_LOGD_STR("Validated upstream message iterator wrapper."); |
3fd7b79d PP |
457 | BT_ASSERT(count > 0); |
458 | ||
b09a5592 | 459 | /* Move messages to our queue */ |
3fd7b79d PP |
460 | for (i = 0; i < count; i++) { |
461 | /* | |
462 | * Push to tail in order; other side | |
b09a5592 | 463 | * (muxer_msg_iter_do_next_one()) consumes |
3fd7b79d PP |
464 | * from the head first. |
465 | */ | |
b09a5592 PP |
466 | g_queue_push_tail(muxer_upstream_msg_iter->msgs, |
467 | (void *) msgs[i]); | |
3fd7b79d | 468 | } |
3e3ea13e | 469 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
ab11110e | 470 | break; |
b09a5592 | 471 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
089717de | 472 | /* |
b09a5592 | 473 | * Message iterator's current message is not |
089717de | 474 | * valid anymore. Return |
b09a5592 | 475 | * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately. |
089717de | 476 | */ |
3e3ea13e | 477 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
ab11110e | 478 | break; |
b09a5592 | 479 | case BT_MESSAGE_ITERATOR_STATUS_END: /* Fall-through. */ |
ab11110e | 480 | /* |
b09a5592 | 481 | * Message iterator reached the end: release it. It |
ab11110e | 482 | * won't be considered again to find the youngest |
b09a5592 | 483 | * message. |
ab11110e | 484 | */ |
a71ed05c | 485 | *is_ended = true; |
3e3ea13e | 486 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
089717de | 487 | break; |
ab11110e PP |
488 | default: |
489 | /* Error or unsupported status code */ | |
318fe670 | 490 | BT_LOGE("Error or unsupported status code: " |
3e3ea13e FD |
491 | "status-code=%d", input_port_iter_status); |
492 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
089717de | 493 | break; |
ab11110e PP |
494 | } |
495 | ||
089717de | 496 | return status; |
ab11110e PP |
497 | } |
498 | ||
958f7d11 | 499 | static |
b09a5592 PP |
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, | |
958f7d11 PP |
503 | int64_t *ts_ns) |
504 | { | |
ecbb78c0 | 505 | const bt_clock_snapshot *clock_snapshot = NULL; |
958f7d11 | 506 | int ret = 0; |
4a4dd64f | 507 | bt_message_stream_activity_clock_snapshot_state sa_cs_state; |
34e13c22 PP |
508 | const bt_stream_class *stream_class = NULL; |
509 | bt_message_type msg_type; | |
958f7d11 | 510 | |
b09a5592 | 511 | BT_ASSERT(msg); |
8b45963b | 512 | BT_ASSERT(ts_ns); |
a684a357 | 513 | BT_LOGD("Getting message's timestamp: " |
b09a5592 | 514 | "muxer-msg-iter-addr=%p, msg-addr=%p, " |
318fe670 | 515 | "last-returned-ts=%" PRId64, |
b09a5592 | 516 | muxer_msg_iter, msg, last_returned_ts_ns); |
318fe670 | 517 | |
85e7137b | 518 | if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation == |
37911c11 PP |
519 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) { |
520 | *ts_ns = last_returned_ts_ns; | |
521 | goto end; | |
522 | } | |
523 | ||
34e13c22 PP |
524 | msg_type = bt_message_get_type(msg); |
525 | ||
85e7137b | 526 | if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) { |
34e13c22 PP |
527 | stream_class = bt_stream_borrow_class_const( |
528 | bt_packet_borrow_stream_const( | |
529 | bt_message_packet_beginning_borrow_packet_const( | |
530 | msg))); | |
85e7137b | 531 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) { |
34e13c22 PP |
532 | stream_class = bt_stream_borrow_class_const( |
533 | bt_packet_borrow_stream_const( | |
534 | bt_message_packet_end_borrow_packet_const( | |
535 | msg))); | |
85e7137b | 536 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) { |
77037b2b PP |
537 | stream_class = bt_stream_borrow_class_const( |
538 | bt_message_discarded_events_borrow_stream_const(msg)); | |
85e7137b | 539 | } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) { |
77037b2b PP |
540 | stream_class = bt_stream_borrow_class_const( |
541 | bt_message_discarded_packets_borrow_stream_const(msg)); | |
34e13c22 PP |
542 | } |
543 | ||
544 | switch (msg_type) { | |
b09a5592 | 545 | case BT_MESSAGE_TYPE_EVENT: |
37911c11 PP |
546 | BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const( |
547 | msg)); | |
11ddb3ef PP |
548 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
549 | msg); | |
958f7d11 | 550 | break; |
4a4dd64f | 551 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
5ef34326 | 552 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot( |
34e13c22 PP |
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 | ||
4a4dd64f PP |
560 | break; |
561 | case BT_MESSAGE_TYPE_PACKET_END: | |
5ef34326 | 562 | if (bt_stream_class_packets_have_end_default_clock_snapshot( |
34e13c22 PP |
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 | ||
4a4dd64f PP |
570 | break; |
571 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
77037b2b PP |
572 | if (bt_stream_class_discarded_events_have_default_clock_snapshots( |
573 | stream_class)) { | |
5ef34326 | 574 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
77037b2b PP |
575 | msg); |
576 | } else { | |
577 | goto no_clock_snapshot; | |
578 | } | |
579 | ||
4a4dd64f PP |
580 | break; |
581 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
77037b2b PP |
582 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots( |
583 | stream_class)) { | |
5ef34326 | 584 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
77037b2b PP |
585 | msg); |
586 | } else { | |
587 | goto no_clock_snapshot; | |
588 | } | |
589 | ||
4a4dd64f PP |
590 | break; |
591 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: | |
37911c11 PP |
592 | BT_ASSERT(bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const( |
593 | msg)); | |
4a4dd64f PP |
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) { | |
2d42927b | 597 | goto no_clock_snapshot; |
4a4dd64f | 598 | } |
958f7d11 | 599 | |
4a4dd64f PP |
600 | break; |
601 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: | |
37911c11 PP |
602 | BT_ASSERT(bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const( |
603 | msg)); | |
4a4dd64f PP |
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) { | |
2d42927b | 607 | goto no_clock_snapshot; |
4a4dd64f PP |
608 | } |
609 | ||
610 | break; | |
40bf6fd0 | 611 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
11ddb3ef PP |
612 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
613 | msg); | |
958f7d11 PP |
614 | break; |
615 | default: | |
b09a5592 | 616 | /* All the other messages have a higher priority */ |
a684a357 | 617 | BT_LOGD_STR("Message has no timestamp: using the last returned timestamp."); |
958f7d11 PP |
618 | *ts_ns = last_returned_ts_ns; |
619 | goto end; | |
620 | } | |
621 | ||
37911c11 PP |
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: | |
a684a357 | 632 | BT_LOGD_STR("Message's default clock snapshot is missing: " |
37911c11 PP |
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) { | |
a684a357 | 642 | BT_LOGD("Found message's timestamp: " |
37911c11 PP |
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); | |
7b33a0e0 PP |
647 | } |
648 | ||
37911c11 PP |
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 | ||
8fc063a2 | 661 | BT_ASSERT(clock_class); |
839d52a5 PP |
662 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
663 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 | 664 | |
b09a5592 PP |
665 | if (muxer_msg_iter->clock_class_expectation == |
666 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
282c8cd0 PP |
667 | /* |
668 | * This is the first clock class that this muxer | |
b09a5592 | 669 | * message iterator encounters. Its properties |
282c8cd0 PP |
670 | * determine what to expect for the whole lifetime of |
671 | * the iterator without a true | |
672 | * `assume-absolute-clock-classes` parameter. | |
673 | */ | |
d608d675 | 674 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
282c8cd0 | 675 | /* Expect absolute clock classes */ |
b09a5592 PP |
676 | muxer_msg_iter->clock_class_expectation = |
677 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
282c8cd0 PP |
678 | } else { |
679 | if (cc_uuid) { | |
680 | /* | |
681 | * Expect non-absolute clock classes | |
682 | * with a specific UUID. | |
683 | */ | |
b09a5592 PP |
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, | |
282c8cd0 PP |
687 | cc_uuid, BABELTRACE_UUID_LEN); |
688 | } else { | |
689 | /* | |
690 | * Expect non-absolute clock classes | |
691 | * with no UUID. | |
692 | */ | |
b09a5592 PP |
693 | muxer_msg_iter->clock_class_expectation = |
694 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
282c8cd0 PP |
695 | } |
696 | } | |
697 | } | |
698 | ||
699 | if (!muxer_comp->assume_absolute_clock_classes) { | |
b09a5592 PP |
700 | switch (muxer_msg_iter->clock_class_expectation) { |
701 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
d608d675 | 702 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { |
318fe670 PP |
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); | |
282c8cd0 PP |
707 | goto error; |
708 | } | |
709 | break; | |
b09a5592 | 710 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: |
d608d675 | 711 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
318fe670 PP |
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); | |
282c8cd0 PP |
716 | goto error; |
717 | } | |
718 | ||
719 | if (cc_uuid) { | |
318fe670 PP |
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]); | |
282c8cd0 PP |
741 | goto error; |
742 | } | |
743 | break; | |
b09a5592 | 744 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: |
d608d675 | 745 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
318fe670 PP |
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); | |
282c8cd0 PP |
750 | goto error; |
751 | } | |
752 | ||
753 | if (!cc_uuid) { | |
318fe670 PP |
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); | |
282c8cd0 PP |
758 | goto error; |
759 | } | |
760 | ||
b09a5592 | 761 | if (memcmp(muxer_msg_iter->expected_clock_class_uuid, |
282c8cd0 | 762 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { |
318fe670 PP |
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, | |
b09a5592 PP |
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], | |
318fe670 PP |
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]); | |
282c8cd0 PP |
801 | goto error; |
802 | } | |
803 | break; | |
37911c11 PP |
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; | |
282c8cd0 PP |
809 | default: |
810 | /* Unexpected */ | |
318fe670 PP |
811 | BT_LOGF("Unexpected clock class expectation: " |
812 | "expectation-code=%d", | |
b09a5592 | 813 | muxer_msg_iter->clock_class_expectation); |
282c8cd0 PP |
814 | abort(); |
815 | } | |
958f7d11 PP |
816 | } |
817 | ||
2d42927b PP |
818 | goto end; |
819 | ||
958f7d11 PP |
820 | error: |
821 | ret = -1; | |
822 | ||
823 | end: | |
37911c11 PP |
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; | |
318fe670 PP |
853 | } |
854 | ||
37911c11 PP |
855 | ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class); |
856 | ||
857 | end: | |
958f7d11 PP |
858 | return ret; |
859 | } | |
860 | ||
ab11110e | 861 | /* |
b09a5592 PP |
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. | |
ab11110e PP |
867 | * |
868 | * This function does NOT: | |
869 | * | |
b09a5592 | 870 | * * Update any upstream message iterator. |
b09a5592 | 871 | * * Check the upstream message iterators to retry. |
ab11110e | 872 | * |
b09a5592 PP |
873 | * On sucess, this function sets *muxer_upstream_msg_iter to the |
874 | * upstream message iterator of which the current message is | |
ab11110e PP |
875 | * the youngest, and sets *ts_ns to its time. |
876 | */ | |
958f7d11 | 877 | static |
3e3ea13e | 878 | bt_self_message_iterator_status |
b09a5592 | 879 | muxer_msg_iter_youngest_upstream_msg_iter( |
958f7d11 | 880 | struct muxer_comp *muxer_comp, |
b09a5592 PP |
881 | struct muxer_msg_iter *muxer_msg_iter, |
882 | struct muxer_upstream_msg_iter **muxer_upstream_msg_iter, | |
958f7d11 PP |
883 | int64_t *ts_ns) |
884 | { | |
885 | size_t i; | |
886 | int ret; | |
887 | int64_t youngest_ts_ns = INT64_MAX; | |
3e3ea13e FD |
888 | bt_self_message_iterator_status status = |
889 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 890 | |
8b45963b | 891 | BT_ASSERT(muxer_comp); |
b09a5592 PP |
892 | BT_ASSERT(muxer_msg_iter); |
893 | BT_ASSERT(muxer_upstream_msg_iter); | |
894 | *muxer_upstream_msg_iter = NULL; | |
895 | ||
a71ed05c PP |
896 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
897 | i++) { | |
b09a5592 PP |
898 | const bt_message *msg; |
899 | struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter = | |
a71ed05c PP |
900 | g_ptr_array_index( |
901 | muxer_msg_iter->active_muxer_upstream_msg_iters, | |
902 | i); | |
b09a5592 PP |
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); | |
958f7d11 PP |
910 | continue; |
911 | } | |
912 | ||
b09a5592 PP |
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); | |
37911c11 | 916 | |
85e7137b | 917 | if (G_UNLIKELY(bt_message_get_type(msg) == |
37911c11 PP |
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 | } | |
85e7137b | 931 | } else if (G_UNLIKELY(bt_message_get_type(msg) == |
37911c11 PP |
932 | BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) { |
933 | const bt_clock_snapshot *cs; | |
37911c11 | 934 | |
11ddb3ef PP |
935 | cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
936 | msg); | |
37911c11 PP |
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 | ||
b09a5592 PP |
946 | ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg, |
947 | muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns); | |
958f7d11 | 948 | if (ret) { |
b09a5592 PP |
949 | /* get_msg_ts_ns() logs errors */ |
950 | *muxer_upstream_msg_iter = NULL; | |
3e3ea13e | 951 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
952 | goto end; |
953 | } | |
954 | ||
b09a5592 PP |
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; | |
958f7d11 PP |
959 | *ts_ns = youngest_ts_ns; |
960 | } | |
961 | } | |
962 | ||
b09a5592 | 963 | if (!*muxer_upstream_msg_iter) { |
3e3ea13e | 964 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
958f7d11 PP |
965 | *ts_ns = INT64_MIN; |
966 | } | |
967 | ||
968 | end: | |
969 | return status; | |
970 | } | |
971 | ||
972 | static | |
3e3ea13e | 973 | bt_self_message_iterator_status validate_muxer_upstream_msg_iter( |
a71ed05c PP |
974 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
975 | bool *is_ended) | |
958f7d11 | 976 | { |
3e3ea13e FD |
977 | bt_self_message_iterator_status status = |
978 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 979 | |
a684a357 | 980 | BT_LOGD("Validating muxer's upstream message iterator wrapper: " |
b09a5592 PP |
981 | "muxer-upstream-msg-iter-wrap-addr=%p", |
982 | muxer_upstream_msg_iter); | |
318fe670 | 983 | |
b09a5592 PP |
984 | if (muxer_upstream_msg_iter->msgs->length > 0 || |
985 | !muxer_upstream_msg_iter->msg_iter) { | |
a684a357 | 986 | BT_LOGD("Already valid or not considered: " |
b09a5592 PP |
987 | "queue-len=%u, upstream-msg-iter-addr=%p", |
988 | muxer_upstream_msg_iter->msgs->length, | |
989 | muxer_upstream_msg_iter->msg_iter); | |
ab11110e PP |
990 | goto end; |
991 | } | |
992 | ||
b09a5592 | 993 | /* muxer_upstream_msg_iter_next() logs details/errors */ |
a71ed05c PP |
994 | status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter, |
995 | is_ended); | |
089717de PP |
996 | |
997 | end: | |
998 | return status; | |
999 | } | |
1000 | ||
1001 | static | |
3e3ea13e | 1002 | bt_self_message_iterator_status validate_muxer_upstream_msg_iters( |
b09a5592 | 1003 | struct muxer_msg_iter *muxer_msg_iter) |
089717de | 1004 | { |
3e3ea13e FD |
1005 | bt_self_message_iterator_status status = |
1006 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
089717de PP |
1007 | size_t i; |
1008 | ||
a684a357 | 1009 | BT_LOGD("Validating muxer's upstream message iterator wrappers: " |
b09a5592 | 1010 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
318fe670 | 1011 | |
a71ed05c PP |
1012 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
1013 | i++) { | |
90aed4f2 | 1014 | bool is_ended = false; |
b09a5592 | 1015 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
089717de | 1016 | g_ptr_array_index( |
a71ed05c | 1017 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
089717de PP |
1018 | i); |
1019 | ||
b09a5592 | 1020 | status = validate_muxer_upstream_msg_iter( |
a71ed05c | 1021 | muxer_upstream_msg_iter, &is_ended); |
3e3ea13e | 1022 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
318fe670 | 1023 | if (status < 0) { |
b09a5592 PP |
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); | |
318fe670 | 1029 | } else { |
a684a357 | 1030 | BT_LOGD("Cannot validate muxer's upstream message iterator wrapper: " |
b09a5592 PP |
1031 | "muxer-msg-iter-addr=%p, " |
1032 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
1033 | muxer_msg_iter, | |
1034 | muxer_upstream_msg_iter); | |
318fe670 PP |
1035 | } |
1036 | ||
089717de PP |
1037 | goto end; |
1038 | } | |
744ba28b PP |
1039 | |
1040 | /* | |
a71ed05c PP |
1041 | * Move this muxer upstream message iterator to the |
1042 | * array of ended iterators if it's ended. | |
744ba28b | 1043 | */ |
85e7137b | 1044 | if (G_UNLIKELY(is_ended)) { |
a684a357 | 1045 | BT_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: " |
a71ed05c PP |
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 | ||
744ba28b PP |
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( | |
a71ed05c | 1059 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
744ba28b PP |
1060 | i); |
1061 | i--; | |
1062 | } | |
089717de PP |
1063 | } |
1064 | ||
1065 | end: | |
1066 | return status; | |
1067 | } | |
1068 | ||
3fd7b79d | 1069 | static inline |
3e3ea13e | 1070 | bt_self_message_iterator_status muxer_msg_iter_do_next_one( |
089717de | 1071 | struct muxer_comp *muxer_comp, |
b09a5592 PP |
1072 | struct muxer_msg_iter *muxer_msg_iter, |
1073 | const bt_message **msg) | |
089717de | 1074 | { |
1043fdea | 1075 | bt_self_message_iterator_status status; |
b09a5592 | 1076 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL; |
089717de PP |
1077 | int64_t next_return_ts; |
1078 | ||
1043fdea PP |
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; | |
958f7d11 PP |
1083 | } |
1084 | ||
1085 | /* | |
089717de | 1086 | * At this point we know that all the existing upstream |
b09a5592 PP |
1087 | * message iterators are valid. We can find the one, |
1088 | * amongst those, of which the current message is the | |
089717de | 1089 | * youngest. |
958f7d11 | 1090 | */ |
b09a5592 PP |
1091 | status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp, |
1092 | muxer_msg_iter, &muxer_upstream_msg_iter, | |
089717de | 1093 | &next_return_ts); |
3e3ea13e | 1094 | if (status < 0 || status == BT_SELF_MESSAGE_ITERATOR_STATUS_END) { |
3fd7b79d | 1095 | if (status < 0) { |
b09a5592 | 1096 | BT_LOGE("Cannot find the youngest upstream message iterator wrapper: " |
318fe670 | 1097 | "status=%s", |
d47c10dc | 1098 | bt_common_self_message_iterator_status_string(status)); |
318fe670 | 1099 | } else { |
a684a357 | 1100 | BT_LOGD("Cannot find the youngest upstream message iterator wrapper: " |
318fe670 | 1101 | "status=%s", |
d47c10dc | 1102 | bt_common_self_message_iterator_status_string(status)); |
318fe670 PP |
1103 | } |
1104 | ||
958f7d11 PP |
1105 | goto end; |
1106 | } | |
1107 | ||
b09a5592 PP |
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 ", " | |
318fe670 | 1111 | "last-returned-ts=%" PRId64, |
b09a5592 PP |
1112 | muxer_msg_iter, next_return_ts, |
1113 | muxer_msg_iter->last_returned_ts_ns); | |
3e3ea13e | 1114 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1115 | goto end; |
1116 | } | |
1117 | ||
a684a357 | 1118 | BT_LOGD("Found youngest upstream message iterator wrapper: " |
b09a5592 PP |
1119 | "muxer-msg-iter-addr=%p, " |
1120 | "muxer-upstream-msg-iter-wrap-addr=%p, " | |
318fe670 | 1121 | "ts=%" PRId64, |
b09a5592 | 1122 | muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts); |
3e3ea13e | 1123 | BT_ASSERT(status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK); |
b09a5592 | 1124 | BT_ASSERT(muxer_upstream_msg_iter); |
958f7d11 PP |
1125 | |
1126 | /* | |
3fd7b79d | 1127 | * Consume from the queue's head: other side |
b09a5592 | 1128 | * (muxer_upstream_msg_iter_next()) writes to the tail. |
958f7d11 | 1129 | */ |
b09a5592 PP |
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; | |
958f7d11 PP |
1133 | |
1134 | end: | |
3fd7b79d PP |
1135 | return status; |
1136 | } | |
1137 | ||
1138 | static | |
3e3ea13e | 1139 | bt_self_message_iterator_status muxer_msg_iter_do_next( |
3fd7b79d | 1140 | struct muxer_comp *muxer_comp, |
b09a5592 PP |
1141 | struct muxer_msg_iter *muxer_msg_iter, |
1142 | bt_message_array_const msgs, uint64_t capacity, | |
3fd7b79d PP |
1143 | uint64_t *count) |
1144 | { | |
3e3ea13e FD |
1145 | bt_self_message_iterator_status status = |
1146 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
3fd7b79d PP |
1147 | uint64_t i = 0; |
1148 | ||
9cce94e4 | 1149 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
b09a5592 PP |
1150 | status = muxer_msg_iter_do_next_one(muxer_comp, |
1151 | muxer_msg_iter, &msgs[i]); | |
9cce94e4 | 1152 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
3fd7b79d PP |
1153 | i++; |
1154 | } | |
1155 | } | |
1156 | ||
1157 | if (i > 0) { | |
1158 | /* | |
b09a5592 | 1159 | * Even if muxer_msg_iter_do_next_one() returned |
3fd7b79d | 1160 | * something else than |
b09a5592 PP |
1161 | * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated |
1162 | * message objects in the output message | |
3fd7b79d | 1163 | * array, so we need to return |
b09a5592 | 1164 | * BT_MESSAGE_ITERATOR_STATUS_OK so that they are |
3fd7b79d | 1165 | * transfered to downstream. This other status occurs |
b09a5592 | 1166 | * again the next time muxer_msg_iter_do_next() is |
3fd7b79d | 1167 | * called, possibly without any accumulated |
b09a5592 | 1168 | * message, in which case we'll return it. |
3fd7b79d PP |
1169 | */ |
1170 | *count = i; | |
3e3ea13e | 1171 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
3fd7b79d PP |
1172 | } |
1173 | ||
1174 | return status; | |
958f7d11 PP |
1175 | } |
1176 | ||
1177 | static | |
b09a5592 | 1178 | void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter) |
ab11110e | 1179 | { |
b09a5592 | 1180 | if (!muxer_msg_iter) { |
ab11110e PP |
1181 | return; |
1182 | } | |
1183 | ||
b09a5592 PP |
1184 | BT_LOGD("Destroying muxer component's message iterator: " |
1185 | "muxer-msg-iter-addr=%p", muxer_msg_iter); | |
318fe670 | 1186 | |
a71ed05c PP |
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."); | |
ab11110e | 1195 | g_ptr_array_free( |
a71ed05c | 1196 | muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE); |
ab11110e PP |
1197 | } |
1198 | ||
b09a5592 | 1199 | g_free(muxer_msg_iter); |
ab11110e PP |
1200 | } |
1201 | ||
1202 | static | |
1043fdea | 1203 | int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp, |
b09a5592 | 1204 | struct muxer_msg_iter *muxer_msg_iter) |
958f7d11 | 1205 | { |
544d0515 PP |
1206 | int64_t count; |
1207 | int64_t i; | |
ab11110e | 1208 | int ret = 0; |
958f7d11 | 1209 | |
834e9996 | 1210 | count = bt_component_filter_get_input_port_count( |
bb61965b | 1211 | bt_self_component_filter_as_component_filter( |
834e9996 | 1212 | muxer_comp->self_comp)); |
544d0515 | 1213 | if (count < 0) { |
b09a5592 PP |
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); | |
ab11110e PP |
1217 | goto end; |
1218 | } | |
958f7d11 PP |
1219 | |
1220 | for (i = 0; i < count; i++) { | |
1043fdea | 1221 | bt_self_component_port_input_message_iterator *upstream_msg_iter; |
8eee8ea2 | 1222 | bt_self_component_port_input *self_port = |
834e9996 PP |
1223 | bt_self_component_filter_borrow_input_port_by_index( |
1224 | muxer_comp->self_comp, i); | |
8eee8ea2 | 1225 | const bt_port *port; |
958f7d11 | 1226 | |
834e9996 | 1227 | BT_ASSERT(self_port); |
bb61965b PP |
1228 | port = bt_self_component_port_as_port( |
1229 | bt_self_component_port_input_as_self_component_port( | |
834e9996 | 1230 | self_port)); |
8b45963b | 1231 | BT_ASSERT(port); |
958f7d11 PP |
1232 | |
1233 | if (!bt_port_is_connected(port)) { | |
1043fdea | 1234 | /* Skip non-connected port */ |
958f7d11 PP |
1235 | continue; |
1236 | } | |
1237 | ||
1043fdea PP |
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); | |
ab11110e PP |
1242 | ret = -1; |
1243 | goto end; | |
958f7d11 | 1244 | } |
318fe670 | 1245 | |
ae644e59 FD |
1246 | ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter, |
1247 | upstream_msg_iter); | |
1043fdea PP |
1248 | bt_self_component_port_input_message_iterator_put_ref( |
1249 | upstream_msg_iter); | |
ae644e59 | 1250 | if (ret) { |
1043fdea | 1251 | /* muxer_msg_iter_add_upstream_msg_iter() logs errors */ |
1043fdea PP |
1252 | goto end; |
1253 | } | |
958f7d11 PP |
1254 | } |
1255 | ||
1256 | end: | |
958f7d11 PP |
1257 | return ret; |
1258 | } | |
1259 | ||
958f7d11 | 1260 | BT_HIDDEN |
ee78f405 | 1261 | bt_self_message_iterator_status muxer_msg_iter_init( |
b09a5592 | 1262 | bt_self_message_iterator *self_msg_iter, |
8eee8ea2 PP |
1263 | bt_self_component_filter *self_comp, |
1264 | bt_self_component_port_output *port) | |
958f7d11 PP |
1265 | { |
1266 | struct muxer_comp *muxer_comp = NULL; | |
b09a5592 | 1267 | struct muxer_msg_iter *muxer_msg_iter = NULL; |
ee78f405 | 1268 | bt_self_message_iterator_status status = |
3e3ea13e | 1269 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
958f7d11 PP |
1270 | int ret; |
1271 | ||
834e9996 | 1272 | muxer_comp = bt_self_component_get_data( |
bb61965b | 1273 | bt_self_component_filter_as_self_component(self_comp)); |
8b45963b | 1274 | BT_ASSERT(muxer_comp); |
b09a5592 PP |
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); | |
a09c6b95 | 1278 | |
b09a5592 | 1279 | if (muxer_comp->initializing_muxer_msg_iter) { |
a09c6b95 | 1280 | /* |
089717de | 1281 | * Weird, unhandled situation detected: downstream |
b09a5592 PP |
1282 | * creates a muxer message iterator while creating |
1283 | * another muxer message iterator (same component). | |
a09c6b95 | 1284 | */ |
b09a5592 PP |
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); | |
958f7d11 PP |
1288 | goto error; |
1289 | } | |
1290 | ||
b09a5592 PP |
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."); | |
ab11110e PP |
1295 | goto error; |
1296 | } | |
1297 | ||
b09a5592 | 1298 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
a71ed05c | 1299 | muxer_msg_iter->active_muxer_upstream_msg_iters = |
958f7d11 | 1300 | g_ptr_array_new_with_free_func( |
b09a5592 | 1301 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); |
a71ed05c PP |
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) { | |
318fe670 | 1311 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1312 | goto error; |
1313 | } | |
1314 | ||
1043fdea | 1315 | ret = muxer_msg_iter_init_upstream_iterators(muxer_comp, |
b09a5592 | 1316 | muxer_msg_iter); |
a09c6b95 | 1317 | if (ret) { |
1043fdea | 1318 | BT_LOGE("Cannot initialize connected input ports for muxer component's message iterator: " |
318fe670 | 1319 | "comp-addr=%p, muxer-comp-addr=%p, " |
b09a5592 PP |
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); | |
a09c6b95 PP |
1323 | goto error; |
1324 | } | |
1325 | ||
1043fdea | 1326 | bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter); |
b09a5592 PP |
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); | |
958f7d11 PP |
1331 | goto end; |
1332 | ||
1333 | error: | |
b09a5592 | 1334 | destroy_muxer_msg_iter(muxer_msg_iter); |
1043fdea | 1335 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
3e3ea13e | 1336 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1337 | |
1338 | end: | |
b09a5592 | 1339 | muxer_comp->initializing_muxer_msg_iter = false; |
958f7d11 PP |
1340 | return status; |
1341 | } | |
1342 | ||
1343 | BT_HIDDEN | |
a71ed05c | 1344 | void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
958f7d11 | 1345 | { |
b09a5592 PP |
1346 | struct muxer_msg_iter *muxer_msg_iter = |
1347 | bt_self_message_iterator_get_data(self_msg_iter); | |
8eee8ea2 | 1348 | bt_self_component *self_comp = NULL; |
958f7d11 PP |
1349 | struct muxer_comp *muxer_comp = NULL; |
1350 | ||
b09a5592 PP |
1351 | self_comp = bt_self_message_iterator_borrow_component( |
1352 | self_msg_iter); | |
834e9996 PP |
1353 | BT_ASSERT(self_comp); |
1354 | muxer_comp = bt_self_component_get_data(self_comp); | |
b09a5592 PP |
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); | |
958f7d11 | 1359 | |
1043fdea | 1360 | if (muxer_msg_iter) { |
b09a5592 | 1361 | destroy_muxer_msg_iter(muxer_msg_iter); |
958f7d11 | 1362 | } |
958f7d11 PP |
1363 | } |
1364 | ||
1365 | BT_HIDDEN | |
3e3ea13e | 1366 | bt_self_message_iterator_status muxer_msg_iter_next( |
b09a5592 PP |
1367 | bt_self_message_iterator *self_msg_iter, |
1368 | bt_message_array_const msgs, uint64_t capacity, | |
3fd7b79d | 1369 | uint64_t *count) |
958f7d11 | 1370 | { |
3e3ea13e | 1371 | bt_self_message_iterator_status status; |
b09a5592 PP |
1372 | struct muxer_msg_iter *muxer_msg_iter = |
1373 | bt_self_message_iterator_get_data(self_msg_iter); | |
8eee8ea2 | 1374 | bt_self_component *self_comp = NULL; |
958f7d11 | 1375 | struct muxer_comp *muxer_comp = NULL; |
958f7d11 | 1376 | |
b09a5592 PP |
1377 | BT_ASSERT(muxer_msg_iter); |
1378 | self_comp = bt_self_message_iterator_borrow_component( | |
1379 | self_msg_iter); | |
834e9996 PP |
1380 | BT_ASSERT(self_comp); |
1381 | muxer_comp = bt_self_component_get_data(self_comp); | |
8b45963b | 1382 | BT_ASSERT(muxer_comp); |
b09a5592 PP |
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); | |
318fe670 | 1387 | |
b09a5592 PP |
1388 | status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter, |
1389 | msgs, capacity, count); | |
3fd7b79d | 1390 | if (status < 0) { |
b09a5592 PP |
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, | |
d47c10dc | 1395 | bt_common_self_message_iterator_status_string(status)); |
318fe670 | 1396 | } else { |
b09a5592 | 1397 | BT_LOGV("Returning from muxer component's message iterator's \"next\" method: " |
3fd7b79d | 1398 | "status=%s", |
d47c10dc | 1399 | bt_common_self_message_iterator_status_string(status)); |
318fe670 | 1400 | } |
958f7d11 | 1401 | |
3fd7b79d | 1402 | return status; |
958f7d11 PP |
1403 | } |
1404 | ||
1405 | BT_HIDDEN | |
ee78f405 | 1406 | bt_self_component_status muxer_input_port_connected( |
8eee8ea2 PP |
1407 | bt_self_component_filter *self_comp, |
1408 | bt_self_component_port_input *self_port, | |
1409 | const bt_port_output *other_port) | |
958f7d11 | 1410 | { |
1043fdea | 1411 | bt_self_component_status status; |
958f7d11 | 1412 | |
1043fdea PP |
1413 | status = add_available_input_port(self_comp); |
1414 | if (status) { | |
06a2cb0d PP |
1415 | /* |
1416 | * Only way to report an error later since this | |
1417 | * method does not return anything. | |
1418 | */ | |
1043fdea PP |
1419 | BT_LOGE("Cannot add one muxer component's input port: " |
1420 | "status=%s", | |
1421 | bt_self_component_status_string(status)); | |
06a2cb0d PP |
1422 | goto end; |
1423 | } | |
1424 | ||
958f7d11 | 1425 | end: |
634f394c | 1426 | return status; |
958f7d11 | 1427 | } |
c9d3ff42 | 1428 | |
a71ed05c PP |
1429 | static inline |
1430 | bt_bool muxer_upstream_msg_iters_can_all_seek_beginning( | |
1431 | GPtrArray *muxer_upstream_msg_iters) | |
c9d3ff42 | 1432 | { |
c9d3ff42 PP |
1433 | uint64_t i; |
1434 | bt_bool ret = BT_TRUE; | |
1435 | ||
a71ed05c | 1436 | for (i = 0; i < muxer_upstream_msg_iters->len; i++) { |
c9d3ff42 | 1437 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
a71ed05c | 1438 | muxer_upstream_msg_iters->pdata[i]; |
c9d3ff42 PP |
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 | ||
a71ed05c PP |
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 | ||
c9d3ff42 PP |
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); | |
ac74d1d1 | 1481 | bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; |
c9d3ff42 PP |
1482 | uint64_t i; |
1483 | ||
a71ed05c PP |
1484 | /* Seek all ended upstream iterators first */ |
1485 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1486 | i++) { | |
c9d3ff42 | 1487 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
a71ed05c | 1488 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; |
c9d3ff42 PP |
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 | } | |
a71ed05c PP |
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; | |
c9d3ff42 PP |
1523 | } |
1524 | ||
a71ed05c PP |
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); | |
c9d3ff42 | 1527 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
a71ed05c PP |
1528 | muxer_msg_iter->clock_class_expectation = |
1529 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY; | |
c9d3ff42 PP |
1530 | |
1531 | end: | |
ac74d1d1 | 1532 | return (bt_self_message_iterator_status) status; |
c9d3ff42 | 1533 | } |