Class RecordingSession

java.lang.Object
net.emustudio.emulib.recording.RecordingSession
All Implemented Interfaces:
AutoCloseable

@ThreadSafe public final class RecordingSession extends Object implements AutoCloseable
Thread-safe, one-time MP4 recording of video (H.264) and audio (PCM).

Video frames and audio arrive independently. Inputs are copied before returning, so producers may reuse their buffers. Video is encoded on a dedicated worker into a temporary file; audio is stored compressed in another temporary file and muxed in on stop(Path). Capture never waits for the worker: exceeding the bounded queue fails the session instead of slowing emulation. Check getFailure() when capture returns false.

Audio is signed 16-bit little-endian stereo PCM at the configured sample rate. Producers own frame pacing, synthesis and mixing.

stop(Path) and close() wait for the worker and file I/O. Call them off the UI thread.

  • Constructor Details

    • RecordingSession

      public RecordingSession(int width, int height, int videoScale, int videoRate, int audioSampleRate) throws IOException
      Starts a recording. Dimensions must be positive and even for H.264.
      Parameters:
      width - video width in pixels
      height - video height in pixels
      videoScale - frame-rate denominator
      videoRate - frame-rate numerator; fps = videoRate / videoScale
      audioSampleRate - stereo PCM frames per second
      Throws:
      IOException - if temporary recording files cannot be created
      IllegalArgumentException - if dimensions, frame rate or sample rate are invalid
  • Method Details

    • captureVideo

      public boolean captureVideo(BufferedImage frame)
      Captures one video frame.
      Parameters:
      frame - video frame of the recording size; null frames are ignored
      Returns:
      false if the frame was ignored, or the session is stopped or failed
    • captureAudio

      public boolean captureAudio(byte[] pcmSamples, int length)
      Captures the first length bytes of PCM audio. Empty chunks are ignored.
      Parameters:
      pcmSamples - PCM buffer, which may be reused after this call
      length - number of bytes, containing only whole stereo PCM frames
      Returns:
      false if the session is stopped or failed
      Throws:
      NullPointerException - if an active producer passes a null buffer
      IndexOutOfBoundsException - if length is outside the buffer
    • getFailure

      public IOException getFailure()
      Returns:
      the capture failure, or null while healthy. Safe to poll from producer/UI threads.
    • stop

      public void stop(Path target) throws IOException
      Stops accepting capture and saves MP4 to target, or discards the recording when target is null. Saving drains accepted captures; discarding drops pending captures. Repeated calls do nothing. Temporary files are always deleted; an existing target is kept if export fails.
      Parameters:
      target - destination MP4 (parent directories are created), or null to discard
      Throws:
      IOException - if capture or export failed, the recording has no frames, or stopping was interrupted
    • close

      public void close() throws IOException
      Discards this recording and releases its files. Blocks until the worker stops.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException