Skip to content

Commit 1b07beb

Browse files
committed
pipewire: validity fixes on polling things
1 parent b1a509d commit 1b07beb

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

include/libremidi/backends/pipewire/context.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ struct pipewire_context
172172

173173
// Add a manual 1ms event loop iteration at the end of
174174
// ctor to ensure synchronous clients will still see the ports
175+
pw_loop_enter(this->lp);
175176
pw_loop_iterate(this->lp, 1);
177+
pw_loop_leave(this->lp);
176178
}
177179

178180
void initialize_observation()

include/libremidi/backends/pipewire/helpers.hpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,18 @@ struct pipewire_helpers
156156
{
157157
// Note: called from a std::jthread.
158158
assert(this->global_context);
159+
auto lp = this->global_context->lp;
160+
if (!lp)
161+
return;
162+
159163
if (int fd = this->global_context->get_fd(); fd != -1)
160164
{
161165
fds[0] = {.fd = fd, .events = POLLIN, .revents = 0};
162166
current_state = poll_state::in_poll;
163167

168+
// pw_loop_iterate requires the loop to be entered first.
169+
pw_loop_enter(lp);
170+
164171
for (;;)
165172
{
166173
if (int err = poll(fds, 2, -1); err < 0)
@@ -174,13 +181,10 @@ struct pipewire_helpers
174181
// Check pipewire fd:
175182
if (fds[0].revents & POLLIN)
176183
{
177-
if (auto lp = this->global_context->lp)
184+
int result = pw_loop_iterate(lp, 0);
185+
if (result < 0)
178186
{
179-
int result = pw_loop_iterate(lp, 0);
180-
if (result < 0)
181-
{
182-
LIBREMIDI_LOG(spa_strerror(result));
183-
}
187+
LIBREMIDI_LOG(spa_strerror(result));
184188
}
185189
fds[0].revents = 0;
186190
}
@@ -191,6 +195,8 @@ struct pipewire_helpers
191195
break;
192196
}
193197
}
198+
199+
pw_loop_leave(lp);
194200
}
195201
current_state = poll_state::not_in_poll;
196202
}
@@ -347,7 +353,11 @@ struct pipewire_helpers
347353
{
348354
// Wait for the pipewire server to send us back our node's info
349355
for (int i = 0; i < 1000; i++)
356+
{
350357
this->filter->synchronize_node();
358+
if (this->filter->filter_node_id() != 4294967295)
359+
break;
360+
}
351361

352362
auto this_node = this->filter->filter_node_id();
353363
auto& midi = this->global_context->current_graph.software_midi;
@@ -380,7 +390,11 @@ struct pipewire_helpers
380390
{
381391
// Wait for the pipewire server to send us back our node's info
382392
for (int i = 0; i < 1000; i++)
393+
{
383394
this->filter->synchronize_node();
395+
if (this->filter->filter_node_id() != 4294967295)
396+
break;
397+
}
384398

385399
auto this_node = this->filter->filter_node_id();
386400
auto& midi = this->global_context->current_graph.software_midi;

0 commit comments

Comments
 (0)