Skip to content

Commit

Permalink
Add tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sgfn committed Aug 8, 2024
1 parent 9b1c327 commit b31ffad
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
14 changes: 9 additions & 5 deletions lib/ex_webrtc/rtp/depayloader.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExWebRTC.RTP.Depayloader do
@moduledoc """
Behaviour for ExWebRTC Depayloaders.
Dispatcher module and behaviour for ExWebRTC Depayloaders.
"""

alias ExWebRTC.RTPCodecParameters
Expand All @@ -9,8 +9,6 @@ defmodule ExWebRTC.RTP.Depayloader do

@doc """
Creates a new depayloader struct.
Refer to the modules implementing the behaviour for available options.
"""
@callback new(options :: any()) :: depayloader()

Expand All @@ -24,7 +22,9 @@ defmodule ExWebRTC.RTP.Depayloader do
{binary() | nil, depayloader()}

@doc """
TODO WRITEME
Creates a new depayloader struct that matches the passed codec parameters.
Refer to the modules implementing the behaviour for available options.
"""
@spec new(RTPCodecParameters.t(), any()) ::
{:ok, depayloader()} | {:error, :no_depayloader_for_codec}
Expand All @@ -37,7 +37,11 @@ defmodule ExWebRTC.RTP.Depayloader do
end

@doc """
TODO WRITEME
Processes binary data from a single RTP packet using the depayloader's module,
and outputs a frame if assembled.
Returns the frame (or `nil` if a frame could not be depayloaded yet)
together with the updated depayloader struct.
"""
@spec depayload(depayloader(), ExRTP.Packet.t()) :: {binary() | nil, depayloader()}
def depayload(%module{} = depayloader, packet) do
Expand Down
12 changes: 7 additions & 5 deletions lib/ex_webrtc/rtp/payloader.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExWebRTC.RTP.Payloader do
@moduledoc """
Behaviour for ExWebRTC Payloaders.
Dispatcher module and behaviour for ExWebRTC Payloaders.
"""

alias ExWebRTC.RTPCodecParameters
Expand All @@ -9,8 +9,6 @@ defmodule ExWebRTC.RTP.Payloader do

@doc """
Creates a new payloader struct.
Refer to the modules implementing the behaviour for available options.
"""
@callback new(options :: any()) :: payloader()

Expand All @@ -22,7 +20,9 @@ defmodule ExWebRTC.RTP.Payloader do
@callback payload(payloader(), frame :: binary()) :: {[ExRTP.Packet.t()], payloader()}

@doc """
TODO WRITEME
Creates a new payloader struct that matches the passed codec parameters.
Refer to the modules implementing the behaviour for available options.
"""
@spec new(RTPCodecParameters.t(), any()) ::
{:ok, payloader()} | {:error, :no_payloader_for_codec}
Expand All @@ -35,7 +35,9 @@ defmodule ExWebRTC.RTP.Payloader do
end

@doc """
TODO WRITEME
Packs a frame into one or more RTP packets using the payloader's module.
Returns the packets together with the updated payloader struct.
"""
@spec payload(payloader(), binary()) :: {[ExRTP.Packet.t()], payloader()}
def payload(%module{} = payloader, frame) do
Expand Down
44 changes: 44 additions & 0 deletions test/ex_webrtc/rtp/depayloader_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule ExWebRTC.RTP.DepayloaderTest do
use ExUnit.Case, async: true

alias ExWebRTC.RTPCodecParameters
alias ExWebRTC.RTP.Depayloader
alias ExWebRTC.RTP.{Opus, VP8}

@packet %ExRTP.Packet{
payload_type: 96,
sequence_number: 0,
timestamp: 0,
ssrc: 0,
payload: <<0, 1, 2, 3>>
}

test "creates a VP8 depayloader and dispatches calls to its module" do
assert {:ok, depayloader} =
%RTPCodecParameters{payload_type: 96, mime_type: "video/VP8", clock_rate: 90_000}
|> Depayloader.new()

assert Depayloader.depayload(depayloader, @packet) ==
VP8.Depayloader.depayload(depayloader, @packet)
end

test "creates an Opus depayloader and dispatches calls to its module" do
assert {:ok, depayloader} =
%RTPCodecParameters{
payload_type: 96,
mime_type: "audio/opus",
clock_rate: 48_000,
channels: 2
}
|> Depayloader.new()

assert Depayloader.depayload(depayloader, @packet) ==
Opus.Depayloader.depayload(depayloader, @packet)
end

test "returns error if no depayloader exists for given codec" do
assert {:error, :no_depayloader_for_codec} =
%RTPCodecParameters{payload_type: 97, mime_type: "video/H264", clock_rate: 90_000}
|> Depayloader.new()
end
end
41 changes: 41 additions & 0 deletions test/ex_webrtc/rtp/payloader_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule ExWebRTC.RTP.PayloaderTest do
use ExUnit.Case, async: true

alias ExWebRTC.RTPCodecParameters
alias ExWebRTC.RTP.Payloader
alias ExWebRTC.RTP.{Opus, VP8}

@frame <<0, 1, 2, 3>>

test "creates a VP8 payloader and dispatches calls to its module" do
assert {:ok, _payloader} =
%RTPCodecParameters{payload_type: 96, mime_type: "video/VP8", clock_rate: 90_000}
|> Payloader.new()

# with options
assert {:ok, payloader} =
%RTPCodecParameters{payload_type: 96, mime_type: "video/VP8", clock_rate: 90_000}
|> Payloader.new(800)

assert Payloader.payload(payloader, @frame) == VP8.Payloader.payload(payloader, @frame)
end

test "creates an Opus payloader and dispatches calls to its module" do
assert {:ok, payloader} =
%RTPCodecParameters{
payload_type: 111,
mime_type: "audio/opus",
clock_rate: 48_000,
channels: 2
}
|> Payloader.new()

assert Payloader.payload(payloader, @frame) == Opus.Payloader.payload(payloader, @frame)
end

test "returns error if no payloader exists for given codec" do
assert {:error, :no_payloader_for_codec} =
%RTPCodecParameters{payload_type: 97, mime_type: "video/H264", clock_rate: 90_000}
|> Payloader.new()
end
end

0 comments on commit b31ffad

Please sign in to comment.