Class ProducerModule

java.lang.Object
com.vivoka.vsdk.audio.ProducerModule
Direct Known Subclasses:
AudioRecorder, BufferForwarder, BufferModule, BufferModuleOut, BufferStreamer, Channel, File

public abstract class ProducerModule extends Object
Abstract class representing a producer module in the audio processing pipeline. This class provides methods for managing subscribers, handling errors, and dispatching audio buffers. It defines the lifecycle of a producer module including opening, running, starting, pausing, resuming, stopping, and closing operations.
  • Constructor Details

    • ProducerModule

      public ProducerModule()
      Constructor for the ProducerModule class. Initializes the producer module with an empty list of subscribers.
  • Method Details

    • subscribeFirst

      public Iterator<IAudioCallback> subscribeFirst(IAudioCallback audioCallback) throws Exception
      Subscribes an audio callback to the producer module. The callback will be notified when audio data is available.
      Parameters:
      audioCallback - The audio callback to subscribe.
      Returns:
      An iterator representing the position of the subscriber.
      Throws:
      Exception - If the audioCallback is null.
    • subscribeLast

      public Iterator<IAudioCallback> subscribeLast(IAudioCallback audioCallback) throws Exception
      Subscribes an audio callback to the end of the subscriber list. The callback will be notified when audio data is available.
      Parameters:
      audioCallback - The audio callback to subscribe.
      Returns:
      An iterator representing the position of the subscriber.
      Throws:
      Exception - If the audioCallback is null.
    • unsubscribe

      public void unsubscribe(int position) throws Exception
      Unsubscribes an audio callback from the producer module. The callback will no longer be notified when audio data is available.
      Parameters:
      position - The position of the subscriber to unsubscribe.
      Throws:
      Exception - If the position is negative or out of bounds.
    • getLastError

      public String getLastError()
      Gets the last error message associated with the producer module. This can be used to retrieve error information after an operation fails.
      Returns:
      The last error message, or null if no error has occurred.
    • setLastError

      protected void setLastError(String error) throws Exception
      Throws:
      Exception
    • getState

      public ProducerModule.State getState()
      Gets the current state of the producer module. This can be used to check if the module is opened, running, started, paused, idle, or closed.
      Returns:
      The current state of the producer module.
    • setState

      protected void setState(ProducerModule.State state)
    • setStateChanedCallback

      public void setStateChanedCallback(androidx.core.util.Consumer<ProducerModule.State> onStateChanged)
    • isOpened

      public boolean isOpened()
      Checks if the producer module is currently opened. This indicates that the module is ready to start processing audio data.
      Returns:
      True if the producer module is opened, false otherwise.
    • isRunning

      public boolean isRunning()
      Checks if the producer module is currently running. This indicates that the module is actively processing audio data in synchronous mode.
      Returns:
      True if the producer module is running, false otherwise.
    • isStarted

      public boolean isStarted()
      Checks if the producer module is currently started. This indicates that the module is actively processing audio data in asynchronous mode.
      Returns:
      True if the producer module is started, false otherwise.
    • isPaused

      public boolean isPaused()
      Checks if the producer module is currently paused. This indicates that the module has been paused and can be resumed later.
      Returns:
      True if the producer module is paused, false otherwise.
    • isIdle

      public boolean isIdle()
      Checks if the producer module is currently idle. This indicates that the module has completed its work and the stop() was not called.
      Returns:
      True if the producer module is idle, false otherwise.
    • isClosed

      public boolean isClosed()
      Checks if the producer module is currently closed and ready to start() or run() a new pipeline operation.
      Returns:
      True if the producer module is closed, false otherwise.
    • isBusy

      public boolean isBusy()
      Checks if the producer module is currently busy. This indicates that the module is either running or started.
      Returns:
      True if the producer module is busy, false otherwise.
    • openImpl

      protected void openImpl() throws Exception
      Opens the producer module. This method prepares the module for processing audio data. This method is called by the pipeline before starting or running the producer.
      Throws:
      Exception - If the producer is already opened or an error occurs during opening.
    • runImpl

      protected void runImpl() throws Exception
      Runs the producer module in a blocking manner. The producer state must be OPENED. When calling run pipeline, this method will be called after the open method. The state remains RUNNING until the end and then becomes OPENED.
      Throws:
      Exception - If the producer is not opened or an error occurs during running.
    • startImpl

      protected void startImpl() throws Exception
      Starts the producer module in a non-blocking manner. The producer state must be OPENED. When calling start pipeline, this method will be called after the open method. startImpl() should initiate the producer and return immediately without waiting for processing to complete and update the state to STARTED when it is done.
      Throws:
      Exception - If an error occurs during starting.
    • pauseImpl

      protected void pauseImpl() throws Exception
      Pauses the producer module. The producer state must be STARTED. When calling pause pipeline, this method will be called. It should initiate the pausing process and return immediately without waiting for it to complete. It should make sure that the state is updated to PAUSED when it is done.
      Throws:
      Exception - If an error occurs during pausing.
    • resumeImpl

      protected void resumeImpl() throws Exception
      Resumes the producer module. The producer state must be PAUSED. When the producer is resumed, the child class needs to update the state to STARTED. When calling resume pipeline, this method will be called. It should initiate the resuming process and return immediately without waiting for it to complete. It should make sure that the state is updated to OPENED when it is done.
      Throws:
      Exception - If an error occurs during resuming.
    • stopImpl

      protected void stopImpl() throws Exception
      Stops the producer module. The producer state must be STARTED. When calling stop pipeline, this method will be called. It should initiate the stopping process and return immediately without waiting for it to complete. It should make sure that the state is updated to OPENED when it is done.
      Throws:
      Exception - If an error occurs during stopping.
    • closeImpl

      protected void closeImpl() throws Exception
      Closes the producer module. The producer state must be OPENED or IDLE. When calling stop pipeline, this method will be called after the closeImpl method. When calling run pipeline, this method will be called after the runImpl method. If the closeImpl is asynchronous, the closeImpl should wait until the producer is OPENED.
      Throws:
      Exception - If an error occurs during closing.
    • dispatchAudio

      protected void dispatchAudio(int channelCount, int sampleRate, byte[] audioData, boolean last) throws Exception
      Dispatches audio data to the subscribers.
      Parameters:
      channelCount - The number of channels.
      sampleRate - The sample rate.
      audioData - The audio data.
      last - Whether this is the last audio data.
      Throws:
      Exception - If an error occurs during dispatching.
    • dispatchBuffer

      protected void dispatchBuffer(@NonNull Buffer buffer, boolean last) throws Exception
      Dispatches audio data to the subscribers.
      Parameters:
      buffer - The audio buffer.
      last - Whether this is the last audio data.
      Throws:
      Exception - If an error occurs during dispatching.