# `Membrane.Testing.Source`
[🔗](https://github.com/membraneframework/membrane-core/blob/v1.3.0/lib/membrane/testing/source.ex#L1)

Testing Element for supplying data based on generator function or payloads passed
through options.

## Example usage

As mentioned earlier you can use this element in one of two ways, providing
either a generator function or an `Enumerable.t`.

If you provide an `Enumerable.t` with payloads, then each of those payloads will
be wrapped in a `Membrane.Buffer` and sent through `:output` pad.
```
%Source{output: [0xA1, 0xB2, 0xC3, 0xD4]}
```

In order to specify `Membrane.Testing.Source` with generator function you need
to provide initial state and function that matches `t:generator/0` type. This
function should take state and demand size as its arguments and return
a tuple consisting of actions that element will return during the
`c:Membrane.Element.WithOutputPads.handle_demand/5`
callback and new state.
```
generator_function = fn state, size ->
  #generate some buffers
  {actions, state + 1}
end
%Source{output: {1, generator_function}}
```

## Element options

Passed via struct `t:Membrane.Testing.Source.t/0`

- `output`  

  ```
  Enum.t() | {initial_state :: any(), generator}
  ```
  
  Default value: `{0, &Membrane.Testing.Source.default_buf_gen/2}`  
  If `output` is an Enumerable, then each element of it will be sent
  through the `:output` pad, followed by `t:Membrane.Element.Action.end_of_stream/0`.
  Each element of the Enumerable must be either `t:Membrane.Buffer.t/0`
  or `t:Membrane.Payload.t/0`. In the latter case, it will be automatically wrapped into
  `t:Membrane.Buffer.t/0` before sending.
  
  Otherwise, if `output` is a `{initial_state, function}` tuple then the
  the function will be invoked each time `handle_demand` is called.
  It is an action generator that takes two arguments.
  The first argument is the state that is initially set to
  `initial_state`. The second one defines the size of the demand.
  Such function should return `{actions, next_state}` where
  `actions` is a list of actions that will be returned from
  `handle_demand/4` and `next_state` is the value that will be
  used for the next call.

- `stream_format`  

  ```
  struct()
  ```
  
  Default value: `%Membrane.RemoteStream{content_format: nil, type: :bytestream}`  
  StreamFormat to be sent before the `output`.

## Pads

### `:output`

Accepted formats:
```
_any
```

Direction: | `:output`
Availability: | `:always`
Flow control: | `:manual`
Demand unit: | `nil`

# `generator`

```elixir
@type generator() :: (state :: any(), buffers_cnt :: pos_integer() -&gt;
                  {[Membrane.Element.Action.t()], state :: any()})
```

# `t`

```elixir
@type t() :: %Membrane.Testing.Source{
  output: Enum.t() | {initial_state :: any(), generator()},
  stream_format: struct()
}
```

Struct containing options for `Membrane.Testing.Source`

# `default_buf_gen`

```elixir
@spec default_buf_gen(integer(), integer()) ::
  {[Membrane.Element.Action.t()], integer()}
```

# `options`

```elixir
@spec options() :: keyword()
```

Returns description of options available for this module

# `output_from_buffers`

```elixir
@spec output_from_buffers([Membrane.Buffer.t()]) ::
  {[Membrane.Buffer.t()], generator()}
```

Creates output with generator function from list of buffers.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
