Prefer exposing `length` key to `complete` boolean

This commit is contained in:
Rob Watson 2018-04-19 12:12:23 +02:00
parent 8c99f55145
commit 395a6881ef
2 changed files with 9 additions and 7 deletions

View File

@ -7,8 +7,8 @@ defmodule MPEGAudioFrameParser.Frame do
padding: 0, padding: 0,
channel_mode: nil, channel_mode: nil,
data: <<>>, data: <<>>,
valid: false, length: 0,
complete: false valid: false
alias MPEGAudioFrameParser.Frame alias MPEGAudioFrameParser.Frame
require Logger require Logger
@ -28,7 +28,7 @@ defmodule MPEGAudioFrameParser.Frame do
|> Map.put(:sample_rate, parse_sample_rate(header)) |> Map.put(:sample_rate, parse_sample_rate(header))
|> Map.put(:padding, parse_padding(header)) |> Map.put(:padding, parse_padding(header))
%{frame | valid: header_valid?(frame)} %{frame | valid: header_valid?(frame), length: frame_length(frame)}
end end
def header_valid?(%Frame{version_id: version_id, layer: layer, bitrate: bitrate, sample_rate: sample_rate}) def header_valid?(%Frame{version_id: version_id, layer: layer, bitrate: bitrate, sample_rate: sample_rate})
@ -55,8 +55,8 @@ defmodule MPEGAudioFrameParser.Frame do
def add_bytes(frame, packet) do def add_bytes(frame, packet) do
limit = bytes_missing(frame) limit = bytes_missing(frame)
{:ok, bytes, rest, complete} = split_packet(packet, limit) {:ok, bytes, rest} = split_packet(packet, limit)
{:ok, %{frame | data: frame.data <> bytes, complete: complete}, rest} {:ok, %{frame | data: frame.data <> bytes}, rest}
end end
def bytes_missing(frame) do def bytes_missing(frame) do
@ -74,7 +74,7 @@ defmodule MPEGAudioFrameParser.Frame do
part1 = :binary.part(packet, {0, bytes_to_take}) part1 = :binary.part(packet, {0, bytes_to_take})
part2 = :binary.part(packet, {bytes_available, -bytes_to_leave}) part2 = :binary.part(packet, {bytes_available, -bytes_to_leave})
{:ok, part1, part2, bytes_to_take == limit} {:ok, part1, part2}
end end
defp parse_version(<<@sync_word::size(11), bits::size(2), _::bits>>), do: version_atom(bits) defp parse_version(<<@sync_word::size(11), bits::size(2), _::bits>>), do: version_atom(bits)

View File

@ -29,7 +29,9 @@ defmodule MPEGAudioFrameParser.Impl do
# Private Functions # Private Functions
# Synced, and the current frame is complete: # Synced, and the current frame is complete:
defp process_bytes(%{current_frame: %Frame{complete: true}} = state, packet) do defp process_bytes(%{current_frame: %Frame{length: frame_length, data: data}} = state, packet)
when frame_length == byte_size(data)
do
frames = [state.current_frame | state.frames] frames = [state.current_frame | state.frames]
process_bytes(%{state | current_frame: nil, frames: frames}, packet) process_bytes(%{state | current_frame: nil, frames: frames}, packet)
end end