# `Membrane.HTTPAdaptiveStream.Storage`
[🔗](https://github.com/membraneframework/membrane_http_adaptive_stream_plugin/blob/v0.21.0/lib/membrane_http_adaptive_stream/storage.ex#L1)

Behaviour for storing manifests and stream segments.

# `callback_result_t`

```elixir
@type callback_result_t() :: :ok | {:error, reason :: any()}
```

# `config_t`

```elixir
@type config_t() :: struct()
```

# `header`

```elixir
@type header() :: String.t()
```

# `metadata_t`

```elixir
@type metadata_t() :: segment_metadata_t() | partial_segment_metadata() | %{}
```

# `parent_t`

```elixir
@type parent_t() :: any()
```

The identifier of a parent that the resource belongs to (the track identifier).

It can either be a master or secondary playlist (a track playlist).
In case of master playlist the identifier will be `:master`  while for tracks it can be an arbitrary value.

# `partial_segment_metadata`

```elixir
@type partial_segment_metadata() :: %{
  duration: Membrane.Time.t(),
  independent: boolean(),
  byte_offset: non_neg_integer()
}
```

# `segment_metadata_t`

```elixir
@type segment_metadata_t() :: %{duration: Membrane.Time.t()}
```

# `segments`

```elixir
@type segments() :: [String.t()]
```

# `state_t`

```elixir
@type state_t() :: any()
```

# `t`

```elixir
@opaque t()
```

# `init`

```elixir
@callback init(config_t()) :: state_t()
```

Generates the storage state based on the configuration struct.

# `remove`

```elixir
@callback remove(
  parent_id :: parent_t(),
  resource_name :: String.t(),
  context :: %{type: :manifest | :header | :segment},
  state_t()
) :: {callback_result_t(), state :: state_t()}
```

Removes the resource.

In case of removing a segment the storage should make sure to remove all
previous partial segments with the same name. It is the user's responsibility to remember
and distinguish between the partial segments.

# `store`

```elixir
@callback store(
  parent_id :: parent_t(),
  resource_name :: String.t(),
  content :: String.t() | binary(),
  metadata :: metadata_t(),
  context :: %{
    type: :manifest | :header | :segment | :partial_segment,
    mode: :text | :binary
  },
  state_t()
) :: {callback_result_t(), state :: state_t()}
```

Stores the resource on a storage.

Gets the mode that should be used when writing to a file and type of the resource

# `apply_track_changeset`

```elixir
@spec apply_track_changeset(
  t(),
  track_id :: term(),
  Membrane.HTTPAdaptiveStream.Manifest.Changeset.t()
) :: {callback_result_t(), t()}
```

Stores a new segment and removes stale ones.

# `clean_all_tracks`

```elixir
@spec clean_all_tracks(t(), %{required(id :: any()) =&gt; segments()}, %{
  required(id :: any()) =&gt; header()
}) ::
  {callback_result_t(), t()}
```

Removes all segments grouped by track.

# `cleanup`

```elixir
@spec cleanup(t(), id :: any(), segments :: segments(), header :: header() | nil) ::
  {callback_result_t(), t()}
```

Removes all the saved segments and manifest for given id.

# `clear_cache`

```elixir
@spec clear_cache(t()) :: t()
```

Clears the manifest cache.

# `new`

```elixir
@spec new(config_t(), [{:enable_cache, boolean()}]) :: t()
```

Initializes the storage.

Accepts the following options:
- `enable_cache` - if true (default), manifests will be stored only if they've been changed

# `store_header`

```elixir
@spec store_header(t(), track_id :: term(), name :: String.t(), payload :: binary()) ::
  {callback_result_t(), t()}
```

Stores stream header file.

# `store_manifests`

```elixir
@spec store_manifests(
  t(),
  Membrane.HTTPAdaptiveStream.Manifest.serialized_manifests_t()
) ::
  {callback_result_t(), t()}
```

Stores serialized manifest files

---

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