Class PulseAudioSystem


  • public class PulseAudioSystem
    extends AudioSystem
    Implements an AudioSystem using the native PulseAudio API/library.
    Author:
    Lyubomir Marinov
    • Field Detail

      • MEDIA_ROLE_EVENT

        public static final String MEDIA_ROLE_EVENT
        The PulseAudio logic role of media which represents an event, notification.
        See Also:
        Constant Field Values
      • MEDIA_ROLE_PHONE

        public static final String MEDIA_ROLE_PHONE
        The PulseAudio logic role of media which represents telephony call audio.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PulseAudioSystem

        public PulseAudioSystem()
                         throws Exception
        Initializes a new PulseAudioSystem instance.
        Throws:
        Exception - if anything goes wrong while initializing the new instance
    • Method Detail

      • corkStream

        public static void corkStream​(long stream,
                                      boolean b)
                               throws IOException
        Pause or resume the playback of a specific PulseAudio stream temporarily.
        Parameters:
        stream - the PulseAudio stream to pause or resume the playback of
        b - true to pause or false to resume the playback of the specified stream
        Throws:
        IOException
      • getPulseAudioSystem

        public static PulseAudioSystem getPulseAudioSystem()
        Returns the one and only instance of PulseAudioSystem known to the AudioSystem framework.
        Returns:
        the one and only instance of PulseAudioSystem known to the AudioSystem framework or null if no such system is known to the AudioSystem framework
      • createRenderer

        public javax.media.Renderer createRenderer​(boolean playback)
        Initializes a new Renderer instance which is to either perform playback on or sound a notification through a device contributed by this system. The (default) implementation of AudioSystem ignores the value of the playback argument and delegates to DeviceSystem.createRenderer(). Overrides the implementation provided by AudioSystem because the PulseAudio Renderer implementation does not follow the convention of AudioSystem.
        Overrides:
        createRenderer in class AudioSystem
        Parameters:
        playback - true if the new instance is to perform playback or false if the new instance is to sound a notification
        Returns:
        a new Renderer instance which is to either perform playback on or sound a notification through a device contributed by this system
      • createStream

        public long createStream​(int sampleRate,
                                 int channels,
                                 String mediaName,
                                 String mediaRole)
                          throws RuntimeException
        Initializes a new PulseAudio stream which is to input or output audio at a specific sample rate and with a specific number of channels. The new audio stream is to be associated with a specific human-readable name and is to have a specific PulseAudio logic role.
        Parameters:
        sampleRate - the sample rate at which the new PulseAudio stream is to input or output
        channels - the number of channels of the audio to be input or output by the new PulseAudio stream
        mediaName - the human-readable name of the new PulseAudio stream
        mediaRole - the PulseAudio logic role of the new stream
        Returns:
        a new PulseAudio stream which is to input or output audio at the specified sampleRate, with the specified number of channels, to be associated with the specified human-readable mediaName, and have the specified PulseAudio logic mediaRole
        Throws:
        RuntimeException
      • doInitialize

        protected void doInitialize()
                             throws Exception
        Invoked by DeviceSystem.initialize() to perform the very logic of the initialization of this DeviceSystem. This instance has been prepared for initialization by an earlier call to DeviceSystem.preInitialize() and the initialization will be completed with a subsequent call to DeviceSystem.postInitialize().
        Specified by:
        doInitialize in class DeviceSystem
        Throws:
        Exception - if an error occurs during the initialization of this instance. The initialization of this instance will be completed with a subsequent call to postInitialize() regardless of any Exception thrown by doInitialize().
      • getContext

        public long getContext()
        Returns the connection context for asynchronous communication with the PulseAudio server. If such a context does not exist, it is created.
        Returns:
        the connection context for asynchronous communication with the PulseAudio server
      • lockMainloop

        public void lockMainloop()
        Locks the PulseAudio event loop object associated with this PulseAudioSystem, effectively blocking the PulseAudio event loop thread from processing events. May be used to enforce exclusive access to all PulseAudio objects attached to the PulseAudio event loop. The lock is recursive. The method may not be called inside the PulseAudio event loop thread. Events that are dispatched from the PulseAudio event loop thread are executed with the lock held.
      • signalMainloop

        public void signalMainloop​(boolean waitForAccept)
        Signals all threads waiting for a signalling event in waitMainloop().
        Parameters:
        waitForAccept - true to not return before the signal is accepted by a pa_threaded_mainloop_accept(); otherwise, false
      • toString

        public String toString()
        Returns a human-readable String representation of this PulseAudioSystem. Always returns "PulseAudio".
        Overrides:
        toString in class DeviceSystem
        Returns:
        "PulseAudio" as a human-readable String representation of this PulseAudioSystem
      • unlockMainloop

        public void unlockMainloop()
        Unlocks the PulseAudio event look object associated with this PulseAudioSystem, inverse of lockMainloop().
      • waitForStreamState

        public int waitForStreamState​(long stream,
                                      int stateToWaitFor)
        Waits for a specific PulseAudio stream to get into a specific state, PA_STREAM_FAILED, or PA_STREAM_TERMINATED.
        Parameters:
        stream - the PulseAudio stream to wait for
        stateToWaitFor - the PulseAudio state of the specified stream to wait for
        Returns:
        the state of the specified stream which caused the method to return
      • waitMainloop

        public void waitMainloop()
        Wait for an event to be signalled by the PulseAudio event loop thread associated with this PulseAudioSystem.