Skip to content

Commit

Permalink
Core v0.11 (#36)
Browse files Browse the repository at this point in the history
* Bump core to v0.11.0

* Update pads definition

* Update callbacks' names

* Update callbacks

* Update tests

* Update raw_audio_format

* Change caps to stream_format

* Fix callbacks results

* Apply reviewer suggestions
  • Loading branch information
DominikWolek authored Nov 22, 2022
1 parent 2377597 commit ad2f61c
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 135 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It is a part of [Membrane Multimedia Framework](https://membrane.stream).
Add the following line to your `deps` in `mix.exs`. Run `mix deps.get`.

```elixir
{:membrane_ffmpeg_swresample_plugin, "~> 0.15.0"}
{:membrane_ffmpeg_swresample_plugin, "~> 0.16.0"}
```

You also need to have [FFmpeg](https://www.ffmpeg.org/) library installed.
Expand Down
118 changes: 64 additions & 54 deletions lib/membrane_ffmpeg_swresample_plugin/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,56 @@ defmodule Membrane.FFmpeg.SWResample.Converter do

require Membrane.Logger

alias Membrane.{Buffer, RawAudio, RemoteStream}
alias Membrane.Caps.Matcher
alias __MODULE__.Native
alias Membrane.{Buffer, RawAudio, RemoteStream}

@supported_out_caps {RawAudio,
sample_format: Matcher.one_of([:u8, :s16le, :s32le, :f32le, :f64le]),
channels: Matcher.one_of([1, 2])}
@supported_sample_format [:u8, :s16le, :s32le, :f32le, :f64le]
@supported_channels [1, 2]

def_output_pad :output, demand_mode: :auto, caps: @supported_out_caps
def_output_pad :output,
demand_mode: :auto,
accepted_format:
%RawAudio{sample_format: format, channels: channels}
when format in @supported_sample_format and channels in @supported_channels

def_input_pad :input,
demand_mode: :auto,
demand_unit: :bytes,
caps: [
@supported_out_caps,
{RemoteStream, content_format: one_of([nil, RawAudio])},
{RawAudio, sample_format: :s24le, channels: one_of([1, 2])}
]

def_options input_caps: [
type: :caps,
accepted_format:
any_of(
%RawAudio{sample_format: format, channels: channels}
when format in @supported_sample_format and channels in @supported_channels,
%RemoteStream{content_format: format} when format in [nil, RawAudio],
%RawAudio{sample_format: :s24le, channels: channels} when channels in @supported_channels
)

def_options input_stream_format: [
spec: RawAudio.t() | nil,
default: nil,
description: """
Caps for the input pad. If set to nil (default value),
caps are assumed to be received through the pad. If explicitly set to some
caps, they cannot be changed by caps received through the pad.
Stream format for the input pad. If set to nil (default value),
stream format is assumed to be received through the pad. If explicitly set to some
stream format, it cannot be changed by stream format received through the pad.
"""
],
output_caps: [
type: :caps,
output_stream_format: [
spec: RawAudio.t(),
description: """
Audio caps for source pad (output)
Audio stream format for output pad
"""
]

@impl true
def handle_init(%__MODULE__{} = options) do
case options.input_caps do
def handle_init(_ctx, %__MODULE__{} = options) do
case options.input_stream_format do
%RawAudio{} -> :ok
nil -> :ok
_other -> raise ":input_caps must be nil or %RawAudio{}"
_other -> raise ":input_stream_format must be nil or %RawAudio{}"
end

case options.output_caps do
case options.output_stream_format do
%RawAudio{} -> :ok
_other -> raise ":output_caps must be %RawAudio{}"
_other -> raise ":output_stream_format must be %RawAudio{}"
end

state =
Expand All @@ -65,61 +67,74 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
|> Map.merge(%{
native: nil,
queue: <<>>,
input_caps_provided?: options.input_caps != nil
input_stream_format_provided?: options.input_stream_format != nil
})

{:ok, state}
{[], state}
end

@impl true
def handle_stopped_to_prepared(_ctx, %{input_caps_provided?: false} = state), do: {:ok, state}
def handle_setup(_ctx, %{input_stream_format_provided?: false} = state), do: {[], state}

def handle_stopped_to_prepared(_ctx, state) do
native = mk_native!(state.input_caps, state.output_caps)
{{:ok, caps: {:output, state.output_caps}}, %{state | native: native}}
def handle_setup(_ctx, state) do
native = mk_native!(state.input_stream_format, state.output_stream_format)
{[], %{state | native: native}}
end

@impl true
def handle_caps(:input, %RemoteStream{}, _ctx, %{input_caps_provided?: false}) do
def handle_stream_format(:input, %RemoteStream{}, _ctx, %{input_stream_format_provided?: false}) do
raise """
Cannot handle RemoteStream without explicitly providing `input_caps` via element options
Cannot handle RemoteStream without explicitly providing `input_stream_format` via element options
"""
end

@impl true
def handle_caps(:input, %RemoteStream{}, _ctx, %{input_caps: stored_caps} = state) do
native = mk_native!(stored_caps, state.output_caps)
def handle_stream_format(
:input,
%RemoteStream{},
_ctx,
%{input_stream_format: stored_stream_format} = state
) do
native = mk_native!(stored_stream_format, state.output_stream_format)
state = %{state | native: native}
{{:ok, caps: {:output, state.output_caps}}, state}
{[stream_format: {:output, state.output_stream_format}], state}
end

@impl true
def handle_caps(:input, caps, _ctx, %{input_caps_provided?: true, input_caps: stored_caps})
when stored_caps != caps do
def handle_stream_format(:input, stream_format, _ctx, %{
input_stream_format_provided?: true,
input_stream_format: stored_stream_format
})
when stored_stream_format != stream_format do
raise """
Received caps #{inspect(caps)} are different then defined in options #{inspect(stored_caps)}.
If you want to allow converter to accept different input caps dynamically, use `nil` as input_caps.
Received stream_format #{inspect(stream_format)} are different then the one defined in options #{inspect(stored_stream_format)}.
If you want to allow converter to accept different input stream formats dynamically, use `nil` as input_stream_format.
"""
end

@impl true
def handle_caps(:input, %RawAudio{} = caps, _ctx, state) do
native = mk_native!(caps, state.output_caps)
state = %{state | native: native, input_caps: caps}
{{:ok, caps: {:output, state.output_caps}}, state}
def handle_stream_format(:input, %RawAudio{} = stream_format, _ctx, state) do
native = mk_native!(stream_format, state.output_stream_format)
state = %{state | native: native, input_stream_format: stream_format}
{[stream_format: {:output, state.output_stream_format}], state}
end

@impl true
def handle_playing(_ctx, state) do
{[stream_format: {:output, state.output_stream_format}], state}
end

@impl true
def handle_process(:input, %Buffer{payload: payload}, _ctx, state) do
conversion_result =
convert!(state.native, RawAudio.frame_size(state.input_caps), payload, state.queue)
convert!(state.native, RawAudio.frame_size(state.input_stream_format), payload, state.queue)

case conversion_result do
{<<>>, queue} ->
{:ok, %{state | queue: queue}}
{[], %{state | queue: queue}}

{converted, queue} ->
{{:ok, buffer: {:output, %Buffer{payload: converted}}}, %{state | queue: queue}}
{[buffer: {:output, %Buffer{payload: converted}}], %{state | queue: queue}}
end
end

Expand All @@ -135,19 +150,14 @@ defmodule Membrane.FFmpeg.SWResample.Converter do

case flush!(state.native) do
<<>> ->
{{:ok, end_of_stream: :output}, %{state | queue: <<>>}}
{[end_of_stream: :output], %{state | queue: <<>>}}

converted ->
{{:ok, buffer: {:output, %Buffer{payload: converted}}, end_of_stream: :output},
{[buffer: {:output, %Buffer{payload: converted}}, end_of_stream: :output],
%{state | queue: <<>>}}
end
end

@impl true
def handle_prepared_to_stopped(_ctx, state) do
{:ok, %{state | native: nil}}
end

defp mk_native!(
%RawAudio{
sample_format: in_sample_format,
Expand Down
12 changes: 6 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Membrane.FFmpeg.SWResample.Mixfile do
use Mix.Project

@github_url "https://github.com/membraneframework/membrane_ffmpeg_swresample_plugin"
@version "0.15.0"
@version "0.16.0"

def project do
[
Expand Down Expand Up @@ -40,15 +40,15 @@ defmodule Membrane.FFmpeg.SWResample.Mixfile do

defp deps do
[
{:membrane_core, "~> 0.10.0"},
{:membrane_raw_audio_format, "~> 0.9.0"},
{:bunch, "~> 1.3.0"},
{:membrane_core, "~> 0.11.0"},
{:membrane_raw_audio_format, "~> 0.10.0"},
{:bunch, "~> 1.5.0"},
{:unifex, "~> 1.0"},
{:membrane_common_c, "~> 0.13.0"},
{:membrane_common_c, "~> 0.14.0"},
{:bundlex, "~> 1.0"},
# Testing
{:mockery, "~> 2.1", runtime: false},
{:membrane_file_plugin, "~> 0.9", only: :test},
{:membrane_file_plugin, "~> 0.13.0"},
# Development
{:ex_doc, "~> 0.28", only: :dev, runtime: false},
{:dialyxir, "~> 1.1", only: :dev, runtime: false},
Expand Down
14 changes: 7 additions & 7 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%{
"bimap": {:hex, :bimap, "1.2.1", "cbfd21894abc15b82018df906594fbc40aca8a2d5e6511473acc6ded12f6d7e2", [:mix], [], "hexpm", "635488554b5db7554aa3379bc219c5d75575a1126a1e94ea0818a20c626fd03e"},
"bunch": {:hex, :bunch, "1.3.1", "f8fe80042f9eb474ef2801ae2c9372f9b13d11e7053265dcfc24b9d912e3750b", [:mix], [], "hexpm", "00e21b16ff9bb698b728a01a2fc4b3bf7fc0e87c4bb9c6e4a442324aa8c5e567"},
"bimap": {:hex, :bimap, "1.3.0", "3ea4832e58dc83a9b5b407c6731e7bae87458aa618e6d11d8e12114a17afa4b3", [:mix], [], "hexpm", "bf5a2b078528465aa705f405a5c638becd63e41d280ada41e0f77e6d255a10b4"},
"bunch": {:hex, :bunch, "1.5.0", "78530e85bc3f53e1711dba654565692e2015cb6d1685e9b53bf7606b14a36c71", [:mix], [], "hexpm", "2c32f8da5d4c9e7a534c8c25aea150da696dd8624ce64f97c21ee193c12258e5"},
"bunch_native": {:hex, :bunch_native, "0.5.0", "8ac1536789a597599c10b652e0b526d8833348c19e4739a0759a2bedfd924e63", [:mix], [{:bundlex, "~> 1.0", [hex: :bundlex, repo: "hexpm", optional: false]}], "hexpm", "24190c760e32b23b36edeb2dc4852515c7c5b3b8675b1a864e0715bdd1c8f80d"},
"bundlex": {:hex, :bundlex, "1.0.0", "358c26a6c027359c6935dcd0716b68736b7604599d376c4e1ac17c6618ef6771", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}], "hexpm", "09b4a0c597a31e6e7ce8e103b94f19539bb2279f5966a3d6d46dcd32a5b6fd44"},
"bundlex": {:hex, :bundlex, "1.0.1", "fd9e68b3636b8b5b678050f1f340e833e6d3890e110766398f38ec9c41da0b44", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}], "hexpm", "c6acfd675a4780c9b66c32c0e3c37b96b5314369857c5b7d3ffa198fa1e0085e"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
"credo": {:hex, :credo, "1.6.6", "f51f8d45db1af3b2e2f7bee3e6d3c871737bda4a91bff00c5eec276517d1a19c", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "625520ce0984ee0f9f1f198165cd46fa73c1e59a17ebc520038b8fce056a5bdc"},
Expand All @@ -15,10 +15,10 @@
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"membrane_common_c": {:hex, :membrane_common_c, "0.13.0", "c314623f93209eb2fa092379954c686f6e50ac89baa48360f836d24f4d53f5ee", [:mix], [{:membrane_core, "~> 0.10.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:shmex, "~> 0.5.0", [hex: :shmex, repo: "hexpm", optional: false]}, {:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "90181fbbe481ccd0a4a76daf0300f8ad1b5b0bf0ebd8b42c133904f8839663ca"},
"membrane_core": {:hex, :membrane_core, "0.10.2", "d2d17039f6df746e4a3c47da32f51867fbafe528272cdd9b226d16b1032bc337", [:mix], [{:bunch, "~> 1.3", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.3", [hex: :qex, repo: "hexpm", optional: false]}, {:ratio, "~> 2.0", [hex: :ratio, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6a4f290f919ada66c772807d64d5830be2962b7c13a2f2bc9ace416a1cd19ee1"},
"membrane_file_plugin": {:hex, :membrane_file_plugin, "0.12.0", "eb940e7a2f2abf30e048bd0b7c2bef9c17c18aa58875b9a833c0bc7e7b1fd709", [:mix], [{:membrane_core, "~> 0.10.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "281b9bf9467beead3f973adce55b9844bc4206bb3f3f60f0db8320a4af4fc5ca"},
"membrane_raw_audio_format": {:hex, :membrane_raw_audio_format, "0.9.0", "c404a6eb38600dd85ad69dcf974b3c82fe0ef07c92e602cd438763dcdaf3462d", [:mix], [{:bimap, "~> 1.1", [hex: :bimap, repo: "hexpm", optional: false]}, {:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 0.10.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "7b346bd3be6bcc7ceb9fe28ca6a9dfd8b072e9f6960fe5766ea99582319c05df"},
"membrane_common_c": {:hex, :membrane_common_c, "0.14.0", "35621d9736829bf675062dc0af66e931b0d82ff8361c2088576d63d4d002692e", [:mix], [{:membrane_core, "~> 0.11.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:shmex, "~> 0.5.0", [hex: :shmex, repo: "hexpm", optional: false]}, {:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "262b06e93fe3f1be57a111fa19f5cba4f26000a442ac32a59f3678e83cbb001f"},
"membrane_core": {:hex, :membrane_core, "0.11.0", "63ae9f56834ec67680d634d8d69f71b2d46b94f4a0ec8fafcf22d8ce216b8f41", [:mix], [{:bunch, "~> 1.5", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.3", [hex: :qex, repo: "hexpm", optional: false]}, {:ratio, "~> 2.0", [hex: :ratio, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "097584018fe948fa3013bfd6bcf002b3ad7cbd13f2259be4f1903f37a7aad7ab"},
"membrane_file_plugin": {:hex, :membrane_file_plugin, "0.13.0", "5d82476706410d372718208b48ace8bbf131f4466bca8f88ca015ebc4dbf5bad", [:mix], [{:membrane_core, "~> 0.11", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "efa63530c1a9cf7e8da6ca5b3af9598573a007c89fc7bedf02ffe4ffc1753e66"},
"membrane_raw_audio_format": {:hex, :membrane_raw_audio_format, "0.10.0", "4ff07002d95b8e9259c38991ead8d98e907ee66346545c168b8a8ad6ae73d87e", [:mix], [{:bimap, "~> 1.1", [hex: :bimap, repo: "hexpm", optional: false]}, {:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 0.11", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "d5b4201d0e634e9fdbc2b9528f1fee245eb667c82ace91c745af6604401003ae"},
"mockery": {:hex, :mockery, "2.3.1", "a02fd60b10ac9ed37a7a2ecf6786c1f1dd5c75d2b079a60594b089fba32dc087", [:mix], [], "hexpm", "1d0971d88ebf084e962da3f2cfee16f0ea8e04ff73a7710428500d4500b947fa"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"numbers": {:hex, :numbers, "5.2.4", "f123d5bb7f6acc366f8f445e10a32bd403c8469bdbce8ce049e1f0972b607080", [:mix], [{:coerce, "~> 1.0", [hex: :coerce, repo: "hexpm", optional: false]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "eeccf5c61d5f4922198395bf87a465b6f980b8b862dd22d28198c5e6fab38582"},
Expand Down
Loading

0 comments on commit ad2f61c

Please sign in to comment.