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