summaryrefslogtreecommitdiff
path: root/cras
diff options
context:
space:
mode:
Diffstat (limited to 'cras')
-rw-r--r--cras/client/libcras/src/cras_server_socket.rs32
-rw-r--r--cras/client/libcras/src/libcras.rs14
2 files changed, 41 insertions, 5 deletions
diff --git a/cras/client/libcras/src/cras_server_socket.rs b/cras/client/libcras/src/cras_server_socket.rs
index 6fa84fbd..34e8cbd4 100644
--- a/cras/client/libcras/src/cras_server_socket.rs
+++ b/cras/client/libcras/src/cras_server_socket.rs
@@ -9,7 +9,23 @@ use sys_util::{net::UnixSeqpacket, ScmSocket};
use data_model::DataInit;
-const CRAS_SERVER_SOCKET_PATH: &str = "/run/cras/.cras_socket";
+/// Server socket type to connect.
+pub enum CrasSocketType {
+ /// A server socket type supports only playback function.
+ Legacy,
+ /// A server socket type supports both playback and capture functions.
+ Unified,
+}
+
+impl CrasSocketType {
+ fn sock_path(&self) -> &str {
+ match self {
+ Self::Legacy => "/run/cras/.cras_socket",
+ Self::Unified => "/run/cras/.cras_unified",
+ }
+ }
+}
+
/// A socket connecting to the CRAS audio server.
pub struct CrasServerSocket {
socket: UnixSeqpacket,
@@ -17,8 +33,18 @@ pub struct CrasServerSocket {
impl CrasServerSocket {
pub fn new() -> io::Result<CrasServerSocket> {
- let socket = UnixSeqpacket::connect(CRAS_SERVER_SOCKET_PATH)?;
- Ok(CrasServerSocket { socket })
+ Self::with_type(CrasSocketType::Legacy)
+ }
+
+ /// Creates a `CrasServerSocket` with given `CrasSocketType`.
+ ///
+ /// # Errors
+ ///
+ /// Returns the `io::Error` generated when connecting to the socket on failure.
+ pub fn with_type(socket_type: CrasSocketType) -> io::Result<CrasServerSocket> {
+ Ok(CrasServerSocket {
+ socket: UnixSeqpacket::connect(socket_type.sock_path())?,
+ })
}
/// Sends a sized and packed server messge to the server socket. The message
diff --git a/cras/client/libcras/src/libcras.rs b/cras/client/libcras/src/libcras.rs
index cdad706f..69a06ace 100644
--- a/cras/client/libcras/src/libcras.rs
+++ b/cras/client/libcras/src/libcras.rs
@@ -142,6 +142,7 @@ mod audio_socket;
use crate::audio_socket::AudioSocket;
mod cras_server_socket;
use crate::cras_server_socket::CrasServerSocket;
+pub use crate::cras_server_socket::CrasSocketType;
mod cras_shm;
use crate::cras_shm::CrasServerState;
pub mod cras_shm_stream;
@@ -227,9 +228,18 @@ impl<'a> CrasClient<'a> {
/// Returns error if error occurs while handling server message or message
/// type is incorrect
pub fn new() -> Result<Self> {
- // Create a connection to the server.
- let mut server_socket = CrasServerSocket::new()?;
+ Self::with_type(CrasSocketType::Legacy)
+ }
+ /// Tries to create a `CrasClient` with a given `CrasSocketType`.
+ ///
+ /// # Errors
+ ///
+ /// Returns error if error occurs while handling server message or message
+ /// type is incorrect.
+ pub fn with_type(socket_type: CrasSocketType) -> Result<Self> {
+ // Create a connection to the server.
+ let mut server_socket = CrasServerSocket::with_type(socket_type)?;
// Gets client ID and server state fd from server
if let ServerResult::Connected(client_id, server_state_fd) =
CrasClient::wait_for_message(&mut server_socket)?