ExMemvid.Encoder (ex_memvid v0.1.1)

A Encoder implementation for encoding text chunks into QR code videos.

Each encoder instance is responsible for managing one video encoding process with proper state transitions and error handling.

States

  • :ready - Ready to accept chunks and configuration
  • :collecting - Collecting text chunks for encoding
  • :building - Building the video and index files
  • :completed - Successfully completed encoding
  • :error - Error state with failure information

State Data

The state machine maintains:

  • config: ExMemvid configuration
  • chunks: List of text chunks to encode
  • index_name: Unique identifier for this encoding process
  • output_path: Video output file path
  • index_path: Index output file path
  • stats: Encoding statistics (when completed)
  • error: Error information (when in error state)

Example Usage

# Start a new encoder process
{:ok, pid} = ExMemvid.EncoderStateMachine.start_link(
  index_name: "my_video_001",
  config: %ExMemvid.Config{codec: :h264}
)

# Add text chunks
:ok = ExMemvid.EncoderStateMachine.add_chunks(pid, ["chunk1", "chunk2"])

# Build the video
{:ok, stats} = ExMemvid.EncoderStateMachine.build_video(
  pid, 
  "output/video.mp4", 
  "output/index.bin"
)

Summary

Functions

Adds text chunks to the encoder.

Adds text and automatically chunks it using the configured chunking settings.

Builds the QR code video and search index. This is an asynchronous operation that transitions the state machine to building state.

Returns a specification to start this module under a supervisor.

Gets the current state and data of the encoder.

Resets the encoder to ready state, clearing all chunks and previous results.

Starts a new encoder

Types

state_data()

@type state_data() :: %{
  config: ExMemvid.Config.t(),
  chunks: [String.t()],
  index_name: String.t(),
  output_path: String.t() | nil,
  index_path: String.t() | nil,
  stats: map() | nil,
  error: term() | nil,
  client_from: pid() | nil
}

state_name()

@type state_name() :: :ready | :collecting | :building | :completed | :error

Functions

add_chunks(server, chunks)

@spec add_chunks(GenServer.server(), [String.t()]) :: :ok | {:error, term()}

Adds text chunks to the encoder.

add_text(server, text)

@spec add_text(GenServer.server(), String.t()) :: :ok | {:error, term()}

Adds text and automatically chunks it using the configured chunking settings.

build_video(server, output_path, index_path)

@spec build_video(GenServer.server(), Path.t(), Path.t()) ::
  {:ok, map()} | {:error, term()}

Builds the QR code video and search index. This is an asynchronous operation that transitions the state machine to building state.

building(arg1, arg2, data)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

collecting(arg1, arg2, data)

completed(arg, arg2, data)

error(arg, arg2, data)

get_state(server)

@spec get_state(GenServer.server()) :: {state_name(), map()}

Gets the current state and data of the encoder.

ready(arg1, arg2, data)

reset(server)

@spec reset(GenServer.server()) :: :ok

Resets the encoder to ready state, clearing all chunks and previous results.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a new encoder

Options

  • :index_name - Unique identifier for this encoder instance (required)
  • :config - ExMemvid configuration (required)
  • :name - Optional process name for registration