Skip to content

Commit f5e86e5

Browse files
committed
add set_corked_state() method to Stream
the C API just has a single function to toggle state. when i first built this binding i thought that separate `cork()` and `uncork()` methods might be much nicer. however for some users it may hypothetically work better to just be able to submit a boolean state to a single function, as with the C API. additionally as it was we had almost identical duplicated code for each function. adding this new function de-duplicates the code and lets the user choose which solution works best for them.
1 parent 5a17802 commit f5e86e5

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

pulse-binding/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Dropped additional long-deprecated constants, missed in the previous version.
77
* Dropped long-deprecated `check_version()` function.
88
* Dropped deprecated `CardProfileInfo2` introspection alias.
9+
* Added `set_corked_state()` method to `Stream` providing an alternative to using `cork()` and
10+
`uncork()`.
911

1012
# 2.29.0 (March 3rd, 2025)
1113

pulse-binding/src/stream.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,34 +1264,37 @@ impl Stream {
12641264

12651265
/// Pauses playback of this stream temporarily.
12661266
///
1267-
/// Available on both playback and recording streams.
1268-
///
1269-
/// The pause operation is executed as quickly as possible. If a cork is very quickly followed
1270-
/// by an uncork, this might not actually have any effect on the stream that is output. You can
1271-
/// use [`is_corked()`] to find out whether the stream is currently paused or not. Normally a
1272-
/// stream will be created in uncorked state. If you pass [`FlagSet::START_CORKED`] as a flag
1273-
/// when connecting the stream, it will be created in corked state.
1274-
///
1275-
/// The optional callback must accept a `bool`, which indicates success.
1276-
///
1277-
/// Panics if the underlying C function returns a null pointer.
1267+
/// This simply calls [`set_corked_state()`] with a value of `true`.
12781268
///
1279-
/// [`is_corked()`]: Self::is_corked
1269+
/// [`set_corked_state()`]: Self::set_corked_state
1270+
#[inline(always)]
12801271
pub fn cork(&mut self, callback: Option<Box<dyn FnMut(bool) + 'static>>)
12811272
-> Operation<dyn FnMut(bool)>
12821273
{
1283-
let (cb_fn, cb_data): (Option<extern "C" fn(_, _, _)>, _) =
1284-
get_su_capi_params::<_, _>(callback, success_cb_proxy);
1285-
let ptr = unsafe { capi::pa_stream_cork(self.ptr, true as i32, cb_fn, cb_data) };
1286-
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
1274+
self.set_corked_state(true, callback)
12871275
}
12881276

12891277
/// Resumes playback of this stream.
12901278
///
1279+
/// This simply calls [`set_corked_state()`] with a value of `false`.
1280+
///
1281+
/// [`set_corked_state()`]: Self::set_corked_state
1282+
#[inline(always)]
1283+
pub fn uncork(&mut self, callback: Option<Box<dyn FnMut(bool) + 'static>>)
1284+
-> Operation<dyn FnMut(bool)>
1285+
{
1286+
self.set_corked_state(false, callback)
1287+
}
1288+
1289+
/// Pauses or resumes playback of this stream.
1290+
///
12911291
/// Available on both playback and recording streams.
12921292
///
1293-
/// The un-pause operation is executed as quickly as possible. If an uncork is very quickly
1294-
/// followed by a cork, this might not actually have any effect on the stream that is output.
1293+
/// Set state to `true` to cork (pause) the stream. Set state to `false` to uncork (resume) the
1294+
/// stream. Alternatively [`cork()`] and [`uncork()`] helper functions can be used.
1295+
///
1296+
/// The operation is executed as quickly as possible. If a cork is very quickly followed by an
1297+
/// uncork, or vice versa, this might not actually have any effect on the stream that is output.
12951298
/// You can use [`is_corked()`] to find out whether the stream is currently paused or not.
12961299
/// Normally a stream will be created in uncorked state. If you pass [`FlagSet::START_CORKED`]
12971300
/// as a flag when connecting the stream, it will be created in corked state.
@@ -1301,12 +1304,14 @@ impl Stream {
13011304
/// Panics if the underlying C function returns a null pointer.
13021305
///
13031306
/// [`is_corked()`]: Self::is_corked
1304-
pub fn uncork(&mut self, callback: Option<Box<dyn FnMut(bool) + 'static>>)
1307+
/// [`cork()`]: Self::cork
1308+
/// [`uncork()`]: Self::uncork
1309+
pub fn set_corked_state(&mut self, state: bool, callback: Option<Box<dyn FnMut(bool) + 'static>>)
13051310
-> Operation<dyn FnMut(bool)>
13061311
{
13071312
let (cb_fn, cb_data): (Option<extern "C" fn(_, _, _)>, _) =
13081313
get_su_capi_params::<_, _>(callback, success_cb_proxy);
1309-
let ptr = unsafe { capi::pa_stream_cork(self.ptr, false as i32, cb_fn, cb_data) };
1314+
let ptr = unsafe { capi::pa_stream_cork(self.ptr, state as i32, cb_fn, cb_data) };
13101315
Operation::from_raw(ptr, cb_data as *mut Box<dyn FnMut(bool)>)
13111316
}
13121317

0 commit comments

Comments
 (0)