1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
use jack_sys as j; use std::sync::Arc; use std::{ffi, fmt, ptr}; use crate::client::common::{sleep_on_test, CREATE_OR_DESTROY_CLIENT_MUTEX}; use crate::jack_utils::collect_strs; use crate::properties::PropertyChangeHandler; use crate::transport::Transport; use crate::{ AsyncClient, ClientOptions, ClientStatus, Error, Frames, NotificationHandler, Port, PortFlags, PortId, PortSpec, ProcessHandler, Time, Unowned, }; /// A client to interact with a JACK server. /// /// # Example /// ``` /// let c_res = jack::Client::new("rusty_client", jack::ClientOptions::NO_START_SERVER); /// match c_res { /// Ok((client, status)) => println!( /// "Managed to open client {}, with /// status {:?}!", /// client.name(), /// status /// ), /// Err(e) => println!("Failed to open client because of error: {:?}", e), /// }; /// ``` pub struct Client( *mut j::jack_client_t, Arc<()>, Option<Box<dyn PropertyChangeHandler>>, ); unsafe impl Send for Client {} impl Client { /// Opens a JACK client with the given name and options. If the client is successfully opened, /// then `Ok(client)` is returned. If there is a failure, then `Err(Error::ClientError(status))` /// will be returned. /// /// Although the client may be successful in opening, there still may be some errors minor /// errors when attempting to opening. To access these, check the returned `ClientStatus`. pub fn new(client_name: &str, options: ClientOptions) -> Result<(Self, ClientStatus), Error> { let _ = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap(); sleep_on_test(); let mut status_bits = 0; let client = unsafe { let client_name = ffi::CString::new(client_name).unwrap(); j::jack_client_open(client_name.as_ptr(), options.bits(), &mut status_bits) }; sleep_on_test(); let status = ClientStatus::from_bits(status_bits).unwrap_or_else(ClientStatus::empty); if client.is_null() { Err(Error::ClientError(status)) } else { Ok((Client(client, Arc::default(), None), status)) } } /// Begin processing in real-time using the specified `NotificationHandler` and /// `ProcessHandler`. pub fn activate_async<N, P>( self, notification_handler: N, process_handler: P, ) -> Result<AsyncClient<N, P>, Error> where N: 'static + Send + Sync + NotificationHandler, P: 'static + Send + ProcessHandler, { AsyncClient::new(self, notification_handler, process_handler) } /// The sample rate of the JACK system, as set by the user when jackd was /// started. pub fn sample_rate(&self) -> usize { let srate = unsafe { j::jack_get_sample_rate(self.raw()) }; srate as usize } /// The current CPU load estimated by JACK. It is on a scale of `0.0` to `100.0`. /// /// This is a running average of the time it takes to execute a full process cycle for all /// clients as a percentage of the real time available per cycle determined by the buffer size /// and sample rate. pub fn cpu_load(&self) -> f32 { let load = unsafe { j::jack_cpu_load(self.raw()) }; load as f32 } /// Get the name of the current client. This may differ from the name requested by `Client::new` /// as JACK will may rename a client if necessary (ie: name collision, name too long). The name /// will only the be different than the one passed to `Client::new` if the `ClientStatus` was /// `NAME_NOT_UNIQUE`. pub fn name(&self) -> &str { unsafe { let ptr = j::jack_get_client_name(self.raw()); let cstr = ffi::CStr::from_ptr(ptr); cstr.to_str().unwrap() } } /// The current maximum size that will every be passed to the process /// callback. pub fn buffer_size(&self) -> Frames { unsafe { j::jack_get_buffer_size(self.raw()) } } /// Change the buffer size passed to the process callback. /// /// This operation stops the JACK engine process cycle, then calls all registered buffer size /// callback functions before restarting the process cycle. This will cause a gap in the audio /// flow, so it should only be done at appropriate stopping points. pub fn set_buffer_size(&self, n_frames: Frames) -> Result<(), Error> { let res = unsafe { j::jack_set_buffer_size(self.raw(), n_frames) }; match res { 0 => Ok(()), _ => Err(Error::SetBufferSizeError), } } /// Get the numeric `uuid` of this client. /// /// # Remarks /// /// * Deallocates, not realtime safe. #[cfg(feature = "metadata")] pub fn uuid(&self) -> j::jack_uuid_t { unsafe { let mut uuid: j::jack_uuid_t = Default::default(); let uuid_s = j::jack_client_get_uuid(self.raw()); assert!(!uuid_s.is_null()); assert_eq!(0, j::jack_uuid_parse(uuid_s, &mut uuid)); j::jack_free(uuid_s as _); uuid } } /// Get a String representation of the `uuid` of this client. /// /// # Remarks /// /// * Allocates & deallocates, not realtime safe. pub fn uuid_string(&self) -> String { unsafe { let uuid_s = j::jack_client_get_uuid(self.raw()); assert!(!uuid_s.is_null()); let uuid = ffi::CStr::from_ptr(uuid_s) .to_str() .expect("uuid is valid string") .to_string(); j::jack_free(uuid_s as _); uuid } } //helper to get client name from uuid string. unsafe fn name_by_uuid_raw(&self, uuid: *const ::libc::c_char) -> Option<String> { let name_ptr = j::jack_get_client_name_by_uuid(self.raw(), uuid); if name_ptr.is_null() { None } else { Some( ffi::CStr::from_ptr(name_ptr) .to_str() .expect("name convert to a valid str") .to_string(), ) } } /// Get the name of a client by its numeric uuid. #[cfg(feature = "metadata")] pub fn name_by_uuid(&self, uuid: j::jack_uuid_t) -> Option<String> { let mut uuid_s = ['\0' as _; 37]; //jack_uuid_unparse expects an array of length 37 unsafe { j::jack_uuid_unparse(uuid, uuid_s.as_mut_ptr()); self.name_by_uuid_raw(uuid_s.as_ptr()) } } /// Get the name of a client by its `&str` uuid. pub fn name_by_uuid_str(&self, uuid: &str) -> Option<String> { let uuid = ffi::CString::new(uuid).unwrap(); unsafe { self.name_by_uuid_raw(uuid.as_ptr()) } } /// Returns a vector of port names that match the specified arguments /// /// `port_name_pattern` - A regular expression used to select ports by name. If `None` or zero /// lengthed, no selection based on name will be carried out. /// /// `type_name_pattern` - A regular expression used to select ports by type. If `None` or zero /// lengthed, no selection based on type will be carried out. The port type is the same one /// returned by `PortSpec::jack_port_type()`. For example, `AudioIn` and `AudioOut` are both of /// type `"32 bit float mono audio"`. /// /// `flags` - A value used to select ports by their flags. Use `PortFlags::empty()` for no flag /// selection. pub fn ports( &self, port_name_pattern: Option<&str>, type_name_pattern: Option<&str>, flags: PortFlags, ) -> Vec<String> { let pnp = ffi::CString::new(port_name_pattern.unwrap_or("")).unwrap(); let tnp = ffi::CString::new(type_name_pattern.unwrap_or("")).unwrap(); let flags = libc::c_ulong::from(flags.bits()); unsafe { let ports = j::jack_get_ports(self.raw(), pnp.as_ptr(), tnp.as_ptr(), flags); collect_strs(ports) } } /// Create a new port for the client. This is an object used for moving data of any type in or /// out of the client. Ports may be connected in various ways. /// /// The `port_spec` specifies the IO direction and data type. Oftentimes, the built-in types /// (`AudioIn`, `AudioOut`, `MidiIn`, `MidiOut`) can be used. /// /// Each port has a short name. The port's full name contains the name of the client /// concatenated with a colon (:) followed by its short name. `Port::name_size()` is the maximum /// length of the full name. Exceeding that will cause the port registration to fail and return /// `Err(())`. /// /// The `port_name` must be unique among all ports owned by this client. If the name is not /// unique, the registration will fail. pub fn register_port<PS: PortSpec>( &self, port_name: &str, port_spec: PS, ) -> Result<Port<PS>, Error> { let port_name_c = ffi::CString::new(port_name).unwrap(); let port_type_c = ffi::CString::new(port_spec.jack_port_type()).unwrap(); let port_flags = port_spec.jack_flags().bits(); let buffer_size = port_spec.jack_buffer_size(); let pp = unsafe { j::jack_port_register( self.raw(), port_name_c.as_ptr(), port_type_c.as_ptr(), libc::c_ulong::from(port_flags), buffer_size, ) }; if pp.is_null() { Err(Error::PortRegistrationError(port_name.to_string())) } else { Ok(unsafe { Port::from_raw(port_spec, self.raw(), pp, Arc::downgrade(&self.1)) }) } } /// Get a `Port` by its port id. pub fn port_by_id(&self, port_id: PortId) -> Option<Port<Unowned>> { let pp = unsafe { j::jack_port_by_id(self.raw(), port_id) }; if pp.is_null() { None } else { Some(unsafe { Port::from_raw(Unowned {}, self.raw(), pp, Arc::downgrade(&self.1)) }) } } /// Get a `Port` by its port name. pub fn port_by_name(&self, port_name: &str) -> Option<Port<Unowned>> { let port_name = ffi::CString::new(port_name).unwrap(); let pp = unsafe { j::jack_port_by_name(self.raw(), port_name.as_ptr()) }; if pp.is_null() { None } else { Some(unsafe { Port::from_raw(Unowned {}, self.raw(), pp, Arc::downgrade(&self.1)) }) } } /// The estimated time in frames that has passed since the JACK server began the current process /// cycle. pub fn frames_since_cycle_start(&self) -> Frames { unsafe { j::jack_frames_since_cycle_start(self.raw()) } } /// The estimated current time in frames. This function is intended for use in other threads /// (not the process callback). The return value can be compared with the value of /// `last_frame_time` to relate time in other threads to JACK time. To obtain better time /// information from within the process callback, see `ProcessScope`. /// /// # TODO /// - test pub fn frame_time(&self) -> Frames { unsafe { j::jack_frame_time(self.raw()) } } /// The estimated time in microseconds of the specified frame time /// /// # TODO /// - Improve test pub fn frames_to_time(&self, n_frames: Frames) -> Time { unsafe { j::jack_frames_to_time(self.raw(), n_frames) } } /// The estimated time in frames for the specified system time. /// /// # TODO /// - Improve test pub fn time_to_frames(&self, t: Time) -> Frames { unsafe { j::jack_time_to_frames(self.raw(), t) } } /// Returns `true` if the port `port` belongs to this client. pub fn is_mine<PS: PortSpec>(&self, port: &Port<PS>) -> bool { match unsafe { j::jack_port_is_mine(self.raw(), port.raw()) } { 1 => true, _ => false, } } /// Toggle input monitoring for the port with name `port_name`. /// /// `Err(Error::PortMonitorError)` is returned on failure. /// /// Only works if the port has the `CAN_MONITOR` flag, or else nothing happens. pub fn request_monitor_by_name( &self, port_name: &str, enable_monitor: bool, ) -> Result<(), Error> { let port_name_cstr = ffi::CString::new(port_name).unwrap(); let res = unsafe { j::jack_port_request_monitor_by_name( self.raw(), port_name_cstr.as_ptr(), if enable_monitor { 1 } else { 0 }, ) }; match res { 0 => Ok(()), _ => Err(Error::PortMonitorError), } } // TODO implement // /// Start/Stop JACK's "freewheel" mode. // /// // /// When in "freewheel" mode, JACK no longer waits for any external event to // /// begin the start of the next process cycle. As a result, freewheel mode // /// causes "faster than real-time" execution of a JACK graph. If possessed, // /// real-time scheduling is dropped when entering freewheel mode, and if // /// appropriate it is reacquired when stopping. // /// // /// IMPORTANT: on systems using capabilities to provide real-time scheduling // /// (i.e. Linux Kernel 2.4), if enabling freewheel, this function must be // /// called from the thread that originally called `self.activate()`. This // /// restriction does not apply to other systems (e.g. Linux Kernel 2.6 or OS // /// X). // pub pub fn set_freewheel(&self, enable: bool) -> Result<(), Error> { // let onoff = match enable { // true => 0, // false => 1, // }; // match unsafe { j::jack_set_freewheel(self.raw(), onoff) } { // 0 => Ok(()), // _ => Err(Error::FreewheelError), // } // } /// Establish a connection between two ports by their full name. /// /// When a connection exists, data written to the source port will be available to be read at /// the destination port. /// /// On failure, either a `PortAlreadyConnected` or `PortConnectionError` is returned. /// /// # Preconditions /// 1. The port types must be identical /// 2. The port flags of the `source_port` must include `IS_OUTPUT` /// 3. The port flags of the `destination_port` must include `IS_INPUT`. /// 4. Both ports must be owned by active clients. pub fn connect_ports_by_name( &self, source_port: &str, destination_port: &str, ) -> Result<(), Error> { let source_cstr = ffi::CString::new(source_port).unwrap(); let destination_cstr = ffi::CString::new(destination_port).unwrap(); let res = unsafe { j::jack_connect(self.raw(), source_cstr.as_ptr(), destination_cstr.as_ptr()) }; match res { 0 => Ok(()), ::libc::EEXIST => Err(Error::PortAlreadyConnected( source_port.to_string(), destination_port.to_string(), )), _ => Err(Error::PortConnectionError( source_port.to_string(), destination_port.to_string(), )), } } /// Establish a connection between two ports. /// /// When a connection exists, data written to the source port will be available to be read at /// the destination port. /// /// On failure, either a `PortAlreadyConnected` or `PortConnectionError` is returned. /// /// # Preconditions /// 1. The port types must be identical /// 2. The port flags of the `source_port` must include `IS_OUTPUT` /// 3. The port flags of the `destination_port` must include `IS_INPUT`. /// 4. Both ports must be owned by active clients. pub fn connect_ports<A: PortSpec, B: PortSpec>( &self, source_port: &Port<A>, destination_port: &Port<B>, ) -> Result<(), Error> { let _ = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap(); self.connect_ports_by_name(&source_port.name()?, &destination_port.name()?) } /// Remove all connections to/from the port. pub fn disconnect<PS>(&self, port: &Port<PS>) -> Result<(), Error> { let res = unsafe { j::jack_port_disconnect(self.raw(), port.raw()) }; match res { 0 => Ok(()), _ => Err(Error::PortDisconnectionError), } } pub fn unregister_port<PS>(&self, port: Port<PS>) -> Result<(), Error> { let res = unsafe { j::jack_port_unregister(self.raw(), port.raw()) }; match res { 0 => Ok(()), _ => Err(Error::PortDisconnectionError), } } /// Remove a connection between two ports. pub fn disconnect_ports<A: PortSpec, B: PortSpec>( &self, source: &Port<A>, destination: &Port<B>, ) -> Result<(), Error> { self.disconnect_ports_by_name(&source.name()?, &destination.name()?) } /// Remove a connection between two ports. pub fn disconnect_ports_by_name( &self, source_port: &str, destination_port: &str, ) -> Result<(), Error> { let source_port = ffi::CString::new(source_port).unwrap(); let destination_port = ffi::CString::new(destination_port).unwrap(); let res = unsafe { j::jack_disconnect(self.raw(), source_port.as_ptr(), destination_port.as_ptr()) }; match res { 0 => Ok(()), _ => Err(Error::PortDisconnectionError), } } /// The buffer size of a port type /// /// # Unsafe /// /// * This function may only be called in a buffer size callback. pub unsafe fn type_buffer_size(&self, port_type: &str) -> usize { let port_type = ffi::CString::new(port_type).unwrap(); j::jack_port_type_get_buffer_size(self.raw(), port_type.as_ptr()) } /// Expose the underlying ffi pointer. /// /// This is mostly for use within the jack crate itself. #[inline(always)] pub fn raw(&self) -> *mut j::jack_client_t { self.0 } /// Create a `Client` from an ffi pointer. /// /// This is mostly for use within the jack crate itself. pub unsafe fn from_raw(p: *mut j::jack_client_t) -> Self { Client(p, Arc::default(), None) } /// Get a `Transport` object associated with this client. /// /// # Remarks /// * The transport methods will only work during this client's lifetime. pub fn transport(&self) -> Transport { Transport { client_ptr: self.0, client_life: Arc::downgrade(&self.1), } } /// Register a property change handler for this client. /// /// # Remarks /// * The handler isn't called until after this client is activated. /// /// # Panics /// Calling this method more than once on any given client with cause a panic. #[cfg(feature = "metadata")] pub fn register_property_change_handler<H: PropertyChangeHandler + 'static>( &mut self, handler: H, ) -> Result<(), Error> { assert!(self.2.is_none()); let handler = Box::into_raw(Box::new(handler)); unsafe { self.2 = Some(Box::from_raw(handler)); if j::jack_set_property_change_callback( self.raw(), Some(crate::properties::property_changed::<H>), std::mem::transmute::<_, _>(handler), ) == 0 { Ok(()) } else { Err(Error::UnknownError) } } } } /// Close the client. impl Drop for Client { fn drop(&mut self) { let _ = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap(); debug_assert!(!self.raw().is_null()); // Rep invariant // Close the client sleep_on_test(); let res = unsafe { j::jack_client_close(self.raw()) }; // close the client sleep_on_test(); assert_eq!(res, 0); self.0 = ptr::null_mut(); } } impl fmt::Debug for Client { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{:?}", ClientInfo::from(self)) } } /// `ProcessScope` provides information on the client and frame time information within a process /// callback. #[derive(Debug)] pub struct ProcessScope { client_ptr: *mut j::jack_client_t, // Used to allow safe access to IO port buffers n_frames: Frames, } impl ProcessScope { /// The number of frames in the current process cycle. #[inline(always)] pub fn n_frames(&self) -> Frames { self.n_frames } /// The precise time at the start of the current process cycle. This function may only be used /// from the process callback, and can be used to interpret timestamps generated by /// `self.frame_time()` in other threads, with respect to the current process cycle. pub fn last_frame_time(&self) -> Frames { unsafe { j::jack_last_frame_time(self.client_ptr()) } } /// The estimated time in frames that has passed since the JACK server began the current process /// cycle. pub fn frames_since_cycle_start(&self) -> Frames { unsafe { j::jack_frames_since_cycle_start(self.client_ptr()) } } /// Provides the internal cycle timing information as used by most of the other time related /// functions. This allows the caller to map between frame counts and microseconds with full /// precision (i.e. without rounding frame times to integers), and also provides e.g. the /// microseconds time of the start of the current cycle directly (it has to be computed /// otherwise). /// /// `Err(Error::TimeError)` is returned on failure. /// `Err(Error::WeakFunctionNotFound)` if the function does not exist. pub fn cycle_times(&self) -> Result<CycleTimes, Error> { let mut current_frames: Frames = 0; let mut current_usecs: Time = 0; let mut next_usecs: Time = 0; let mut period_usecs: libc::c_float = 0.0; let jack_get_cycle_times = { match *j::jack_get_cycle_times { Some(f) => f, None => return Err(Error::WeakFunctionNotFound), } }; let res = unsafe { (jack_get_cycle_times)( self.client_ptr(), &mut current_frames, &mut current_usecs, &mut next_usecs, &mut period_usecs, ) }; match res { 0 => Ok(CycleTimes { current_frames, current_usecs, next_usecs, period_usecs, }), _ => Err(Error::TimeError), } } /// Expose the `client_ptr` for low level purposes. /// /// This is mostly for use within the jack crate itself. #[inline(always)] pub fn client_ptr(&self) -> *mut j::jack_client_t { self.client_ptr } /// Create a `ProcessScope` for the client with the given pointer and the specified amount of /// frames. /// /// This is mostly for use within the jack crate itself. pub unsafe fn from_raw(n_frames: Frames, client_ptr: *mut j::jack_client_t) -> Self { ProcessScope { n_frames, client_ptr, } } } /// Internal cycle timing information. #[derive(Clone, Copy, Debug, PartialEq)] pub struct CycleTimes { pub current_frames: Frames, pub current_usecs: Time, pub next_usecs: Time, pub period_usecs: libc::c_float, } #[derive(Debug)] struct ClientInfo { name: String, sample_rate: usize, buffer_size: u32, cpu_usage: String, ports: Vec<String>, frame_time: Frames, } impl<'a> From<&'a Client> for ClientInfo { fn from(c: &Client) -> ClientInfo { ClientInfo { name: c.name().into(), sample_rate: c.sample_rate(), buffer_size: c.buffer_size(), cpu_usage: format!("{}%", c.cpu_load() / 100.0), ports: c.ports(None, None, PortFlags::empty()), frame_time: c.frame_time(), } } }