Minor cleanup and fixes

- Small refactor to remove a function body
- Check parameter type in Frame.from_header/1
- Prefer doctest to mpeg_audio_frame_parser_test
This commit is contained in:
Rob Watson 2018-04-18 09:09:24 +02:00
parent d89db0ec49
commit 82f661cc25
3 changed files with 6 additions and 58 deletions

View File

@ -17,7 +17,8 @@ defmodule MPEGAudioFrameParser.Frame do
@header_length 32 @header_length 32
def from_header(header) def from_header(header)
when bit_size(header) == @header_length when is_binary(header)
and bit_size(header) == @header_length
do do
frame = %Frame{data: header} frame = %Frame{data: header}
|> Map.put(:version_id, parse_version(header)) |> Map.put(:version_id, parse_version(header))

View File

@ -62,19 +62,11 @@ defmodule MPEGAudioFrameParser.Impl do
process_bytes(%{state | current_frame: nil}, rest) process_bytes(%{state | current_frame: nil}, rest)
end end
# Synced, frame complete, and looks like another sync word. Create a new frame struct: # Synced, and the current frame is complete:
defp process_bytes(%{current_frame: %Frame{complete: true}} = state, <<@sync_word::size(11), header::size(21), rest::bits>>) do
header = <<@sync_word::size(11), header::size(21)>>
frames = [state.current_frame | state.frames]
new_state = %{state | current_frame: Frame.from_header(header), frames: frames}
process_bytes(new_state, rest)
end
# Synced, frame complete, but next frame looks bad. Prepend, discard a byte and unsync:
defp process_bytes(%{current_frame: %Frame{complete: true}} = state, packet) do defp process_bytes(%{current_frame: %Frame{complete: true}} = state, packet) do
Logger.warn "Lost sync. Discarding a byte and searching again for a sync word." frames = [state.current_frame | state.frames]
<<_byte, rest>> = state.current_frame.data new_state = %{state | current_frame: nil, frames: frames}
process_bytes(%{state | current_frame: nil}, <<rest, packet::bits>>) process_bytes(new_state, packet)
end end
# Synced, current frame not complete and we have bytes available. Add bytes to frame: # Synced, current frame not complete and we have bytes available. Add bytes to frame:

View File

@ -1,45 +0,0 @@
defmodule MPEGAudioFrameParserTest do
use ExUnit.Case
alias MPEGAudioFrameParser.Frame
doctest MPEGAudioFrameParser
# MP3, 128kbps, no CRC protection, 44100hz, no padding, stereo
@frame1 <<0b11111111111_11_01_0_1001_00_0_0_00_00_0_0_00::size(32), 1::size(3304)>>
@frame2 <<0b11111111111_11_01_0_1001_00_0_0_00_00_0_0_00::size(32), 0::size(3304)>>
test "start_link" do
MPEGAudioFrameParser.start_link()
end
test "add_packet" do
MPEGAudioFrameParser.start_link()
MPEGAudioFrameParser.add_packet(@frame1)
result = MPEGAudioFrameParser.add_packet(@frame2)
assert [%Frame{data: @frame1}] = result
end
test "cast_packet" do
MPEGAudioFrameParser.start_link()
MPEGAudioFrameParser.cast_packet(@frame1)
MPEGAudioFrameParser.cast_packet(@frame2)
end
test "pop_frame" do
MPEGAudioFrameParser.start_link()
MPEGAudioFrameParser.cast_packet(@frame1)
MPEGAudioFrameParser.cast_packet(@frame2)
assert %Frame{data: @frame1} = MPEGAudioFrameParser.pop_frame()
assert nil == MPEGAudioFrameParser.pop_frame()
end
test "flush" do
MPEGAudioFrameParser.start_link()
MPEGAudioFrameParser.cast_packet(@frame1)
MPEGAudioFrameParser.cast_packet(@frame2)
MPEGAudioFrameParser.flush()
assert nil == MPEGAudioFrameParser.pop_frame()
end
end