libpulse_binding/
lib.rs

1// Copyright 2017 Lyndon Brown
2//
3// This file is part of the PulseAudio Rust language binding.
4//
5// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not
6// copy, modify, or distribute this file except in compliance with said license. You can find copies
7// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at
8// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0>
9// respectively.
10//
11// Portions of documentation are copied from the LGPL 2.1+ licensed PulseAudio C headers on a
12// fair-use basis, as discussed in the overall project readme (available in the git repository).
13
14//! A binding for the PulseAudio system library (`libpulse`).
15//!
16//! # About
17//!
18//! This binding enables Rust projects to make use of the [PulseAudio] client system library. It
19//! builds upon the [separate raw FFI crate][sys] to provide a more “Rusty” interface.
20//!
21//! The documentation below and throughout this crate has been largely copied (under fair-use terms)
22//! from those in the PulseAudio C API header files, and adjusted where appropriate to fit any
23//! differences, thus it should not be too unfamiliar to those of you already familiar with the C
24//! API.
25//!
26//! <div style="border-left:#bda000aa 5px solid; padding:0.5em 1em; margin:1em 0 0.6em 0; background-color:#aaa2">
27//! <h3 style="color:#bfa800; margin-top:0.2em">
28//! <span aria-hidden="true">
29//! <svg style="width:1em; height:1em; margin-bottom:-0.15em; fill:currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg>
30//! </span>
31//! Warning
32//! </h3>
33//! <p>
34//! The PulseAudio API, even in this Rust-ified form, is not the easiest thing to understand how to
35//! make use of. Furthermore, the somewhat complex underlying C API imposes certain limitations upon
36//! just how safe and simple an interface this binding can reasonably offer. One particularly
37//! notable example is the threaded mainloop locking mechanism, which uses a perfectly legitimate
38//! design, but one that happens to conflict with what is typically used in Rust code; it does not
39//! fit perfectly with the Rust borrow checking mechanism and thus you cannot rely upon the borrow
40//! checker to prevent unsafe use as much as is typical.
41//! </p>
42//! </div>
43//!
44//! # Introduction
45//!
46//! The PulseAudio API comes in two flavours to accommodate different styles of applications and
47//! different needs in complexity:
48//!
49//! * The complete but somewhat complicated to use asynchronous API.
50//! * The simplified, easy to use, but limited synchronous API.
51//!
52//! ## Simple API
53//!
54//! Use this if you develop your program in synchronous style and just need a way to play or record
55//! data on the sound server. This functionality is kept in the separate [`libpulse-simple-binding`]
56//! crate. See that for details.
57//!
58//! ## Asynchronous API
59//!
60//! Use this if you develop your programs in asynchronous, event loop based style or if you want to
61//! use the advanced features of the PulseAudio API. A guide can be found in the
62//! [`mainloop`](mod@mainloop) module.
63//!
64//! By using the built-in threaded main loop, it is possible to achieve a pseudo-synchronous API,
65//! which can be useful in synchronous applications where the simple API is insufficient.
66//!
67//! ## Threads
68//!
69//! The PulseAudio client libraries are not designed to be directly thread-safe. They are however
70//! designed to be re-entrant and thread-aware.
71//!
72//! To use the libraries in a threaded environment, you must assure that all objects are only used
73//! in one thread at a time. Normally, this means that all objects belonging to a single context
74//! must be accessed from the same thread.
75//!
76//! The included main loop implementation is also not thread safe. Take care to make sure event
77//! objects are not manipulated when any other code is using the main loop.
78//!
79//! Note that some objects implement the `Sync` trait, despite not truly being thread-safe. The
80//! reason for this is that when the threaded mainloop is used (the most common one), its lock
81//! provides the thread safety, and when such types are used behind that lock, they are then `Sync`
82//! safe. If you use the standard mainloop though, then you would have to add an `Arc` wrapper to
83//! make them safe. Not having `Sync` would force the threaded mainloop case to require unnecessary
84//! and undesirable `Arc` wrappers. This is an unfortunate compromise resulting from the
85//! complication of needing to support multiple mainloop designs, and the threaded mainloop design
86//! being built upon a non-wrapper lock that is not typical of Rust code.
87//!
88//! ## Logging
89//!
90//! You can configure different logging parameters for the PulseAudio client libraries. The
91//! following environment variables are recognized:
92//!
93//! * `PULSE_LOG`: Maximum log level required. Bigger values result in a more verbose logging
94//!   output. The following values are recognized:
95//!   * `0`: Error messages
96//!   * `1`: Warning messages
97//!   * `2`: Notice messages
98//!   * `3`: Info messages
99//!   * `4`: Debug messages
100//! * `PULSE_LOG_SYSLOG`: If defined, force all client libraries to log their output using the
101//!   `syslog(3)` mechanism. Default behavior is to log all output to `stderr`.
102//! * `PULSE_LOG_JOURNAL`: If defined, force all client libraries to log their output using the
103//!   systemd journal. If both `PULSE_LOG_JOURNAL` and `PULSE_LOG_SYSLOG` are defined, logging to
104//!   the systemd journal takes a higher precedence. Each message originating library file name and
105//!   function are included by default through the journal fields `CODE_FILE`, `CODE_FUNC`, and
106//!   `CODE_LINE`. Any backtrace attached to the logging message is sent through the
107//!   PulseAudio-specific journal field `PULSE_BACKTRACE`. This environment variable has no effect
108//!   if PulseAudio was compiled without systemd journal support.
109//! * `PULSE_LOG_COLORS`: If defined, enables colored logging output.
110//! * `PULSE_LOG_TIME`: If defined, include timestamps with each message.
111//! * `PULSE_LOG_FILE`: If defined, include each message originating file name.
112//! * `PULSE_LOG_META`: If defined, include each message originating file name and path relative to
113//!   the PulseAudio source tree root.
114//! * `PULSE_LOG_LEVEL`: If defined, include a log level prefix with each message. Respectively, the
115//!   prefixes "E", "W", "N", "I", "D" stands for Error, Warning, Notice, Info, and Debugging.
116//! * `PULSE_LOG_BACKTRACE`: Number of functions to display in the backtrace. If this variable is
117//!   not defined, or has a value of zero, no backtrace is shown.
118//! * `PULSE_LOG_BACKTRACE_SKIP`: Number of backtrace levels to skip, from the function printing the
119//!   log message downwards.
120//! * `PULSE_LOG_NO_RATE_LIMIT`: If defined, do not rate limit the logging output. Rate limiting
121//!   skips certain log messages when their frequency is considered too high.
122//!
123//! # Usage
124//!
125//! Start by adding a dependency on the crate in your program’s `Cargo.toml` file. Note that it is
126//! recommended that you rename the crate such that you can refer to it by a shorter name within
127//! your code (such as `pulse`, as used within examples within this crate’s documentation). Such
128//! renaming can be done [within your `Cargo.toml` file][rename] with cargo version 1.31 or newer,
129//! or otherwise with `extern crate` statements.
130//!
131//! See sub-modules for further information.
132//!
133//! Note that establishing a connection with PulseAudio involves creation of [context](mod@context)
134//! and [mainloop](mod@mainloop) objects and both of these must live for the entire duration that
135//! your program wishes to communicate with PulseAudio. The context object represents the actual
136//! communicaiton channel with PA. The mainloop object is used for controlled processing of messages
137//! sent to your client program from PA and execution of your callbacks. This detail can be ignored
138//! if you use the 'simple' binding interface.
139//!
140//! <div style="border-left:#bda000aa 5px solid; padding:0.5em 1em; margin:1em 0 0.6em 0; background-color:#aaa2">
141//! <h3 style="color:#bfa800; margin-top:0.2em">
142//! <span aria-hidden="true">
143//! <svg style="width:1em; height:1em; margin-bottom:-0.15em; fill:currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M569.517 440.013C587.975 472.007 564.806 512 527.94 512H48.054c-36.937 0-59.999-40.055-41.577-71.987L246.423 23.985c18.467-32.009 64.72-31.951 83.154 0l239.94 416.028zM288 354c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path></svg>
144//! </span>
145//! Warning
146//! </h3>
147//! <p>
148//! It is important that you read the <code>COMPATIBILITY.md</code> guide available in the
149//! <a href="https://github.com/jnqnfe/pulse-binding-rust">code repository</a> to understand the
150//! topic of compatibility with different versions of PulseAudio.
151//! </p>
152//! </div>
153//!
154//! [sys]: https://docs.rs/libpulse-sys
155//! [`libpulse-simple-binding`]: https://docs.rs/libpulse-simple-binding
156//! [PulseAudio]: https://en.wikipedia.org/wiki/PulseAudio
157//! [rename]: https://doc.rust-lang.org/1.31.0/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
158//! [code repository]: https://github.com/jnqnfe/pulse-binding-rust
159
160#![doc(
161    html_logo_url = "https://github.com/jnqnfe/pulse-binding-rust/raw/master/logo.svg",
162    html_favicon_url = "https://github.com/jnqnfe/pulse-binding-rust/raw/master/favicon.ico"
163)]
164
165#![warn(missing_docs)]
166
167#![cfg_attr(docsrs, feature(doc_cfg))]
168
169extern crate libpulse_sys as capi;
170
171pub mod callbacks;
172pub mod channelmap;
173pub mod context;
174pub mod def;
175pub mod direction;
176pub mod error;
177pub mod format;
178pub mod mainloop;
179pub mod operation;
180pub mod proplist;
181pub mod sample;
182pub mod stream;
183pub mod time;
184pub mod utf8;
185pub mod util;
186pub mod version;
187pub mod volume;