Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation #1

Merged
merged 13 commits into from
Dec 9, 2024
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
# Membrane Template Plugin
# Membrane Transcider Plugin
varsill marked this conversation as resolved.
Show resolved Hide resolved

[![Hex.pm](https://img.shields.io/hexpm/v/membrane_template_plugin.svg)](https://hex.pm/packages/membrane_template_plugin)
[![API Docs](https://img.shields.io/badge/api-docs-yellow.svg?style=flat)](https://hexdocs.pm/membrane_template_plugin)
[![CircleCI](https://circleci.com/gh/membraneframework/membrane_template_plugin.svg?style=svg)](https://circleci.com/gh/membraneframework/membrane_template_plugin)

This repository contains a template for new plugins.

Check out different branches for other flavors of this template.
This repository provides `Membrane.Transcoder` which is a bin that is capable
of transcoding the input audio or video stream into the descired one specified
with simple declarative API.

It's a part of the [Membrane Framework](https://membrane.stream).

## Installation

The package can be installed by adding `membrane_template_plugin` to your list of dependencies in `mix.exs`:
The package can be installed by adding `membrane_transcoder_plugin` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:membrane_template_plugin, "~> 0.1.0"}
{:membrane_transcoder_plugin, "~> 0.1.0"}
]
end
```

## Usage

TODO

## Copyright and License

Copyright 2020, [Software Mansion](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_template_plugin)
Expand Down
7 changes: 6 additions & 1 deletion lib/transcoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ defmodule Membrane.Transcoder do
end

@impl true
def handle_child_notification({:stream_format, format}, :forwarding_filter, _ctx, state) do
def handle_child_notification(
{:stream_format, format},
:forwarding_filter,
_ctx,
%{input_stream_format: nil} = state
) do
varsill marked this conversation as resolved.
Show resolved Hide resolved
state =
%{state | input_stream_format: format}
|> resolve_output_stream_format()
Expand Down
30 changes: 28 additions & 2 deletions lib/transcoder/audio.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
defmodule Boombox.Transcoder.Audio do
defmodule Membrane.Transcoder.Audio do
@moduledoc false

import Membrane.ChildrenSpec
require Membrane.Logger
alias Membrane.{AAC, ChildrenSpec, Opus, RawAudio, RemoteStream}

@opus_sample_rate 48_000
@aac_sample_rates [
96_000,
88_200,
64_000,
48_000,
44_100,
32_000,
24_000,
22_050,
16_000,
12_000,
11_025,
8000
]

@type audio_stream_format :: AAC.t() | Opus.t() | RawAudio.t()

defguard is_audio_format(format)
when is_struct(format) and
(format.__struct__ in [AAC, Opus, RawAudio] or
(format.__struct__ == RemoteStream and format.content_format == Opus and
(format.__struct__ == RemoteStream and format.content_format in [Opus, AAC] and
varsill marked this conversation as resolved.
Show resolved Hide resolved
format.type == :packetized))

@spec plug_audio_transcoding(
Expand Down Expand Up @@ -72,6 +86,18 @@ defmodule Boombox.Transcoder.Audio do
})
end

defp maybe_plug_resampler(builder, %{sample_rate: sample_rate} = input_format, %AAC{})
when sample_rate not in @aac_sample_rates do
builder
|> child(:resampler, %Membrane.FFmpeg.SWResample.Converter{
output_stream_format: %RawAudio{
sample_format: :s16le,
sample_rate: 44_100,
channels: input_format.channels
}
})
end

defp maybe_plug_resampler(builder, _input_format, _output_format) do
builder
end
Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Membrane.Template.Mixfile do
defmodule Membrane.Transcoder.Plugin.Mixfile do
use Mix.Project

@version "0.1.0"
Expand Down Expand Up @@ -47,6 +47,8 @@ defmodule Membrane.Template.Mixfile do
{:membrane_h265_ffmpeg_plugin, "~> 0.4.2"},
{:membrane_ffmpeg_swresample_plugin, "~> 0.20.0"},
{:membrane_timestamp_queue, "~> 0.2.2"},
{:membrane_h264_format, "~> 0.6.1"},
{:membrane_opus_format, "~> 0.3.0"},
varsill marked this conversation as resolved.
Show resolved Hide resolved
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:dialyxir, ">= 0.0.0", only: :dev, runtime: false},
{:credo, ">= 0.0.0", only: :dev, runtime: false}
Expand Down