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