| 1 |
// Copyright © 2017 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details. |
| 5 |
|
| 6 |
use context; |
| 7 |
use ffi; |
| 8 |
use operation; |
| 9 |
use std::ffi::CStr; |
| 10 |
use std::mem::{self, forget, MaybeUninit}; |
| 11 |
use std::os::raw::{c_int, c_void}; |
| 12 |
use std::ptr; |
| 13 |
use util::*; |
| 14 |
use *; |
| 15 |
|
| 16 |
#[derive(Debug)] |
| 17 |
pub struct Stream(*mut ffi::pa_stream); |
| 18 |
|
| 19 |
impl Stream { |
| 20 |
pub fn new<'a, CM>( |
| 21 |
c: &Context, |
| 22 |
name: &::std::ffi::CStr, |
| 23 |
ss: &SampleSpec, |
| 24 |
map: CM, |
| 25 |
) -> Option<Self> |
| 26 |
where |
| 27 |
CM: Into<Option<&'a ChannelMap>>, |
| 28 |
{ |
| 29 |
let ptr = unsafe { |
| 30 |
ffi::pa_stream_new( |
| 31 |
c.raw_mut(), |
| 32 |
name.as_ptr(), |
| 33 |
ss as *const _, |
| 34 |
to_ptr(map.into()), |
| 35 |
) |
| 36 |
}; |
| 37 |
if ptr.is_null() { |
| 38 |
None |
| 39 |
} else { |
| 40 |
Some(Stream(ptr)) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
#[doc(hidden)] |
| 45 |
pub fn raw_mut(&self) -> &mut ffi::pa_stream { |
| 46 |
unsafe { &mut *self.0 } |
| 47 |
} |
| 48 |
|
| 49 |
pub fn unref(self) { |
| 50 |
unsafe { |
| 51 |
ffi::pa_stream_unref(self.raw_mut()); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
pub fn get_state(&self) -> StreamState { |
| 56 |
StreamState::try_from(unsafe { ffi::pa_stream_get_state(self.raw_mut()) }) |
| 57 |
.expect("pa_stream_get_state returned invalid StreamState") |
| 58 |
} |
| 59 |
|
| 60 |
pub fn get_context(&self) -> Option<Context> { |
| 61 |
let ptr = unsafe { ffi::pa_stream_get_context(self.raw_mut()) }; |
| 62 |
if ptr.is_null() { |
| 63 |
return None; |
| 64 |
} |
| 65 |
|
| 66 |
let ctx = unsafe { context::from_raw_ptr(ptr) }; |
| 67 |
Some(ctx) |
| 68 |
} |
| 69 |
|
| 70 |
pub fn get_index(&self) -> u32 { |
| 71 |
unsafe { ffi::pa_stream_get_index(self.raw_mut()) } |
| 72 |
} |
| 73 |
|
| 74 |
pub fn get_device_name<'a>(&'a self) -> Result<&'a CStr> { |
| 75 |
let r = unsafe { ffi::pa_stream_get_device_name(self.raw_mut()) }; |
| 76 |
if r.is_null() { |
| 77 |
let err = if let Some(c) = self.get_context() { |
| 78 |
c.errno() |
| 79 |
} else { |
| 80 |
ffi::PA_ERR_UNKNOWN |
| 81 |
}; |
| 82 |
return Err(ErrorCode::from_error_code(err)); |
| 83 |
} |
| 84 |
Ok(unsafe { CStr::from_ptr(r) }) |
| 85 |
} |
| 86 |
|
| 87 |
pub fn is_suspended(&self) -> Result<bool> { |
| 88 |
let r = unsafe { ffi::pa_stream_is_suspended(self.raw_mut()) }; |
| 89 |
error_result!(r != 0, r) |
| 90 |
} |
| 91 |
|
| 92 |
pub fn is_corked(&self) -> Result<bool> { |
| 93 |
let r = unsafe { ffi::pa_stream_is_corked(self.raw_mut()) }; |
| 94 |
error_result!(r != 0, r) |
| 95 |
} |
| 96 |
|
| 97 |
pub fn connect_playback<'a, D, A, V, S>( |
| 98 |
&self, |
| 99 |
dev: D, |
| 100 |
attr: A, |
| 101 |
flags: StreamFlags, |
| 102 |
volume: V, |
| 103 |
sync_stream: S, |
| 104 |
) -> Result<()> |
| 105 |
where |
| 106 |
D: Into<Option<&'a CStr>>, |
| 107 |
A: Into<Option<&'a BufferAttr>>, |
| 108 |
V: Into<Option<&'a CVolume>>, |
| 109 |
S: Into<Option<&'a mut Stream>>, |
| 110 |
{ |
| 111 |
let r = unsafe { |
| 112 |
ffi::pa_stream_connect_playback( |
| 113 |
self.raw_mut(), |
| 114 |
str_to_ptr(dev.into()), |
| 115 |
to_ptr(attr.into()), |
| 116 |
flags.into(), |
| 117 |
to_ptr(volume.into()), |
| 118 |
map_to_mut_ptr(sync_stream.into(), |p| p.0), |
| 119 |
) |
| 120 |
}; |
| 121 |
error_result!((), r) |
| 122 |
} |
| 123 |
|
| 124 |
pub fn connect_record<'a, D, A>(&self, dev: D, attr: A, flags: StreamFlags) -> Result<()> |
| 125 |
where |
| 126 |
D: Into<Option<&'a CStr>>, |
| 127 |
A: Into<Option<&'a BufferAttr>>, |
| 128 |
{ |
| 129 |
let r = unsafe { |
| 130 |
ffi::pa_stream_connect_record( |
| 131 |
self.raw_mut(), |
| 132 |
str_to_ptr(dev.into()), |
| 133 |
to_ptr(attr.into()), |
| 134 |
flags.into(), |
| 135 |
) |
| 136 |
}; |
| 137 |
error_result!((), r) |
| 138 |
} |
| 139 |
|
| 140 |
pub fn disconnect(&self) -> Result<()> { |
| 141 |
let r = unsafe { ffi::pa_stream_disconnect(self.raw_mut()) }; |
| 142 |
error_result!((), r) |
| 143 |
} |
| 144 |
|
| 145 |
pub fn begin_write(&self, req_bytes: usize) -> Result<(*mut c_void, usize)> { |
| 146 |
let mut data: *mut c_void = ptr::null_mut(); |
| 147 |
let mut nbytes = req_bytes; |
| 148 |
let r = unsafe { ffi::pa_stream_begin_write(self.raw_mut(), &mut data, &mut nbytes) }; |
| 149 |
error_result!((data, nbytes), r) |
| 150 |
} |
| 151 |
|
| 152 |
pub fn cancel_write(&self) -> Result<()> { |
| 153 |
let r = unsafe { ffi::pa_stream_cancel_write(self.raw_mut()) }; |
| 154 |
error_result!((), r) |
| 155 |
} |
| 156 |
|
| 157 |
pub fn write( |
| 158 |
&self, |
| 159 |
data: *const c_void, |
| 160 |
nbytes: usize, |
| 161 |
offset: i64, |
| 162 |
seek: SeekMode, |
| 163 |
) -> Result<()> { |
| 164 |
let r = unsafe { |
| 165 |
ffi::pa_stream_write(self.raw_mut(), data, nbytes, None, offset, seek.into()) |
| 166 |
}; |
| 167 |
error_result!((), r) |
| 168 |
} |
| 169 |
|
| 170 |
pub unsafe fn peek(&self, data: *mut *const c_void, length: *mut usize) -> Result<()> { |
| 171 |
let r = ffi::pa_stream_peek(self.raw_mut(), data, length); |
| 172 |
error_result!((), r) |
| 173 |
} |
| 174 |
|
| 175 |
pub fn drop(&self) -> Result<()> { |
| 176 |
let r = unsafe { ffi::pa_stream_drop(self.raw_mut()) }; |
| 177 |
error_result!((), r) |
| 178 |
} |
| 179 |
|
| 180 |
pub fn writable_size(&self) -> Result<usize> { |
| 181 |
let r = unsafe { ffi::pa_stream_writable_size(self.raw_mut()) }; |
| 182 |
if r == ::std::usize::MAX { |
| 183 |
let err = if let Some(c) = self.get_context() { |
| 184 |
c.errno() |
| 185 |
} else { |
| 186 |
ffi::PA_ERR_UNKNOWN |
| 187 |
}; |
| 188 |
return Err(ErrorCode::from_error_code(err)); |
| 189 |
} |
| 190 |
Ok(r) |
| 191 |
} |
| 192 |
|
| 193 |
pub fn readable_size(&self) -> Result<usize> { |
| 194 |
let r = unsafe { ffi::pa_stream_readable_size(self.raw_mut()) }; |
| 195 |
if r == ::std::usize::MAX { |
| 196 |
let err = if let Some(c) = self.get_context() { |
| 197 |
c.errno() |
| 198 |
} else { |
| 199 |
ffi::PA_ERR_UNKNOWN |
| 200 |
}; |
| 201 |
return Err(ErrorCode::from_error_code(err)); |
| 202 |
} |
| 203 |
Ok(r) |
| 204 |
} |
| 205 |
|
| 206 |
pub fn update_timing_info<CB>(&self, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 207 |
where |
| 208 |
CB: Fn(&Stream, i32, *mut c_void), |
| 209 |
{ |
| 210 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 211 |
|
| 212 |
// See: A note about `wrapped` functions |
| 213 |
unsafe extern "C" fn wrapped<F>( |
| 214 |
s: *mut ffi::pa_stream, |
| 215 |
success: c_int, |
| 216 |
userdata: *mut c_void, |
| 217 |
) where |
| 218 |
F: Fn(&Stream, i32, *mut c_void), |
| 219 |
{ |
| 220 |
let mut stm = stream::from_raw_ptr(s); |
| 221 |
let cb = MaybeUninit::<F>::uninit(); |
| 222 |
let result = (*cb.as_ptr())(&mut stm, success, userdata); |
| 223 |
forget(stm); |
| 224 |
|
| 225 |
result |
| 226 |
} |
| 227 |
|
| 228 |
let r = unsafe { |
| 229 |
ffi::pa_stream_update_timing_info(self.raw_mut(), Some(wrapped::<CB>), userdata) |
| 230 |
}; |
| 231 |
if r.is_null() { |
| 232 |
let err = if let Some(c) = self.get_context() { |
| 233 |
c.errno() |
| 234 |
} else { |
| 235 |
ffi::PA_ERR_UNKNOWN |
| 236 |
}; |
| 237 |
return Err(ErrorCode::from_error_code(err)); |
| 238 |
} |
| 239 |
Ok(unsafe { operation::from_raw_ptr(r) }) |
| 240 |
} |
| 241 |
|
| 242 |
pub fn clear_state_callback(&self) { |
| 243 |
unsafe { |
| 244 |
ffi::pa_stream_set_state_callback(self.raw_mut(), None, ptr::null_mut()); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
pub fn set_state_callback<CB>(&self, _: CB, userdata: *mut c_void) |
| 249 |
where |
| 250 |
CB: Fn(&Stream, *mut c_void), |
| 251 |
{ |
| 252 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 253 |
|
| 254 |
// See: A note about `wrapped` functions |
| 255 |
unsafe extern "C" fn wrapped<F>(s: *mut ffi::pa_stream, userdata: *mut c_void) |
| 256 |
where |
| 257 |
F: Fn(&Stream, *mut c_void), |
| 258 |
{ |
| 259 |
let mut stm = stream::from_raw_ptr(s); |
| 260 |
let cb = MaybeUninit::<F>::uninit(); |
| 261 |
let result = (*cb.as_ptr())(&mut stm, userdata); |
| 262 |
forget(stm); |
| 263 |
|
| 264 |
result |
| 265 |
} |
| 266 |
|
| 267 |
unsafe { |
| 268 |
ffi::pa_stream_set_state_callback(self.raw_mut(), Some(wrapped::<CB>), userdata); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
pub fn clear_write_callback(&self) { |
| 273 |
unsafe { |
| 274 |
ffi::pa_stream_set_write_callback(self.raw_mut(), None, ptr::null_mut()); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
pub fn set_write_callback<CB>(&self, _: CB, userdata: *mut c_void) |
| 279 |
where |
| 280 |
CB: Fn(&Stream, usize, *mut c_void), |
| 281 |
{ |
| 282 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 283 |
|
| 284 |
// See: A note about `wrapped` functions |
| 285 |
unsafe extern "C" fn wrapped<F>( |
| 286 |
s: *mut ffi::pa_stream, |
| 287 |
nbytes: usize, |
| 288 |
userdata: *mut c_void, |
| 289 |
) where |
| 290 |
F: Fn(&Stream, usize, *mut c_void), |
| 291 |
{ |
| 292 |
let mut stm = stream::from_raw_ptr(s); |
| 293 |
let cb = MaybeUninit::<F>::uninit(); |
| 294 |
let result = (*cb.as_ptr())(&mut stm, nbytes, userdata); |
| 295 |
forget(stm); |
| 296 |
|
| 297 |
result |
| 298 |
} |
| 299 |
|
| 300 |
unsafe { |
| 301 |
ffi::pa_stream_set_write_callback(self.raw_mut(), Some(wrapped::<CB>), userdata); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
pub fn clear_read_callback(&self) { |
| 306 |
unsafe { |
| 307 |
ffi::pa_stream_set_read_callback(self.raw_mut(), None, ptr::null_mut()); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
pub fn set_read_callback<CB>(&self, _: CB, userdata: *mut c_void) |
| 312 |
where |
| 313 |
CB: Fn(&Stream, usize, *mut c_void), |
| 314 |
{ |
| 315 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 316 |
|
| 317 |
// See: A note about `wrapped` functions |
| 318 |
unsafe extern "C" fn wrapped<F>( |
| 319 |
s: *mut ffi::pa_stream, |
| 320 |
nbytes: usize, |
| 321 |
userdata: *mut c_void, |
| 322 |
) where |
| 323 |
F: Fn(&Stream, usize, *mut c_void), |
| 324 |
{ |
| 325 |
let mut stm = stream::from_raw_ptr(s); |
| 326 |
let cb = MaybeUninit::<F>::uninit(); |
| 327 |
let result = (*cb.as_ptr())(&mut stm, nbytes, userdata); |
| 328 |
forget(stm); |
| 329 |
|
| 330 |
result |
| 331 |
} |
| 332 |
|
| 333 |
unsafe { |
| 334 |
ffi::pa_stream_set_read_callback(self.raw_mut(), Some(wrapped::<CB>), userdata); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
pub fn cork<CB>(&self, b: i32, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 339 |
where |
| 340 |
CB: Fn(&Stream, i32, *mut c_void), |
| 341 |
{ |
| 342 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 343 |
|
| 344 |
// See: A note about `wrapped` functions |
| 345 |
unsafe extern "C" fn wrapped<F>( |
| 346 |
s: *mut ffi::pa_stream, |
| 347 |
success: c_int, |
| 348 |
userdata: *mut c_void, |
| 349 |
) where |
| 350 |
F: Fn(&Stream, i32, *mut c_void), |
| 351 |
{ |
| 352 |
let mut stm = stream::from_raw_ptr(s); |
| 353 |
let cb = MaybeUninit::<F>::uninit(); |
| 354 |
let result = (*cb.as_ptr())(&mut stm, success, userdata); |
| 355 |
forget(stm); |
| 356 |
|
| 357 |
result |
| 358 |
} |
| 359 |
|
| 360 |
let r = unsafe { ffi::pa_stream_cork(self.raw_mut(), b, Some(wrapped::<CB>), userdata) }; |
| 361 |
if r.is_null() { |
| 362 |
let err = if let Some(c) = self.get_context() { |
| 363 |
c.errno() |
| 364 |
} else { |
| 365 |
ffi::PA_ERR_UNKNOWN |
| 366 |
}; |
| 367 |
return Err(ErrorCode::from_error_code(err)); |
| 368 |
} |
| 369 |
Ok(unsafe { operation::from_raw_ptr(r) }) |
| 370 |
} |
| 371 |
|
| 372 |
pub fn get_time(&self) -> Result<USec> { |
| 373 |
let mut usec: USec = 0; |
| 374 |
let r = unsafe { ffi::pa_stream_get_time(self.raw_mut(), &mut usec) }; |
| 375 |
error_result!(usec, r) |
| 376 |
} |
| 377 |
|
| 378 |
pub fn get_latency(&self) -> Result<StreamLatency> { |
| 379 |
let mut usec: u64 = 0; |
| 380 |
let mut negative: i32 = 0; |
| 381 |
let r = unsafe { ffi::pa_stream_get_latency(self.raw_mut(), &mut usec, &mut negative) }; |
| 382 |
error_result!( |
| 383 |
if negative == 0 { |
| 384 |
StreamLatency::Positive(usec) |
| 385 |
} else { |
| 386 |
StreamLatency::Negative(usec) |
| 387 |
}, |
| 388 |
r |
| 389 |
) |
| 390 |
} |
| 391 |
|
| 392 |
pub fn get_sample_spec(&self) -> &SampleSpec { |
| 393 |
unsafe { |
| 394 |
let ptr = ffi::pa_stream_get_sample_spec(self.raw_mut()); |
| 395 |
debug_assert!(!ptr.is_null()); |
| 396 |
&*ptr |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
pub fn get_channel_map(&self) -> &ChannelMap { |
| 401 |
unsafe { |
| 402 |
let ptr = ffi::pa_stream_get_channel_map(self.raw_mut()); |
| 403 |
debug_assert!(!ptr.is_null()); |
| 404 |
&*ptr |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
pub fn get_buffer_attr(&self) -> &BufferAttr { |
| 409 |
unsafe { |
| 410 |
let ptr = ffi::pa_stream_get_buffer_attr(self.raw_mut()); |
| 411 |
debug_assert!(!ptr.is_null()); |
| 412 |
&*ptr |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
pub fn set_name<CB>(&self, name: &CStr, _: CB, userdata: *mut c_void) -> Result<Operation> |
| 417 |
where |
| 418 |
CB: Fn(&Stream, i32, *mut c_void), |
| 419 |
{ |
| 420 |
assert_eq!(mem::size_of::<CB>(), 0); |
| 421 |
|
| 422 |
// See: A note about `wrapped` functions |
| 423 |
unsafe extern "C" fn wrapped<F>( |
| 424 |
s: *mut ffi::pa_stream, |
| 425 |
success: c_int, |
| 426 |
userdata: *mut c_void, |
| 427 |
) where |
| 428 |
F: Fn(&Stream, i32, *mut c_void), |
| 429 |
{ |
| 430 |
let mut stm = stream::from_raw_ptr(s); |
| 431 |
let cb = MaybeUninit::<F>::uninit(); |
| 432 |
let result = (*cb.as_ptr())(&mut stm, success, userdata); |
| 433 |
forget(stm); |
| 434 |
|
| 435 |
result |
| 436 |
} |
| 437 |
|
| 438 |
let r = unsafe { |
| 439 |
ffi::pa_stream_set_name(self.raw_mut(), name.as_ptr(), Some(wrapped::<CB>), userdata) |
| 440 |
}; |
| 441 |
if r.is_null() { |
| 442 |
let err = if let Some(c) = self.get_context() { |
| 443 |
c.errno() |
| 444 |
} else { |
| 445 |
ffi::PA_ERR_UNKNOWN |
| 446 |
}; |
| 447 |
return Err(ErrorCode::from_error_code(err)); |
| 448 |
} |
| 449 |
Ok(unsafe { operation::from_raw_ptr(r) }) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
#[doc(hidden)] |
| 454 |
pub unsafe fn from_raw_ptr(ptr: *mut ffi::pa_stream) -> Stream { |
| 455 |
Stream(ptr) |
| 456 |
} |