Skip to content

Commit

Permalink
Add parser (#13)
Browse files Browse the repository at this point in the history
* add parser and dummy test

* add back self-delimiting framing

* add init

* move data to metadata instead of caps

* cleanup and add test

* remove now-unnecessary modules

* bump version

* remove unused fixture

* fix tests

* comments

* respond to comments

* fix tests and remove 'with' statement

* allow Parser to undelimit

* split out frame length parsing

* moving things around

* cleanup

* comments

* comments and cosmetics

* support opus streams in parser (wip)

* fancyness with behaviours

* incremental update

* format

* handler interrupted headers

* comments

* comments

* comments
  • Loading branch information
ledhed2222 committed Mar 18, 2021
1 parent 9a9b00e commit bfd5e2a
Show file tree
Hide file tree
Showing 21 changed files with 837 additions and 378 deletions.
3 changes: 1 addition & 2 deletions c_src/membrane_opus_plugin/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define MAX_PACKET 1500

char *get_error(int err_code) {
static char *get_error(int err_code) {
switch (err_code) {
case OPUS_BAD_ARG:
return "Bad argument";
Expand Down Expand Up @@ -48,7 +48,6 @@ UNIFEX_TERM encode_packet(UnifexEnv *env, State *state,
);

if (encoded_size_or_error < 0) {
MEMBRANE_WARN(env, "Opus: Encode error: %x\n", encoded_size_or_error);
return encode_packet_result_error(env, get_error(encoded_size_or_error));
}

Expand Down
2 changes: 0 additions & 2 deletions c_src/membrane_opus_plugin/encoder.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#define MEMBRANE_LOG_TAG "Membrane.Opus.EncoderNative"
#include <membrane/log.h>
#include <opus/opus.h>
#include <unifex/unifex.h>

Expand Down
5 changes: 3 additions & 2 deletions lib/membrane_opus/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Membrane.Opus.Decoder do
alias __MODULE__.Native
alias Membrane.{Buffer, Opus, RemoteStream}
alias Membrane.Caps.Audio.Raw
alias Membrane.Opus.PacketUtils
alias Membrane.Opus.Util

@avg_opus_packet_size 960

Expand Down Expand Up @@ -64,7 +64,8 @@ defmodule Membrane.Opus.Decoder do

@impl true
def handle_process(:input, buffer, _ctx, state) do
{:ok, %{channels: channels}, _data} = PacketUtils.skip_toc(buffer.payload)
{:ok, _config_number, stereo_flag, _frame_packing} = Util.parse_toc_byte(buffer.payload)
channels = Util.parse_channels(stereo_flag)
{caps, state} = maybe_make_native(channels, state)

decoded = Native.decode_packet(state.native, buffer.payload)
Expand Down
File renamed without changes.
61 changes: 23 additions & 38 deletions lib/membrane_opus/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ defmodule Membrane.Opus.Encoder do
alias Membrane.Opus

@list_type allowed_channels :: [1, 2]

@list_type allowed_applications :: [:voip, :audio, :low_delay]

@list_type allowed_sample_rates :: [8000, 12_000, 16_000, 24_000, 48_000]

@supported_input {Raw,
format: :s16le,
channels: Matcher.one_of(@allowed_channels),
sample_rate: Matcher.one_of(@allowed_sample_rates)}

def_options application: [
spec: allowed_applications(),
default: :audio,
description: """
Output type (similar to compression amount). See https://opus-codec.org/docs/opus_api-1.3.1/group__opus__encoder.html#gaa89264fd93c9da70362a0c9b96b9ca88.
Output type (similar to compression amount).
See https://opus-codec.org/docs/opus_api-1.3.1/group__opus__encoder.html#gaa89264fd93c9da70362a0c9b96b9ca88.
"""
],
input_caps: [
spec: Raw.t(),
type: :caps,
default: nil,
description: """
Input type - used to set input sample rate and channels
Input type - used to set input sample rate and channels.
"""
]

def_input_pad :input, demand_unit: :bytes, caps: @supported_input
def_input_pad :input,
demand_unit: :bytes,
caps:
{Raw,
format: :s16le,
channels: Matcher.one_of(@allowed_channels),
sample_rate: Matcher.one_of(@allowed_sample_rates)}

def_output_pad :output, caps: {Opus, self_delimiting?: false}

@impl true
Expand Down Expand Up @@ -91,10 +93,7 @@ defmodule Membrane.Opus.Encoder do

@impl true
def handle_process(:input, %Buffer{payload: data}, _ctx, state) do
# holds a buffer of raw input that is not yet encoded
to_encode = state.queue <> data

case encode_buffer(to_encode, state, frame_size_in_bytes(state)) do
case encode_buffer(state.queue <> data, state, frame_size_in_bytes(state)) do
{:ok, {[], rest}} ->
# nothing was encoded
{{:ok, redemand: :output}, %{state | queue: rest}}
Expand Down Expand Up @@ -140,37 +139,23 @@ defmodule Membrane.Opus.Encoder do
end
end

defp map_application_to_value(:voip) do
{:ok, 2048}
end

defp map_application_to_value(:audio) do
{:ok, 2049}
end

defp map_application_to_value(:low_delay) do
{:ok, 2051}
end

defp map_application_to_value(_) do
{:error, :invalid_application}
defp map_application_to_value(application) do
case application do
:voip -> {:ok, 2048}
:audio -> {:ok, 2049}
:low_delay -> {:ok, 2051}
_ -> {:error, "Invalid application"}
end
end

defp validate_sample_rate(sample_rate) when sample_rate in @allowed_sample_rates do
{:ok, sample_rate}
end

defp validate_sample_rate(_) do
{:error, :invalid_sample_rate}
end

defp validate_channels(channels) when channels in @allowed_channels do
{:ok, channels}
end
defp validate_sample_rate(_), do: {:error, "Invalid sample rate"}

defp validate_channels(_) do
{:error, :invalid_channels}
end
defp validate_channels(channels) when channels in @allowed_channels, do: {:ok, channels}
defp validate_channels(_), do: {:error, "Invalid channels"}

defp frame_size(state) do
# 20 milliseconds
Expand Down
File renamed without changes.
131 changes: 0 additions & 131 deletions lib/membrane_opus/packet_utils.ex

This file was deleted.

Loading

0 comments on commit bfd5e2a

Please sign in to comment.