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 | ||
fed72692 PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
24 | #include "logging.h" | |
25 | ||
958f7d11 | 26 | #include <babeltrace/babeltrace-internal.h> |
282c8cd0 | 27 | #include <babeltrace/compat/uuid-internal.h> |
9d408fca PP |
28 | #include <babeltrace/babeltrace.h> |
29 | #include <babeltrace/values-internal.h> | |
fed72692 | 30 | #include <babeltrace/graph/component-internal.h> |
fed72692 | 31 | #include <babeltrace/graph/notification-iterator-internal.h> |
fed72692 | 32 | #include <babeltrace/graph/connection-internal.h> |
958f7d11 PP |
33 | #include <plugins-common.h> |
34 | #include <glib.h> | |
c55a9f58 | 35 | #include <stdbool.h> |
fed72692 | 36 | #include <inttypes.h> |
f6ccaed9 | 37 | #include <babeltrace/assert-internal.h> |
0fbb9a9f | 38 | #include <stdlib.h> |
282c8cd0 | 39 | #include <string.h> |
958f7d11 | 40 | |
c3acd5f3 | 41 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
65ee897d | 42 | |
958f7d11 | 43 | struct muxer_comp { |
90157d89 PP |
44 | /* |
45 | * Array of struct | |
46 | * bt_private_connection_private_notification_iterator * | |
47 | * (weak refs) | |
48 | */ | |
958f7d11 PP |
49 | GPtrArray *muxer_notif_iters; |
50 | ||
51 | /* Weak ref */ | |
52 | struct bt_private_component *priv_comp; | |
53 | unsigned int next_port_num; | |
54 | size_t available_input_ports; | |
55 | bool error; | |
a09c6b95 | 56 | bool initializing_muxer_notif_iter; |
282c8cd0 | 57 | bool assume_absolute_clock_classes; |
958f7d11 PP |
58 | }; |
59 | ||
60 | struct muxer_upstream_notif_iter { | |
ab11110e | 61 | /* Owned by this, NULL if ended */ |
958f7d11 PP |
62 | struct bt_notification_iterator *notif_iter; |
63 | ||
089717de PP |
64 | /* |
65 | * This flag is true if the upstream notification iterator's | |
66 | * current notification must be considered for the multiplexing | |
67 | * operations. If the upstream iterator returns | |
68 | * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN, then this object | |
69 | * is considered invalid, because its current notification is | |
70 | * still the previous one, but we already took it into account. | |
71 | * | |
72 | * The value of this flag is not important if notif_iter above | |
73 | * is NULL (which means the upstream iterator is finished). | |
74 | */ | |
75 | bool is_valid; | |
958f7d11 PP |
76 | }; |
77 | ||
282c8cd0 PP |
78 | enum muxer_notif_iter_clock_class_expectation { |
79 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0, | |
80 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE, | |
81 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID, | |
82 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID, | |
83 | }; | |
84 | ||
958f7d11 | 85 | struct muxer_notif_iter { |
ab11110e PP |
86 | /* |
87 | * Array of struct muxer_upstream_notif_iter * (owned by this). | |
88 | * | |
89 | * NOTE: This array is searched in linearly to find the youngest | |
90 | * current notification. Keep this until benchmarks confirm that | |
91 | * another data structure is faster than this for our typical | |
92 | * use cases. | |
93 | */ | |
958f7d11 PP |
94 | GPtrArray *muxer_upstream_notif_iters; |
95 | ||
ab11110e | 96 | /* |
06a2cb0d | 97 | * List of "recently" connected input ports (weak) to |
ab11110e PP |
98 | * handle by this muxer notification iterator. |
99 | * muxer_port_connected() adds entries to this list, and the | |
100 | * entries are removed when a notification iterator is created | |
101 | * on the port's connection and put into | |
102 | * muxer_upstream_notif_iters above by | |
103 | * muxer_notif_iter_handle_newly_connected_ports(). | |
104 | */ | |
105 | GList *newly_connected_priv_ports; | |
958f7d11 PP |
106 | |
107 | /* Next thing to return by the "next" method */ | |
90157d89 | 108 | struct bt_notification_iterator_next_method_return next_next_return; |
958f7d11 PP |
109 | |
110 | /* Last time returned in a notification */ | |
111 | int64_t last_returned_ts_ns; | |
282c8cd0 PP |
112 | |
113 | /* Clock class expectation state */ | |
114 | enum muxer_notif_iter_clock_class_expectation clock_class_expectation; | |
115 | ||
116 | /* | |
117 | * Expected clock class UUID, only valid when | |
118 | * clock_class_expectation is | |
119 | * MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. | |
120 | */ | |
121 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; | |
958f7d11 PP |
122 | }; |
123 | ||
ab11110e PP |
124 | static |
125 | void destroy_muxer_upstream_notif_iter( | |
126 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
127 | { | |
128 | if (!muxer_upstream_notif_iter) { | |
129 | return; | |
130 | } | |
131 | ||
fed72692 PP |
132 | BT_LOGD("Destroying muxer's upstream notification iterator wrapper: " |
133 | "addr=%p, notif-iter-addr=%p, is-valid=%d", | |
134 | muxer_upstream_notif_iter, | |
135 | muxer_upstream_notif_iter->notif_iter, | |
136 | muxer_upstream_notif_iter->is_valid); | |
ab11110e | 137 | bt_put(muxer_upstream_notif_iter->notif_iter); |
ab11110e PP |
138 | g_free(muxer_upstream_notif_iter); |
139 | } | |
140 | ||
958f7d11 PP |
141 | static |
142 | struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter( | |
143 | struct muxer_notif_iter *muxer_notif_iter, | |
144 | struct bt_notification_iterator *notif_iter, | |
145 | struct bt_private_port *priv_port) | |
146 | { | |
147 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
148 | g_new0(struct muxer_upstream_notif_iter, 1); | |
149 | ||
150 | if (!muxer_upstream_notif_iter) { | |
fed72692 | 151 | BT_LOGE_STR("Failed to allocate one muxer's upstream notification iterator wrapper."); |
958f7d11 PP |
152 | goto end; |
153 | } | |
154 | ||
155 | muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter); | |
089717de | 156 | muxer_upstream_notif_iter->is_valid = false; |
958f7d11 PP |
157 | g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters, |
158 | muxer_upstream_notif_iter); | |
fed72692 PP |
159 | BT_LOGD("Added muxer's upstream notification iterator wrapper: " |
160 | "addr=%p, muxer-notif-iter-addr=%p, notif-iter-addr=%p", | |
161 | muxer_upstream_notif_iter, muxer_notif_iter, | |
162 | notif_iter); | |
958f7d11 PP |
163 | |
164 | end: | |
165 | return muxer_upstream_notif_iter; | |
166 | } | |
167 | ||
958f7d11 | 168 | static |
147337a3 PP |
169 | enum bt_component_status ensure_available_input_port( |
170 | struct bt_private_component *priv_comp) | |
958f7d11 PP |
171 | { |
172 | struct muxer_comp *muxer_comp = | |
173 | bt_private_component_get_user_data(priv_comp); | |
147337a3 | 174 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
958f7d11 | 175 | GString *port_name = NULL; |
958f7d11 | 176 | |
f6ccaed9 | 177 | BT_ASSERT(muxer_comp); |
958f7d11 PP |
178 | |
179 | if (muxer_comp->available_input_ports >= 1) { | |
180 | goto end; | |
181 | } | |
182 | ||
183 | port_name = g_string_new("in"); | |
184 | if (!port_name) { | |
fed72692 | 185 | BT_LOGE_STR("Failed to allocate a GString."); |
147337a3 | 186 | status = BT_COMPONENT_STATUS_NOMEM; |
958f7d11 PP |
187 | goto end; |
188 | } | |
189 | ||
190 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
147337a3 PP |
191 | status = bt_private_component_filter_add_input_private_port( |
192 | priv_comp, port_name->str, NULL, NULL); | |
193 | if (status != BT_COMPONENT_STATUS_OK) { | |
fed72692 PP |
194 | BT_LOGE("Cannot add input port to muxer component: " |
195 | "port-name=\"%s\", comp-addr=%p, status=%s", | |
196 | port_name->str, priv_comp, | |
197 | bt_component_status_string(status)); | |
958f7d11 PP |
198 | goto end; |
199 | } | |
200 | ||
201 | muxer_comp->available_input_ports++; | |
202 | muxer_comp->next_port_num++; | |
fed72692 PP |
203 | BT_LOGD("Added one input port to muxer component: " |
204 | "port-name=\"%s\", comp-addr=%p", | |
205 | port_name->str, priv_comp); | |
958f7d11 PP |
206 | end: |
207 | if (port_name) { | |
208 | g_string_free(port_name, TRUE); | |
209 | } | |
210 | ||
147337a3 | 211 | return status; |
958f7d11 PP |
212 | } |
213 | ||
958f7d11 | 214 | static |
147337a3 PP |
215 | enum bt_component_status create_output_port( |
216 | struct bt_private_component *priv_comp) | |
958f7d11 | 217 | { |
147337a3 PP |
218 | return bt_private_component_filter_add_output_private_port( |
219 | priv_comp, "out", NULL, NULL); | |
958f7d11 PP |
220 | } |
221 | ||
222 | static | |
223 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
224 | { | |
225 | if (!muxer_comp) { | |
226 | return; | |
227 | } | |
228 | ||
fed72692 PP |
229 | BT_LOGD("Destroying muxer component: muxer-comp-addr=%p, " |
230 | "muxer-notif-iter-count=%u", muxer_comp, | |
af18c5e7 JG |
231 | muxer_comp->muxer_notif_iters ? |
232 | muxer_comp->muxer_notif_iters->len : 0); | |
fed72692 | 233 | |
958f7d11 PP |
234 | if (muxer_comp->muxer_notif_iters) { |
235 | g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE); | |
236 | } | |
237 | ||
238 | g_free(muxer_comp); | |
239 | } | |
240 | ||
65ee897d PP |
241 | static |
242 | struct bt_value *get_default_params(void) | |
243 | { | |
244 | struct bt_value *params; | |
245 | int ret; | |
246 | ||
247 | params = bt_value_map_create(); | |
248 | if (!params) { | |
fed72692 | 249 | BT_LOGE_STR("Cannot create a map value object."); |
65ee897d PP |
250 | goto error; |
251 | } | |
252 | ||
c3acd5f3 JG |
253 | ret = bt_value_map_insert_bool(params, |
254 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); | |
65ee897d | 255 | if (ret) { |
fed72692 | 256 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
65ee897d PP |
257 | goto error; |
258 | } | |
259 | ||
260 | goto end; | |
261 | ||
262 | error: | |
263 | BT_PUT(params); | |
264 | ||
265 | end: | |
266 | return params; | |
267 | } | |
268 | ||
269 | static | |
270 | int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params) | |
271 | { | |
272 | struct bt_value *default_params = NULL; | |
273 | struct bt_value *real_params = NULL; | |
282c8cd0 | 274 | struct bt_value *assume_absolute_clock_classes = NULL; |
65ee897d | 275 | int ret = 0; |
c55a9f58 | 276 | bt_bool bool_val; |
65ee897d PP |
277 | |
278 | default_params = get_default_params(); | |
279 | if (!default_params) { | |
fed72692 PP |
280 | BT_LOGE("Cannot get default parameters: " |
281 | "muxer-comp-addr=%p", muxer_comp); | |
65ee897d PP |
282 | goto error; |
283 | } | |
284 | ||
285 | real_params = bt_value_map_extend(default_params, params); | |
286 | if (!real_params) { | |
fed72692 PP |
287 | BT_LOGE("Cannot extend default parameters map value: " |
288 | "muxer-comp-addr=%p, def-params-addr=%p, " | |
289 | "params-addr=%p", muxer_comp, default_params, | |
290 | params); | |
65ee897d PP |
291 | goto error; |
292 | } | |
293 | ||
778f1ddd | 294 | assume_absolute_clock_classes = bt_value_map_borrow(real_params, |
c3acd5f3 | 295 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); |
f6ccaed9 PP |
296 | if (assume_absolute_clock_classes && |
297 | !bt_value_is_bool(assume_absolute_clock_classes)) { | |
fed72692 PP |
298 | BT_LOGE("Expecting a boolean value for the `%s` parameter: " |
299 | "muxer-comp-addr=%p, value-type=%s", | |
300 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp, | |
301 | bt_value_type_string( | |
302 | bt_value_get_type(assume_absolute_clock_classes))); | |
65ee897d PP |
303 | goto error; |
304 | } | |
305 | ||
fed72692 | 306 | ret = bt_value_bool_get(assume_absolute_clock_classes, &bool_val); |
f6ccaed9 | 307 | BT_ASSERT(ret == 0); |
282c8cd0 | 308 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
fed72692 PP |
309 | BT_LOGD("Configured muxer component: muxer-comp-addr=%p, " |
310 | "assume-absolute-clock-classes=%d", | |
311 | muxer_comp, muxer_comp->assume_absolute_clock_classes); | |
65ee897d PP |
312 | goto end; |
313 | ||
314 | error: | |
315 | ret = -1; | |
316 | ||
317 | end: | |
318 | bt_put(default_params); | |
319 | bt_put(real_params); | |
65ee897d PP |
320 | return ret; |
321 | } | |
322 | ||
958f7d11 PP |
323 | BT_HIDDEN |
324 | enum bt_component_status muxer_init( | |
325 | struct bt_private_component *priv_comp, | |
326 | struct bt_value *params, void *init_data) | |
327 | { | |
328 | int ret; | |
329 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; | |
330 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); | |
331 | ||
fed72692 PP |
332 | BT_LOGD("Initializing muxer component: " |
333 | "comp-addr=%p, params-addr=%p", priv_comp, params); | |
334 | ||
958f7d11 | 335 | if (!muxer_comp) { |
fed72692 | 336 | BT_LOGE_STR("Failed to allocate one muxer component."); |
958f7d11 PP |
337 | goto error; |
338 | } | |
339 | ||
65ee897d PP |
340 | ret = configure_muxer_comp(muxer_comp, params); |
341 | if (ret) { | |
fed72692 PP |
342 | BT_LOGE("Cannot configure muxer component: " |
343 | "muxer-comp-addr=%p, params-addr=%p", | |
344 | muxer_comp, params); | |
65ee897d PP |
345 | goto error; |
346 | } | |
347 | ||
958f7d11 PP |
348 | muxer_comp->muxer_notif_iters = g_ptr_array_new(); |
349 | if (!muxer_comp->muxer_notif_iters) { | |
fed72692 | 350 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
351 | goto error; |
352 | } | |
353 | ||
354 | muxer_comp->priv_comp = priv_comp; | |
355 | ret = bt_private_component_set_user_data(priv_comp, muxer_comp); | |
f6ccaed9 | 356 | BT_ASSERT(ret == 0); |
147337a3 PP |
357 | status = ensure_available_input_port(priv_comp); |
358 | if (status != BT_COMPONENT_STATUS_OK) { | |
fed72692 PP |
359 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
360 | "muxer-comp-addr=%p, status=%s", | |
361 | muxer_comp, | |
362 | bt_component_status_string(status)); | |
958f7d11 PP |
363 | goto error; |
364 | } | |
365 | ||
fed72692 PP |
366 | status = create_output_port(priv_comp); |
367 | if (status) { | |
368 | BT_LOGE("Cannot create muxer component's output port: " | |
369 | "muxer-comp-addr=%p, status=%s", | |
370 | muxer_comp, | |
371 | bt_component_status_string(status)); | |
958f7d11 PP |
372 | goto error; |
373 | } | |
374 | ||
fed72692 PP |
375 | BT_LOGD("Initialized muxer component: " |
376 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", | |
377 | priv_comp, params, muxer_comp); | |
378 | ||
958f7d11 PP |
379 | goto end; |
380 | ||
381 | error: | |
382 | destroy_muxer_comp(muxer_comp); | |
383 | ret = bt_private_component_set_user_data(priv_comp, NULL); | |
f6ccaed9 | 384 | BT_ASSERT(ret == 0); |
147337a3 PP |
385 | |
386 | if (status == BT_COMPONENT_STATUS_OK) { | |
387 | status = BT_COMPONENT_STATUS_ERROR; | |
388 | } | |
958f7d11 PP |
389 | |
390 | end: | |
391 | return status; | |
392 | } | |
393 | ||
394 | BT_HIDDEN | |
395 | void muxer_finalize(struct bt_private_component *priv_comp) | |
396 | { | |
397 | struct muxer_comp *muxer_comp = | |
398 | bt_private_component_get_user_data(priv_comp); | |
399 | ||
fed72692 PP |
400 | BT_LOGD("Finalizing muxer component: comp-addr=%p", |
401 | priv_comp); | |
958f7d11 PP |
402 | destroy_muxer_comp(muxer_comp); |
403 | } | |
404 | ||
405 | static | |
406 | struct bt_notification_iterator *create_notif_iter_on_input_port( | |
407 | struct bt_private_port *priv_port, int *ret) | |
408 | { | |
5c563278 | 409 | struct bt_port *port = bt_port_borrow_from_private(priv_port); |
958f7d11 PP |
410 | struct bt_notification_iterator *notif_iter = NULL; |
411 | struct bt_private_connection *priv_conn = NULL; | |
73d5c1ad | 412 | enum bt_connection_status conn_status; |
958f7d11 | 413 | |
f6ccaed9 | 414 | BT_ASSERT(ret); |
958f7d11 | 415 | *ret = 0; |
f6ccaed9 PP |
416 | BT_ASSERT(port); |
417 | BT_ASSERT(bt_port_is_connected(port)); | |
958f7d11 | 418 | priv_conn = bt_private_port_get_private_connection(priv_port); |
f6ccaed9 | 419 | BT_ASSERT(priv_conn); |
958f7d11 | 420 | |
ab11110e PP |
421 | // TODO: Advance the iterator to >= the time of the latest |
422 | // returned notification by the muxer notification | |
423 | // iterator which creates it. | |
73d5c1ad | 424 | conn_status = bt_private_connection_create_notification_iterator( |
f42867e2 | 425 | priv_conn, ¬if_iter); |
73d5c1ad | 426 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
fed72692 PP |
427 | BT_LOGE("Cannot create upstream notification iterator on input port's connection: " |
428 | "port-addr=%p, port-name=\"%s\", conn-addr=%p, " | |
429 | "status=%s", | |
430 | port, bt_port_get_name(port), priv_conn, | |
431 | bt_connection_status_string(conn_status)); | |
958f7d11 PP |
432 | *ret = -1; |
433 | goto end; | |
434 | } | |
435 | ||
fed72692 PP |
436 | BT_LOGD("Created upstream notification iterator on input port's connection: " |
437 | "port-addr=%p, port-name=\"%s\", conn-addr=%p, " | |
438 | "notif-iter-addr=%p", | |
439 | port, bt_port_get_name(port), priv_conn, notif_iter); | |
440 | ||
958f7d11 | 441 | end: |
958f7d11 PP |
442 | bt_put(priv_conn); |
443 | return notif_iter; | |
444 | } | |
445 | ||
ab11110e | 446 | static |
089717de | 447 | enum bt_notification_iterator_status muxer_upstream_notif_iter_next( |
ab11110e PP |
448 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) |
449 | { | |
089717de | 450 | enum bt_notification_iterator_status status; |
ab11110e | 451 | |
fed72692 PP |
452 | BT_LOGV("Calling upstream notification iterator's \"next\" method: " |
453 | "muxer-upstream-notif-iter-wrap-addr=%p, notif-iter-addr=%p", | |
454 | muxer_upstream_notif_iter, | |
455 | muxer_upstream_notif_iter->notif_iter); | |
089717de | 456 | status = bt_notification_iterator_next( |
ab11110e | 457 | muxer_upstream_notif_iter->notif_iter); |
fed72692 PP |
458 | BT_LOGV("Upstream notification iterator's \"next\" method returned: " |
459 | "status=%s", bt_notification_iterator_status_string(status)); | |
ab11110e | 460 | |
089717de | 461 | switch (status) { |
ab11110e | 462 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
089717de PP |
463 | /* |
464 | * Notification iterator's current notification is valid: | |
465 | * it must be considered for muxing operations. | |
466 | */ | |
fed72692 | 467 | BT_LOGV_STR("Validated upstream notification iterator wrapper."); |
089717de | 468 | muxer_upstream_notif_iter->is_valid = true; |
ab11110e PP |
469 | break; |
470 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
089717de PP |
471 | /* |
472 | * Notification iterator's current notification is not | |
473 | * valid anymore. Return | |
474 | * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN | |
475 | * immediately. | |
476 | */ | |
fed72692 | 477 | BT_LOGV_STR("Invalidated upstream notification iterator wrapper because of BT_NOTIFICATION_ITERATOR_STATUS_AGAIN."); |
089717de | 478 | muxer_upstream_notif_iter->is_valid = false; |
ab11110e | 479 | break; |
beed0223 MD |
480 | case BT_NOTIFICATION_ITERATOR_STATUS_END: /* Fall-through. */ |
481 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: | |
ab11110e PP |
482 | /* |
483 | * Notification iterator reached the end: release it. It | |
484 | * won't be considered again to find the youngest | |
485 | * notification. | |
486 | */ | |
fed72692 | 487 | BT_LOGV_STR("Invalidated upstream notification iterator wrapper because of BT_NOTIFICATION_ITERATOR_STATUS_END or BT_NOTIFICATION_ITERATOR_STATUS_CANCELED."); |
ab11110e | 488 | BT_PUT(muxer_upstream_notif_iter->notif_iter); |
089717de PP |
489 | muxer_upstream_notif_iter->is_valid = false; |
490 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
491 | break; | |
ab11110e PP |
492 | default: |
493 | /* Error or unsupported status code */ | |
fed72692 PP |
494 | BT_LOGE("Error or unsupported status code: " |
495 | "status-code=%d", status); | |
089717de PP |
496 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
497 | break; | |
ab11110e PP |
498 | } |
499 | ||
089717de | 500 | return status; |
ab11110e PP |
501 | } |
502 | ||
503 | static | |
089717de | 504 | int muxer_notif_iter_handle_newly_connected_ports( |
ab11110e PP |
505 | struct muxer_notif_iter *muxer_notif_iter) |
506 | { | |
ab11110e PP |
507 | int ret = 0; |
508 | ||
fed72692 PP |
509 | BT_LOGV("Handling newly connected ports: " |
510 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
511 | ||
ab11110e PP |
512 | /* |
513 | * Here we create one upstream notification iterator for each | |
089717de PP |
514 | * newly connected port. We do not perform an initial "next" on |
515 | * those new upstream notification iterators: they are | |
516 | * invalidated, to be validated later. The list of newly | |
517 | * connected ports to handle here is updated by | |
518 | * muxer_port_connected(). | |
ab11110e PP |
519 | */ |
520 | while (true) { | |
521 | GList *node = muxer_notif_iter->newly_connected_priv_ports; | |
522 | struct bt_private_port *priv_port; | |
523 | struct bt_port *port; | |
524 | struct bt_notification_iterator *upstream_notif_iter = NULL; | |
525 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter; | |
526 | ||
527 | if (!node) { | |
528 | break; | |
529 | } | |
530 | ||
531 | priv_port = node->data; | |
5c563278 | 532 | port = bt_port_borrow_from_private(priv_port); |
f6ccaed9 | 533 | BT_ASSERT(port); |
ab11110e PP |
534 | |
535 | if (!bt_port_is_connected(port)) { | |
536 | /* | |
537 | * Looks like this port is not connected | |
538 | * anymore: we can't create an upstream | |
089717de PP |
539 | * notification iterator on its (non-existing) |
540 | * connection in this case. | |
ab11110e PP |
541 | */ |
542 | goto remove_node; | |
543 | } | |
544 | ||
ab11110e PP |
545 | upstream_notif_iter = create_notif_iter_on_input_port(priv_port, |
546 | &ret); | |
547 | if (ret) { | |
fed72692 | 548 | /* create_notif_iter_on_input_port() logs errors */ |
f6ccaed9 | 549 | BT_ASSERT(!upstream_notif_iter); |
ab11110e PP |
550 | goto error; |
551 | } | |
552 | ||
553 | muxer_upstream_notif_iter = | |
554 | muxer_notif_iter_add_upstream_notif_iter( | |
555 | muxer_notif_iter, upstream_notif_iter, | |
556 | priv_port); | |
ab11110e PP |
557 | BT_PUT(upstream_notif_iter); |
558 | if (!muxer_upstream_notif_iter) { | |
fed72692 PP |
559 | /* |
560 | * muxer_notif_iter_add_upstream_notif_iter() | |
561 | * logs errors. | |
562 | */ | |
ab11110e PP |
563 | goto error; |
564 | } | |
565 | ||
ab11110e PP |
566 | remove_node: |
567 | bt_put(upstream_notif_iter); | |
ab11110e PP |
568 | muxer_notif_iter->newly_connected_priv_ports = |
569 | g_list_delete_link( | |
570 | muxer_notif_iter->newly_connected_priv_ports, | |
571 | node); | |
572 | } | |
573 | ||
574 | goto end; | |
575 | ||
576 | error: | |
089717de | 577 | if (ret >= 0) { |
ab11110e PP |
578 | ret = -1; |
579 | } | |
580 | ||
581 | end: | |
ab11110e PP |
582 | return ret; |
583 | } | |
584 | ||
958f7d11 PP |
585 | static |
586 | int get_notif_ts_ns(struct muxer_comp *muxer_comp, | |
282c8cd0 | 587 | struct muxer_notif_iter *muxer_notif_iter, |
958f7d11 PP |
588 | struct bt_notification *notif, int64_t last_returned_ts_ns, |
589 | int64_t *ts_ns) | |
590 | { | |
591 | struct bt_clock_class_priority_map *cc_prio_map = NULL; | |
50842bdc PP |
592 | struct bt_clock_class *clock_class = NULL; |
593 | struct bt_clock_value *clock_value = NULL; | |
594 | struct bt_event *event = NULL; | |
958f7d11 | 595 | int ret = 0; |
282c8cd0 | 596 | const unsigned char *cc_uuid; |
fed72692 | 597 | const char *cc_name; |
958f7d11 | 598 | |
f6ccaed9 PP |
599 | BT_ASSERT(notif); |
600 | BT_ASSERT(ts_ns); | |
958f7d11 | 601 | |
fed72692 PP |
602 | BT_LOGV("Getting notification's timestamp: " |
603 | "muxer-notif-iter-addr=%p, notif-addr=%p, " | |
604 | "last-returned-ts=%" PRId64, | |
605 | muxer_notif_iter, notif, last_returned_ts_ns); | |
606 | ||
958f7d11 PP |
607 | switch (bt_notification_get_type(notif)) { |
608 | case BT_NOTIFICATION_TYPE_EVENT: | |
609 | cc_prio_map = | |
778f1ddd | 610 | bt_notification_event_borrow_clock_class_priority_map( |
958f7d11 PP |
611 | notif); |
612 | break; | |
613 | ||
614 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
615 | cc_prio_map = | |
778f1ddd | 616 | bt_notification_inactivity_borrow_clock_class_priority_map( |
958f7d11 PP |
617 | notif); |
618 | break; | |
619 | default: | |
089717de | 620 | /* All the other notifications have a higher priority */ |
fed72692 | 621 | BT_LOGV_STR("Notification has no timestamp: using the last returned timestamp."); |
958f7d11 PP |
622 | *ts_ns = last_returned_ts_ns; |
623 | goto end; | |
624 | } | |
625 | ||
626 | if (!cc_prio_map) { | |
fed72692 PP |
627 | BT_LOGE("Cannot get notification's clock class priority map: " |
628 | "notif-addr=%p", notif); | |
958f7d11 PP |
629 | goto error; |
630 | } | |
631 | ||
632 | /* | |
633 | * If the clock class priority map is empty, then we consider | |
634 | * that this notification has no time. In this case it's always | |
635 | * the youngest. | |
636 | */ | |
637 | if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) { | |
63f99cca | 638 | BT_LOGV_STR("Notification's clock class priority map contains 0 clock classes: " |
fed72692 | 639 | "using the last returned timestamp."); |
958f7d11 PP |
640 | *ts_ns = last_returned_ts_ns; |
641 | goto end; | |
642 | } | |
643 | ||
644 | clock_class = | |
778f1ddd | 645 | bt_clock_class_priority_map_borrow_highest_priority_clock_class( |
958f7d11 PP |
646 | cc_prio_map); |
647 | if (!clock_class) { | |
fed72692 PP |
648 | BT_LOGE("Cannot get the clock class with the highest priority from clock class priority map: " |
649 | "cc-prio-map-addr=%p", cc_prio_map); | |
958f7d11 PP |
650 | goto error; |
651 | } | |
652 | ||
50842bdc PP |
653 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
654 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 PP |
655 | |
656 | if (muxer_notif_iter->clock_class_expectation == | |
657 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
658 | /* | |
659 | * This is the first clock class that this muxer | |
660 | * notification iterator encounters. Its properties | |
661 | * determine what to expect for the whole lifetime of | |
662 | * the iterator without a true | |
663 | * `assume-absolute-clock-classes` parameter. | |
664 | */ | |
50842bdc | 665 | if (bt_clock_class_is_absolute(clock_class)) { |
282c8cd0 PP |
666 | /* Expect absolute clock classes */ |
667 | muxer_notif_iter->clock_class_expectation = | |
668 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
669 | } else { | |
670 | if (cc_uuid) { | |
671 | /* | |
672 | * Expect non-absolute clock classes | |
673 | * with a specific UUID. | |
674 | */ | |
675 | muxer_notif_iter->clock_class_expectation = | |
676 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; | |
677 | memcpy(muxer_notif_iter->expected_clock_class_uuid, | |
678 | cc_uuid, BABELTRACE_UUID_LEN); | |
679 | } else { | |
680 | /* | |
681 | * Expect non-absolute clock classes | |
682 | * with no UUID. | |
683 | */ | |
684 | muxer_notif_iter->clock_class_expectation = | |
685 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
686 | } | |
687 | } | |
688 | } | |
689 | ||
690 | if (!muxer_comp->assume_absolute_clock_classes) { | |
691 | switch (muxer_notif_iter->clock_class_expectation) { | |
692 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
50842bdc | 693 | if (!bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
694 | BT_LOGE("Expecting an absolute clock class, " |
695 | "but got a non-absolute one: " | |
696 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
697 | clock_class, cc_name); | |
282c8cd0 PP |
698 | goto error; |
699 | } | |
700 | break; | |
701 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: | |
50842bdc | 702 | if (bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
703 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
704 | "but got an absolute one: " | |
705 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
706 | clock_class, cc_name); | |
282c8cd0 PP |
707 | goto error; |
708 | } | |
709 | ||
710 | if (cc_uuid) { | |
fed72692 PP |
711 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
712 | "but got one with a UUID: " | |
713 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
714 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
715 | clock_class, cc_name, | |
716 | (unsigned int) cc_uuid[0], | |
717 | (unsigned int) cc_uuid[1], | |
718 | (unsigned int) cc_uuid[2], | |
719 | (unsigned int) cc_uuid[3], | |
720 | (unsigned int) cc_uuid[4], | |
721 | (unsigned int) cc_uuid[5], | |
722 | (unsigned int) cc_uuid[6], | |
723 | (unsigned int) cc_uuid[7], | |
724 | (unsigned int) cc_uuid[8], | |
725 | (unsigned int) cc_uuid[9], | |
726 | (unsigned int) cc_uuid[10], | |
727 | (unsigned int) cc_uuid[11], | |
728 | (unsigned int) cc_uuid[12], | |
729 | (unsigned int) cc_uuid[13], | |
730 | (unsigned int) cc_uuid[14], | |
731 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
732 | goto error; |
733 | } | |
734 | break; | |
735 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: | |
50842bdc | 736 | if (bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
737 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
738 | "but got an absolute one: " | |
739 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
740 | clock_class, cc_name); | |
282c8cd0 PP |
741 | goto error; |
742 | } | |
743 | ||
744 | if (!cc_uuid) { | |
fed72692 PP |
745 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
746 | "but got one with no UUID: " | |
747 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
748 | clock_class, cc_name); | |
282c8cd0 PP |
749 | goto error; |
750 | } | |
751 | ||
752 | if (memcmp(muxer_notif_iter->expected_clock_class_uuid, | |
753 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { | |
fed72692 PP |
754 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
755 | "but got one with different UUID: " | |
756 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
757 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " | |
758 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
759 | clock_class, cc_name, | |
760 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[0], | |
761 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[1], | |
762 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[2], | |
763 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[3], | |
764 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[4], | |
765 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[5], | |
766 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[6], | |
767 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[7], | |
768 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[8], | |
769 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[9], | |
770 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[10], | |
771 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[11], | |
772 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[12], | |
773 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[13], | |
774 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[14], | |
775 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[15], | |
776 | (unsigned int) cc_uuid[0], | |
777 | (unsigned int) cc_uuid[1], | |
778 | (unsigned int) cc_uuid[2], | |
779 | (unsigned int) cc_uuid[3], | |
780 | (unsigned int) cc_uuid[4], | |
781 | (unsigned int) cc_uuid[5], | |
782 | (unsigned int) cc_uuid[6], | |
783 | (unsigned int) cc_uuid[7], | |
784 | (unsigned int) cc_uuid[8], | |
785 | (unsigned int) cc_uuid[9], | |
786 | (unsigned int) cc_uuid[10], | |
787 | (unsigned int) cc_uuid[11], | |
788 | (unsigned int) cc_uuid[12], | |
789 | (unsigned int) cc_uuid[13], | |
790 | (unsigned int) cc_uuid[14], | |
791 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
792 | goto error; |
793 | } | |
794 | break; | |
795 | default: | |
796 | /* Unexpected */ | |
fed72692 PP |
797 | BT_LOGF("Unexpected clock class expectation: " |
798 | "expectation-code=%d", | |
799 | muxer_notif_iter->clock_class_expectation); | |
282c8cd0 PP |
800 | abort(); |
801 | } | |
958f7d11 PP |
802 | } |
803 | ||
804 | switch (bt_notification_get_type(notif)) { | |
805 | case BT_NOTIFICATION_TYPE_EVENT: | |
778f1ddd | 806 | event = bt_notification_event_borrow_event(notif); |
f6ccaed9 | 807 | BT_ASSERT(event); |
778f1ddd | 808 | clock_value = bt_event_borrow_clock_value(event, |
958f7d11 PP |
809 | clock_class); |
810 | break; | |
811 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
778f1ddd | 812 | clock_value = bt_notification_inactivity_borrow_clock_value( |
958f7d11 PP |
813 | notif, clock_class); |
814 | break; | |
815 | default: | |
fed72692 PP |
816 | BT_LOGF("Unexpected notification type at this point: " |
817 | "type=%d", bt_notification_get_type(notif)); | |
0fbb9a9f | 818 | abort(); |
958f7d11 PP |
819 | } |
820 | ||
821 | if (!clock_value) { | |
fed72692 PP |
822 | BT_LOGE("Cannot get notification's clock value for clock class: " |
823 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
824 | clock_class, cc_name); | |
958f7d11 PP |
825 | goto error; |
826 | } | |
827 | ||
50842bdc | 828 | ret = bt_clock_value_get_value_ns_from_epoch(clock_value, ts_ns); |
958f7d11 | 829 | if (ret) { |
fed72692 PP |
830 | BT_LOGE("Cannot get nanoseconds from Epoch of clock value: " |
831 | "clock-value-addr=%p", clock_value); | |
958f7d11 PP |
832 | goto error; |
833 | } | |
834 | ||
835 | goto end; | |
836 | ||
837 | error: | |
838 | ret = -1; | |
839 | ||
840 | end: | |
fed72692 PP |
841 | if (ret == 0) { |
842 | BT_LOGV("Found notification's timestamp: " | |
843 | "muxer-notif-iter-addr=%p, notif-addr=%p, " | |
844 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, | |
845 | muxer_notif_iter, notif, last_returned_ts_ns, | |
846 | *ts_ns); | |
847 | } | |
848 | ||
958f7d11 PP |
849 | return ret; |
850 | } | |
851 | ||
ab11110e PP |
852 | /* |
853 | * This function finds the youngest available notification amongst the | |
854 | * non-ended upstream notification iterators and returns the upstream | |
855 | * notification iterator which has it, or | |
856 | * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available | |
857 | * notification. | |
858 | * | |
859 | * This function does NOT: | |
860 | * | |
861 | * * Update any upstream notification iterator. | |
862 | * * Check for newly connected ports. | |
863 | * * Check the upstream notification iterators to retry. | |
864 | * | |
865 | * On sucess, this function sets *muxer_upstream_notif_iter to the | |
866 | * upstream notification iterator of which the current notification is | |
867 | * the youngest, and sets *ts_ns to its time. | |
868 | */ | |
958f7d11 PP |
869 | static |
870 | enum bt_notification_iterator_status | |
871 | muxer_notif_iter_youngest_upstream_notif_iter( | |
872 | struct muxer_comp *muxer_comp, | |
873 | struct muxer_notif_iter *muxer_notif_iter, | |
874 | struct muxer_upstream_notif_iter **muxer_upstream_notif_iter, | |
875 | int64_t *ts_ns) | |
876 | { | |
877 | size_t i; | |
878 | int ret; | |
879 | int64_t youngest_ts_ns = INT64_MAX; | |
880 | enum bt_notification_iterator_status status = | |
881 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
882 | ||
f6ccaed9 PP |
883 | BT_ASSERT(muxer_comp); |
884 | BT_ASSERT(muxer_notif_iter); | |
885 | BT_ASSERT(muxer_upstream_notif_iter); | |
958f7d11 PP |
886 | *muxer_upstream_notif_iter = NULL; |
887 | ||
888 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { | |
889 | struct bt_notification *notif; | |
890 | struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter = | |
891 | g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i); | |
892 | int64_t notif_ts_ns; | |
893 | ||
894 | if (!cur_muxer_upstream_notif_iter->notif_iter) { | |
ab11110e | 895 | /* This upstream notification iterator is ended */ |
fed72692 PP |
896 | BT_LOGV("Skipping ended upstream notification iterator: " |
897 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
898 | cur_muxer_upstream_notif_iter); | |
958f7d11 PP |
899 | continue; |
900 | } | |
901 | ||
f6ccaed9 | 902 | BT_ASSERT(cur_muxer_upstream_notif_iter->is_valid); |
778f1ddd | 903 | notif = bt_notification_iterator_borrow_notification( |
958f7d11 | 904 | cur_muxer_upstream_notif_iter->notif_iter); |
f6ccaed9 | 905 | BT_ASSERT(notif); |
282c8cd0 | 906 | ret = get_notif_ts_ns(muxer_comp, muxer_notif_iter, notif, |
958f7d11 | 907 | muxer_notif_iter->last_returned_ts_ns, ¬if_ts_ns); |
958f7d11 | 908 | if (ret) { |
fed72692 | 909 | /* get_notif_ts_ns() logs errors */ |
958f7d11 PP |
910 | *muxer_upstream_notif_iter = NULL; |
911 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
912 | goto end; | |
913 | } | |
914 | ||
915 | if (notif_ts_ns <= youngest_ts_ns) { | |
916 | *muxer_upstream_notif_iter = | |
917 | cur_muxer_upstream_notif_iter; | |
918 | youngest_ts_ns = notif_ts_ns; | |
919 | *ts_ns = youngest_ts_ns; | |
920 | } | |
921 | } | |
922 | ||
923 | if (!*muxer_upstream_notif_iter) { | |
924 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
925 | *ts_ns = INT64_MIN; | |
926 | } | |
927 | ||
928 | end: | |
929 | return status; | |
930 | } | |
931 | ||
932 | static | |
089717de PP |
933 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iter( |
934 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
958f7d11 | 935 | { |
089717de PP |
936 | enum bt_notification_iterator_status status = |
937 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
958f7d11 | 938 | |
fed72692 PP |
939 | BT_LOGV("Validating muxer's upstream notification iterator wrapper: " |
940 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
941 | muxer_upstream_notif_iter); | |
942 | ||
089717de PP |
943 | if (muxer_upstream_notif_iter->is_valid || |
944 | !muxer_upstream_notif_iter->notif_iter) { | |
ab11110e PP |
945 | goto end; |
946 | } | |
947 | ||
fed72692 | 948 | /* muxer_upstream_notif_iter_next() logs details/errors */ |
089717de PP |
949 | status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter); |
950 | ||
951 | end: | |
952 | return status; | |
953 | } | |
954 | ||
955 | static | |
956 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iters( | |
957 | struct muxer_notif_iter *muxer_notif_iter) | |
958 | { | |
959 | enum bt_notification_iterator_status status = | |
960 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
961 | size_t i; | |
962 | ||
fed72692 PP |
963 | BT_LOGV("Validating muxer's upstream notification iterator wrappers: " |
964 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
965 | ||
089717de PP |
966 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { |
967 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
968 | g_ptr_array_index( | |
969 | muxer_notif_iter->muxer_upstream_notif_iters, | |
970 | i); | |
971 | ||
972 | status = validate_muxer_upstream_notif_iter( | |
973 | muxer_upstream_notif_iter); | |
974 | if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
fed72692 PP |
975 | if (status < 0) { |
976 | BT_LOGE("Cannot validate muxer's upstream notification iterator wrapper: " | |
977 | "muxer-notif-iter-addr=%p, " | |
978 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
979 | muxer_notif_iter, | |
980 | muxer_upstream_notif_iter); | |
981 | } else { | |
982 | BT_LOGV("Cannot validate muxer's upstream notification iterator wrapper: " | |
983 | "muxer-notif-iter-addr=%p, " | |
984 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
985 | muxer_notif_iter, | |
986 | muxer_upstream_notif_iter); | |
987 | } | |
988 | ||
089717de PP |
989 | goto end; |
990 | } | |
744ba28b PP |
991 | |
992 | /* | |
993 | * Remove this muxer upstream notification iterator | |
994 | * if it's ended or canceled. | |
995 | */ | |
996 | if (!muxer_upstream_notif_iter->notif_iter) { | |
997 | /* | |
998 | * Use g_ptr_array_remove_fast() because the | |
999 | * order of those elements is not important. | |
1000 | */ | |
fed72692 PP |
1001 | BT_LOGV("Removing muxer's upstream notification iterator wrapper: ended or canceled: " |
1002 | "muxer-notif-iter-addr=%p, " | |
1003 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
1004 | muxer_notif_iter, muxer_upstream_notif_iter); | |
744ba28b PP |
1005 | g_ptr_array_remove_index_fast( |
1006 | muxer_notif_iter->muxer_upstream_notif_iters, | |
1007 | i); | |
1008 | i--; | |
1009 | } | |
089717de PP |
1010 | } |
1011 | ||
1012 | end: | |
1013 | return status; | |
1014 | } | |
1015 | ||
1016 | static | |
90157d89 | 1017 | struct bt_notification_iterator_next_method_return muxer_notif_iter_do_next( |
089717de PP |
1018 | struct muxer_comp *muxer_comp, |
1019 | struct muxer_notif_iter *muxer_notif_iter) | |
1020 | { | |
1021 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL; | |
90157d89 | 1022 | struct bt_notification_iterator_next_method_return next_return = { |
089717de PP |
1023 | .notification = NULL, |
1024 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, | |
1025 | }; | |
1026 | int64_t next_return_ts; | |
1027 | ||
1028 | while (true) { | |
1029 | int ret = muxer_notif_iter_handle_newly_connected_ports( | |
1030 | muxer_notif_iter); | |
1031 | ||
1032 | if (ret) { | |
fed72692 PP |
1033 | BT_LOGE("Cannot handle newly connected input ports for muxer's notification iterator: " |
1034 | "muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1035 | "ret=%d", | |
1036 | muxer_comp, muxer_notif_iter, ret); | |
089717de PP |
1037 | next_return.status = |
1038 | BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
1039 | goto end; | |
1040 | } | |
1041 | ||
1042 | next_return.status = | |
1043 | validate_muxer_upstream_notif_iters(muxer_notif_iter); | |
1044 | if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
3823b499 | 1045 | /* validate_muxer_upstream_notif_iters() logs details */ |
089717de PP |
1046 | goto end; |
1047 | } | |
ab11110e | 1048 | |
958f7d11 | 1049 | /* |
089717de PP |
1050 | * At this point, we know that all the existing upstream |
1051 | * notification iterators are valid. However the | |
1052 | * operations to validate them (during | |
1053 | * validate_muxer_upstream_notif_iters()) may have | |
1054 | * connected new ports. If no ports were connected | |
1055 | * during this operation, exit the loop. | |
958f7d11 | 1056 | */ |
089717de | 1057 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1058 | BT_LOGV("Not breaking this loop: muxer's notification iterator still has newly connected input ports to handle: " |
1059 | "muxer-comp-addr=%p", muxer_comp); | |
089717de PP |
1060 | break; |
1061 | } | |
958f7d11 PP |
1062 | } |
1063 | ||
f6ccaed9 | 1064 | BT_ASSERT(!muxer_notif_iter->newly_connected_priv_ports); |
089717de | 1065 | |
958f7d11 | 1066 | /* |
089717de PP |
1067 | * At this point we know that all the existing upstream |
1068 | * notification iterators are valid. We can find the one, | |
1069 | * amongst those, of which the current notification is the | |
1070 | * youngest. | |
958f7d11 | 1071 | */ |
089717de | 1072 | next_return.status = |
958f7d11 PP |
1073 | muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp, |
1074 | muxer_notif_iter, &muxer_upstream_notif_iter, | |
089717de PP |
1075 | &next_return_ts); |
1076 | if (next_return.status < 0 || | |
beed0223 MD |
1077 | next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_END || |
1078 | next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_CANCELED) { | |
fed72692 PP |
1079 | if (next_return.status < 0) { |
1080 | BT_LOGE("Cannot find the youngest upstream notification iterator wrapper: " | |
1081 | "status=%s", | |
1082 | bt_notification_iterator_status_string(next_return.status)); | |
1083 | } else { | |
1084 | BT_LOGV("Cannot find the youngest upstream notification iterator wrapper: " | |
1085 | "status=%s", | |
1086 | bt_notification_iterator_status_string(next_return.status)); | |
1087 | } | |
1088 | ||
958f7d11 PP |
1089 | goto end; |
1090 | } | |
1091 | ||
089717de | 1092 | if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) { |
fed72692 PP |
1093 | BT_LOGE("Youngest upstream notification iterator wrapper's timestamp is less than muxer's notification iterator's last returned timestamp: " |
1094 | "muxer-notif-iter-addr=%p, ts=%" PRId64 ", " | |
1095 | "last-returned-ts=%" PRId64, | |
1096 | muxer_notif_iter, next_return_ts, | |
1097 | muxer_notif_iter->last_returned_ts_ns); | |
089717de | 1098 | next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1099 | goto end; |
1100 | } | |
1101 | ||
fed72692 PP |
1102 | BT_LOGV("Found youngest upstream notification iterator wrapper: " |
1103 | "muxer-notif-iter-addr=%p, " | |
1104 | "muxer-upstream-notif-iter-wrap-addr=%p, " | |
1105 | "ts=%" PRId64, | |
1106 | muxer_notif_iter, muxer_upstream_notif_iter, next_return_ts); | |
f6ccaed9 PP |
1107 | BT_ASSERT(next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK); |
1108 | BT_ASSERT(muxer_upstream_notif_iter); | |
089717de PP |
1109 | next_return.notification = bt_notification_iterator_get_notification( |
1110 | muxer_upstream_notif_iter->notif_iter); | |
f6ccaed9 | 1111 | BT_ASSERT(next_return.notification); |
958f7d11 PP |
1112 | |
1113 | /* | |
089717de PP |
1114 | * We invalidate the upstream notification iterator so that, the |
1115 | * next time this function is called, | |
1116 | * validate_muxer_upstream_notif_iters() will make it valid. | |
958f7d11 | 1117 | */ |
089717de PP |
1118 | muxer_upstream_notif_iter->is_valid = false; |
1119 | muxer_notif_iter->last_returned_ts_ns = next_return_ts; | |
958f7d11 PP |
1120 | |
1121 | end: | |
089717de | 1122 | return next_return; |
958f7d11 PP |
1123 | } |
1124 | ||
1125 | static | |
ab11110e PP |
1126 | void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter) |
1127 | { | |
ab11110e PP |
1128 | if (!muxer_notif_iter) { |
1129 | return; | |
1130 | } | |
1131 | ||
fed72692 PP |
1132 | BT_LOGD("Destroying muxer component's notification iterator: " |
1133 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
1134 | ||
ab11110e | 1135 | if (muxer_notif_iter->muxer_upstream_notif_iters) { |
fed72692 | 1136 | BT_LOGD_STR("Destroying muxer's upstream notification iterator wrappers."); |
ab11110e PP |
1137 | g_ptr_array_free( |
1138 | muxer_notif_iter->muxer_upstream_notif_iters, TRUE); | |
1139 | } | |
1140 | ||
ab11110e PP |
1141 | g_list_free(muxer_notif_iter->newly_connected_priv_ports); |
1142 | g_free(muxer_notif_iter); | |
1143 | } | |
1144 | ||
1145 | static | |
1146 | int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp, | |
958f7d11 PP |
1147 | struct muxer_notif_iter *muxer_notif_iter) |
1148 | { | |
ab11110e | 1149 | struct bt_component *comp; |
544d0515 PP |
1150 | int64_t count; |
1151 | int64_t i; | |
ab11110e | 1152 | int ret = 0; |
958f7d11 | 1153 | |
ab11110e PP |
1154 | /* |
1155 | * Add the connected input ports to this muxer notification | |
1156 | * iterator's list of newly connected ports. They will be | |
1157 | * handled by muxer_notif_iter_handle_newly_connected_ports(). | |
1158 | */ | |
5c563278 | 1159 | comp = bt_component_borrow_from_private(muxer_comp->priv_comp); |
f6ccaed9 | 1160 | BT_ASSERT(comp); |
544d0515 PP |
1161 | count = bt_component_filter_get_input_port_count(comp); |
1162 | if (count < 0) { | |
fed72692 PP |
1163 | BT_LOGD("No input port to initialize for muxer component's notification iterator: " |
1164 | "muxer-comp-addr=%p, muxer-notif-iter-addr=%p", | |
1165 | muxer_comp, muxer_notif_iter); | |
ab11110e PP |
1166 | goto end; |
1167 | } | |
958f7d11 PP |
1168 | |
1169 | for (i = 0; i < count; i++) { | |
1170 | struct bt_private_port *priv_port = | |
9ac68eb1 | 1171 | bt_private_component_filter_get_input_private_port_by_index( |
958f7d11 PP |
1172 | muxer_comp->priv_comp, i); |
1173 | struct bt_port *port; | |
958f7d11 | 1174 | |
f6ccaed9 | 1175 | BT_ASSERT(priv_port); |
5c563278 | 1176 | port = bt_port_borrow_from_private(priv_port); |
f6ccaed9 | 1177 | BT_ASSERT(port); |
958f7d11 PP |
1178 | |
1179 | if (!bt_port_is_connected(port)) { | |
fed72692 PP |
1180 | BT_LOGD("Skipping input port: not connected: " |
1181 | "muxer-comp-addr=%p, port-addr=%p, port-name\"%s\"", | |
1182 | muxer_comp, port, bt_port_get_name(port)); | |
958f7d11 PP |
1183 | bt_put(priv_port); |
1184 | continue; | |
1185 | } | |
1186 | ||
06a2cb0d | 1187 | bt_put(priv_port); |
ab11110e PP |
1188 | muxer_notif_iter->newly_connected_priv_ports = |
1189 | g_list_append( | |
1190 | muxer_notif_iter->newly_connected_priv_ports, | |
958f7d11 | 1191 | priv_port); |
ab11110e | 1192 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1193 | BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: " |
1194 | "port-addr=%p, port-name=\"%s\", " | |
1195 | "muxer-notif-iter-addr=%p", port, | |
1196 | bt_port_get_name(port), muxer_notif_iter); | |
ab11110e PP |
1197 | ret = -1; |
1198 | goto end; | |
958f7d11 | 1199 | } |
fed72692 PP |
1200 | |
1201 | BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: " | |
1202 | "port-addr=%p, port-name=\"%s\", " | |
1203 | "muxer-notif-iter-addr=%p", port, | |
1204 | bt_port_get_name(port), muxer_notif_iter); | |
958f7d11 PP |
1205 | } |
1206 | ||
1207 | end: | |
958f7d11 PP |
1208 | return ret; |
1209 | } | |
1210 | ||
958f7d11 PP |
1211 | BT_HIDDEN |
1212 | enum bt_notification_iterator_status muxer_notif_iter_init( | |
90157d89 | 1213 | struct bt_private_connection_private_notification_iterator *priv_notif_iter, |
958f7d11 PP |
1214 | struct bt_private_port *output_priv_port) |
1215 | { | |
1216 | struct muxer_comp *muxer_comp = NULL; | |
1217 | struct muxer_notif_iter *muxer_notif_iter = NULL; | |
1218 | struct bt_private_component *priv_comp = NULL; | |
1219 | enum bt_notification_iterator_status status = | |
1220 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
1221 | int ret; | |
1222 | ||
90157d89 | 1223 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1224 | priv_notif_iter); |
f6ccaed9 | 1225 | BT_ASSERT(priv_comp); |
958f7d11 | 1226 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
f6ccaed9 | 1227 | BT_ASSERT(muxer_comp); |
fed72692 PP |
1228 | BT_LOGD("Initializing muxer component's notification iterator: " |
1229 | "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p", | |
1230 | priv_comp, muxer_comp, priv_notif_iter); | |
a09c6b95 PP |
1231 | |
1232 | if (muxer_comp->initializing_muxer_notif_iter) { | |
1233 | /* | |
089717de PP |
1234 | * Weird, unhandled situation detected: downstream |
1235 | * creates a muxer notification iterator while creating | |
1236 | * another muxer notification iterator (same component). | |
a09c6b95 | 1237 | */ |
fed72692 PP |
1238 | BT_LOGE("Recursive initialization of muxer component's notification iterator: " |
1239 | "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p", | |
1240 | priv_comp, muxer_comp, priv_notif_iter); | |
958f7d11 PP |
1241 | goto error; |
1242 | } | |
1243 | ||
a09c6b95 PP |
1244 | muxer_comp->initializing_muxer_notif_iter = true; |
1245 | muxer_notif_iter = g_new0(struct muxer_notif_iter, 1); | |
1246 | if (!muxer_notif_iter) { | |
fed72692 | 1247 | BT_LOGE_STR("Failed to allocate one muxer component's notification iterator."); |
ab11110e PP |
1248 | goto error; |
1249 | } | |
1250 | ||
958f7d11 PP |
1251 | muxer_notif_iter->last_returned_ts_ns = INT64_MIN; |
1252 | muxer_notif_iter->muxer_upstream_notif_iters = | |
1253 | g_ptr_array_new_with_free_func( | |
1254 | (GDestroyNotify) destroy_muxer_upstream_notif_iter); | |
1255 | if (!muxer_notif_iter->muxer_upstream_notif_iters) { | |
fed72692 | 1256 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1257 | goto error; |
1258 | } | |
1259 | ||
a09c6b95 PP |
1260 | /* |
1261 | * Add the muxer notification iterator to the component's array | |
1262 | * of muxer notification iterators here because | |
1263 | * muxer_notif_iter_init_newly_connected_ports() can cause | |
1264 | * muxer_port_connected() to be called, which adds the newly | |
1265 | * connected port to each muxer notification iterator's list of | |
1266 | * newly connected ports. | |
1267 | */ | |
1268 | g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter); | |
1269 | ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp, | |
1270 | muxer_notif_iter); | |
1271 | if (ret) { | |
fed72692 PP |
1272 | BT_LOGE("Cannot initialize newly connected input ports for muxer component's notification iterator: " |
1273 | "comp-addr=%p, muxer-comp-addr=%p, " | |
1274 | "muxer-notif-iter-addr=%p, notif-iter-addr=%p, ret=%d", | |
1275 | priv_comp, muxer_comp, muxer_notif_iter, | |
1276 | priv_notif_iter, ret); | |
a09c6b95 PP |
1277 | goto error; |
1278 | } | |
1279 | ||
90157d89 | 1280 | ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter, |
958f7d11 | 1281 | muxer_notif_iter); |
f6ccaed9 | 1282 | BT_ASSERT(ret == 0); |
fed72692 PP |
1283 | BT_LOGD("Initialized muxer component's notification iterator: " |
1284 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1285 | "notif-iter-addr=%p", | |
1286 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
958f7d11 PP |
1287 | goto end; |
1288 | ||
1289 | error: | |
a09c6b95 PP |
1290 | if (g_ptr_array_index(muxer_comp->muxer_notif_iters, |
1291 | muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) { | |
1292 | g_ptr_array_remove_index(muxer_comp->muxer_notif_iters, | |
1293 | muxer_comp->muxer_notif_iters->len - 1); | |
1294 | } | |
1295 | ||
958f7d11 | 1296 | destroy_muxer_notif_iter(muxer_notif_iter); |
90157d89 | 1297 | ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter, |
958f7d11 | 1298 | NULL); |
f6ccaed9 | 1299 | BT_ASSERT(ret == 0); |
958f7d11 PP |
1300 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
1301 | ||
1302 | end: | |
a09c6b95 | 1303 | muxer_comp->initializing_muxer_notif_iter = false; |
958f7d11 PP |
1304 | bt_put(priv_comp); |
1305 | return status; | |
1306 | } | |
1307 | ||
1308 | BT_HIDDEN | |
1309 | void muxer_notif_iter_finalize( | |
90157d89 | 1310 | struct bt_private_connection_private_notification_iterator *priv_notif_iter) |
958f7d11 PP |
1311 | { |
1312 | struct muxer_notif_iter *muxer_notif_iter = | |
90157d89 | 1313 | bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter); |
958f7d11 PP |
1314 | struct bt_private_component *priv_comp = NULL; |
1315 | struct muxer_comp *muxer_comp = NULL; | |
1316 | ||
90157d89 | 1317 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1318 | priv_notif_iter); |
f6ccaed9 | 1319 | BT_ASSERT(priv_comp); |
958f7d11 | 1320 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
fed72692 PP |
1321 | BT_LOGD("Finalizing muxer component's notification iterator: " |
1322 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1323 | "notif-iter-addr=%p", | |
1324 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
958f7d11 PP |
1325 | |
1326 | if (muxer_comp) { | |
1327 | (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters, | |
1328 | muxer_notif_iter); | |
1329 | destroy_muxer_notif_iter(muxer_notif_iter); | |
1330 | } | |
1331 | ||
1332 | bt_put(priv_comp); | |
1333 | } | |
1334 | ||
1335 | BT_HIDDEN | |
90157d89 PP |
1336 | struct bt_notification_iterator_next_method_return muxer_notif_iter_next( |
1337 | struct bt_private_connection_private_notification_iterator *priv_notif_iter) | |
958f7d11 | 1338 | { |
90157d89 | 1339 | struct bt_notification_iterator_next_method_return next_ret; |
958f7d11 | 1340 | struct muxer_notif_iter *muxer_notif_iter = |
90157d89 | 1341 | bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter); |
958f7d11 PP |
1342 | struct bt_private_component *priv_comp = NULL; |
1343 | struct muxer_comp *muxer_comp = NULL; | |
958f7d11 | 1344 | |
f6ccaed9 | 1345 | BT_ASSERT(muxer_notif_iter); |
90157d89 | 1346 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1347 | priv_notif_iter); |
f6ccaed9 | 1348 | BT_ASSERT(priv_comp); |
958f7d11 | 1349 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
f6ccaed9 | 1350 | BT_ASSERT(muxer_comp); |
958f7d11 | 1351 | |
fed72692 PP |
1352 | BT_LOGV("Muxer component's notification iterator's \"next\" method called: " |
1353 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1354 | "notif-iter-addr=%p", | |
1355 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
1356 | ||
958f7d11 PP |
1357 | /* Are we in an error state set elsewhere? */ |
1358 | if (unlikely(muxer_comp->error)) { | |
fed72692 PP |
1359 | BT_LOGE("Muxer component is already in an error state: returning BT_NOTIFICATION_ITERATOR_STATUS_ERROR: " |
1360 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1361 | "notif-iter-addr=%p", | |
1362 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
089717de PP |
1363 | next_ret.notification = NULL; |
1364 | next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
1365 | goto end; | |
958f7d11 PP |
1366 | } |
1367 | ||
089717de | 1368 | next_ret = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter); |
fed72692 PP |
1369 | if (next_ret.status < 0) { |
1370 | BT_LOGE("Cannot get next notification: " | |
1371 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1372 | "notif-iter-addr=%p, status=%s", | |
1373 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter, | |
1374 | bt_notification_iterator_status_string(next_ret.status)); | |
1375 | } else { | |
1376 | BT_LOGV("Returning from muxer component's notification iterator's \"next\" method: " | |
1377 | "status=%s, notif-addr=%p", | |
1378 | bt_notification_iterator_status_string(next_ret.status), | |
1379 | next_ret.notification); | |
1380 | } | |
958f7d11 PP |
1381 | |
1382 | end: | |
1383 | bt_put(priv_comp); | |
1384 | return next_ret; | |
1385 | } | |
1386 | ||
1387 | BT_HIDDEN | |
1388 | void muxer_port_connected( | |
1389 | struct bt_private_component *priv_comp, | |
1390 | struct bt_private_port *self_private_port, | |
1391 | struct bt_port *other_port) | |
1392 | { | |
1393 | struct bt_port *self_port = | |
5c563278 | 1394 | bt_port_borrow_from_private(self_private_port); |
958f7d11 PP |
1395 | struct muxer_comp *muxer_comp = |
1396 | bt_private_component_get_user_data(priv_comp); | |
1397 | size_t i; | |
06a2cb0d | 1398 | int ret; |
958f7d11 | 1399 | |
f6ccaed9 PP |
1400 | BT_ASSERT(self_port); |
1401 | BT_ASSERT(muxer_comp); | |
fed72692 PP |
1402 | BT_LOGD("Port connected: " |
1403 | "comp-addr=%p, muxer-comp-addr=%p, " | |
1404 | "port-addr=%p, port-name=\"%s\", " | |
1405 | "other-port-addr=%p, other-port-name=\"%s\"", | |
1406 | priv_comp, muxer_comp, self_port, bt_port_get_name(self_port), | |
1407 | other_port, bt_port_get_name(other_port)); | |
958f7d11 | 1408 | |
06a2cb0d PP |
1409 | if (bt_port_get_type(self_port) == BT_PORT_TYPE_OUTPUT) { |
1410 | goto end; | |
958f7d11 PP |
1411 | } |
1412 | ||
1413 | for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) { | |
1414 | struct muxer_notif_iter *muxer_notif_iter = | |
1415 | g_ptr_array_index(muxer_comp->muxer_notif_iters, i); | |
1416 | ||
1417 | /* | |
ab11110e PP |
1418 | * Add this port to the list of newly connected ports |
1419 | * for this muxer notification iterator. We append at | |
1420 | * the end of this list while | |
1421 | * muxer_notif_iter_handle_newly_connected_ports() | |
1422 | * removes the nodes from the beginning. | |
958f7d11 | 1423 | */ |
ab11110e PP |
1424 | muxer_notif_iter->newly_connected_priv_ports = |
1425 | g_list_append( | |
1426 | muxer_notif_iter->newly_connected_priv_ports, | |
06a2cb0d | 1427 | self_private_port); |
ab11110e | 1428 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1429 | BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: " |
1430 | "port-addr=%p, port-name=\"%s\", " | |
1431 | "muxer-notif-iter-addr=%p", self_port, | |
1432 | bt_port_get_name(self_port), muxer_notif_iter); | |
958f7d11 PP |
1433 | muxer_comp->error = true; |
1434 | goto end; | |
1435 | } | |
fed72692 PP |
1436 | |
1437 | BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: " | |
1438 | "port-addr=%p, port-name=\"%s\", " | |
1439 | "muxer-notif-iter-addr=%p", self_port, | |
1440 | bt_port_get_name(self_port), muxer_notif_iter); | |
958f7d11 PP |
1441 | } |
1442 | ||
06a2cb0d PP |
1443 | /* One less available input port */ |
1444 | muxer_comp->available_input_ports--; | |
1445 | ret = ensure_available_input_port(priv_comp); | |
1446 | if (ret) { | |
1447 | /* | |
1448 | * Only way to report an error later since this | |
1449 | * method does not return anything. | |
1450 | */ | |
fed72692 PP |
1451 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
1452 | "muxer-comp-addr=%p, status=%s", | |
1453 | muxer_comp, bt_component_status_string(ret)); | |
06a2cb0d PP |
1454 | muxer_comp->error = true; |
1455 | goto end; | |
1456 | } | |
1457 | ||
958f7d11 | 1458 | end: |
5c563278 | 1459 | return; |
958f7d11 PP |
1460 | } |
1461 | ||
1462 | BT_HIDDEN | |
1463 | void muxer_port_disconnected(struct bt_private_component *priv_comp, | |
1464 | struct bt_private_port *priv_port) | |
1465 | { | |
5c563278 | 1466 | struct bt_port *port = bt_port_borrow_from_private(priv_port); |
958f7d11 PP |
1467 | struct muxer_comp *muxer_comp = |
1468 | bt_private_component_get_user_data(priv_comp); | |
1469 | ||
f6ccaed9 PP |
1470 | BT_ASSERT(port); |
1471 | BT_ASSERT(muxer_comp); | |
fed72692 PP |
1472 | BT_LOGD("Port disconnected: " |
1473 | "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, " | |
1474 | "port-name=\"%s\"", priv_comp, muxer_comp, | |
1475 | port, bt_port_get_name(port)); | |
958f7d11 | 1476 | |
ab11110e PP |
1477 | /* |
1478 | * There's nothing special to do when a port is disconnected | |
1479 | * because this component deals with upstream notification | |
1480 | * iterators which were already created thanks to connected | |
1481 | * ports. The fact that the port is disconnected does not cancel | |
1482 | * the upstream notification iterators created using its | |
089717de PP |
1483 | * connection: they still exist, even if the connection is dead. |
1484 | * The only way to remove an upstream notification iterator is | |
1485 | * for its "next" operation to return | |
1486 | * BT_NOTIFICATION_ITERATOR_STATUS_END. | |
ab11110e | 1487 | */ |
958f7d11 PP |
1488 | if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) { |
1489 | /* One more available input port */ | |
1490 | muxer_comp->available_input_ports++; | |
fed72692 PP |
1491 | BT_LOGD("Leaving disconnected input port available for future connections: " |
1492 | "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, " | |
1493 | "port-name=\"%s\", avail-input-port-count=%zu", | |
1494 | priv_comp, muxer_comp, port, bt_port_get_name(port), | |
1495 | muxer_comp->available_input_ports); | |
958f7d11 | 1496 | } |
958f7d11 | 1497 | } |