Skip to content

Commit

Permalink
Reorder result tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
sgfn committed Aug 23, 2024
1 parent 8173dc1 commit c0ef08d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/save_to_file/lib/save_to_file/peer_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule SaveToFile.PeerHandler do

defp handle_webrtc_msg(_msg, state), do: {:ok, state}

defp handle_jitter_buffer_result({buffer, packets, timer}, kind, state) do
defp handle_jitter_buffer_result({packets, timer, buffer}, kind, state) do
state =
case kind do
:video -> %{state | video_buffer: buffer}
Expand Down
12 changes: 6 additions & 6 deletions lib/ex_webrtc/rtp/jitter_buffer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ defmodule ExWebRTC.RTP.JitterBuffer do
@typedoc """
The 3-element tuple returned by all functions other than `new/1`.
* `buffer` - `t:#{inspect(__MODULE__)}.t/0`.
* `packets` - a list with packets flushed from the buffer as a result of the function call. May be empty.
* `timer_duration_ms` - see `t:timer/0`.
* `buffer` - `t:#{inspect(__MODULE__)}.t/0`.
Generally speaking, all results of this type can be handled in the same way.
"""
@type result :: {buffer :: t(), packets :: [Packet.t()], timer_duration_ms :: timer()}
@type result :: {packets :: [Packet.t()], timer_duration_ms :: timer(), buffer :: t()}

@opaque t :: %__MODULE__{
latency: non_neg_integer(),
Expand Down Expand Up @@ -72,13 +72,13 @@ defmodule ExWebRTC.RTP.JitterBuffer do
{buffer, timer} = maybe_set_timer(buffer)
{_result, buffer} = try_insert_packet(buffer, packet)

{buffer, [], timer}
{[], timer, buffer}
end

def place_packet(buffer, packet) do
case try_insert_packet(buffer, packet) do
{:ok, buffer} -> send_packets(buffer)
{:error, buffer} -> {buffer, [], nil}
{:error, buffer} -> {[], nil, buffer}
end
end

Expand All @@ -94,7 +94,7 @@ defmodule ExWebRTC.RTP.JitterBuffer do
|> PacketStore.dump()
|> records_to_packets()

{%__MODULE__{latency: buffer.latency}, packets, nil}
{packets, nil, %__MODULE__{latency: buffer.latency}}
end

@doc """
Expand Down Expand Up @@ -127,7 +127,7 @@ defmodule ExWebRTC.RTP.JitterBuffer do

{buffer, timer} = maybe_set_timer(%__MODULE__{buffer | store: store})

{buffer, packets, timer}
{packets, timer, buffer}
end

@spec records_to_packets(Enumerable.t(PacketStore.Record.t())) :: [Packet.t()]
Expand Down
2 changes: 1 addition & 1 deletion test/ex_webrtc/rtp/jitter_buffer/realtime_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule ExWebRTC.RTP.JitterBuffer.RealtimeTest do
|> handle_jitter_buffer_result(state)
end

defp handle_jitter_buffer_result({buffer, packets, timer}, state) do
defp handle_jitter_buffer_result({packets, timer, buffer}, state) do
for packet <- packets do
send(state.owner, packet)
end
Expand Down
18 changes: 9 additions & 9 deletions test/ex_webrtc/rtp/jitter_buffer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ defmodule ExWebRTC.RTP.JitterBufferTest do

test "first packet starts timer that changes state", %{buffer: buffer, packet: packet} do
assert buffer.state == :initial_wait
{buffer, [], timer} = JitterBuffer.place_packet(buffer, packet)
{[], timer, buffer} = JitterBuffer.place_packet(buffer, packet)
assert timer == buffer.latency
{buffer, _packets, _timer} = JitterBuffer.handle_timer(buffer)
{_packets, _timer, buffer} = JitterBuffer.handle_timer(buffer)
assert buffer.state != :initial_wait
end

test "any new packet is kept", %{buffer: buffer, packet: packet} do
assert PacketStore.dump(buffer.store) == []
{buffer, [], _timer} = JitterBuffer.place_packet(buffer, packet)
{[], _timer, buffer} = JitterBuffer.place_packet(buffer, packet)

%{store: store} = buffer
{%Record{packet: ^packet}, new_store} = PacketStore.flush_one(store)
Expand All @@ -48,15 +48,15 @@ defmodule ExWebRTC.RTP.JitterBufferTest do
end

test "outputs it immediately if it is in order", %{buffer: buffer, packet: packet} do
{buffer, [^packet], _timer} = JitterBuffer.place_packet(buffer, packet)
{[^packet], _timer, buffer} = JitterBuffer.place_packet(buffer, packet)

%{store: store} = buffer
assert PacketStore.dump(store) == []
end

test "refuses to add that packet when it comes too late", %{buffer: buffer} do
late_packet = PacketFactory.sample_packet(@base_seq_number - 2)
{new_buffer, [], nil} = JitterBuffer.place_packet(buffer, late_packet)
{[], nil, new_buffer} = JitterBuffer.place_packet(buffer, late_packet)
assert new_buffer == buffer
end

Expand All @@ -78,7 +78,7 @@ defmodule ExWebRTC.RTP.JitterBufferTest do

buffer = %{buffer | store: store}

{%{store: result_store}, packets, _timer} = JitterBuffer.place_packet(buffer, first_packet)
{packets, _timer, %{store: result_store}} = JitterBuffer.place_packet(buffer, first_packet)

assert packets == [first_packet, second_packet, third_packet]

Expand All @@ -98,12 +98,12 @@ defmodule ExWebRTC.RTP.JitterBufferTest do

buffer = %{buffer | store: store, state: :timer_not_set}

{buffer, [], timer} = JitterBuffer.place_packet(buffer, packet)
{[], timer, buffer} = JitterBuffer.place_packet(buffer, packet)
assert timer != nil
assert buffer.state == :timer_set

Process.sleep(buffer.latency + 5)
{_buffer, [^packet], _timer} = JitterBuffer.handle_timer(buffer)
{[^packet], _timer, _buffer} = JitterBuffer.handle_timer(buffer)
end
end

Expand All @@ -119,7 +119,7 @@ defmodule ExWebRTC.RTP.JitterBufferTest do

{:ok, store} = PacketStore.insert_packet(store, packet)
buffer = %{buffer | store: store}
{buffer, [^packet], nil} = JitterBuffer.flush(buffer)
{[^packet], nil, buffer} = JitterBuffer.flush(buffer)

assert buffer.store == %PacketStore{}
end
Expand Down

0 comments on commit c0ef08d

Please sign in to comment.