From c0ef08d8e95b62934cd27dd6bc53e52b8b1ef803 Mon Sep 17 00:00:00 2001 From: Jakub Pisarek <99591440+sgfn@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:14:48 +0200 Subject: [PATCH] Reorder result tuple --- .../lib/save_to_file/peer_handler.ex | 2 +- lib/ex_webrtc/rtp/jitter_buffer.ex | 12 ++++++------ .../rtp/jitter_buffer/realtime_test.exs | 2 +- test/ex_webrtc/rtp/jitter_buffer_test.exs | 18 +++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/save_to_file/lib/save_to_file/peer_handler.ex b/examples/save_to_file/lib/save_to_file/peer_handler.ex index e0e72ab..793cfa1 100644 --- a/examples/save_to_file/lib/save_to_file/peer_handler.ex +++ b/examples/save_to_file/lib/save_to_file/peer_handler.ex @@ -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} diff --git a/lib/ex_webrtc/rtp/jitter_buffer.ex b/lib/ex_webrtc/rtp/jitter_buffer.ex index 04a5a0a..7a4ea0e 100644 --- a/lib/ex_webrtc/rtp/jitter_buffer.ex +++ b/lib/ex_webrtc/rtp/jitter_buffer.ex @@ -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(), @@ -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 @@ -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 """ @@ -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()] diff --git a/test/ex_webrtc/rtp/jitter_buffer/realtime_test.exs b/test/ex_webrtc/rtp/jitter_buffer/realtime_test.exs index 36fd657..fd0f157 100644 --- a/test/ex_webrtc/rtp/jitter_buffer/realtime_test.exs +++ b/test/ex_webrtc/rtp/jitter_buffer/realtime_test.exs @@ -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 diff --git a/test/ex_webrtc/rtp/jitter_buffer_test.exs b/test/ex_webrtc/rtp/jitter_buffer_test.exs index ae5743d..aa75576 100644 --- a/test/ex_webrtc/rtp/jitter_buffer_test.exs +++ b/test/ex_webrtc/rtp/jitter_buffer_test.exs @@ -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) @@ -48,7 +48,7 @@ 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) == [] @@ -56,7 +56,7 @@ defmodule ExWebRTC.RTP.JitterBufferTest do 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 @@ -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] @@ -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 @@ -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