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