Skip to content

Commit

Permalink
Update packet datastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorn committed Nov 8, 2023
1 parent 0113caf commit d0c63c8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
24 changes: 13 additions & 11 deletions c_src/libav.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,29 @@ int enif_get_packet(ErlNifEnv *env, ERL_NIF_TERM term, AVPacket **packet) {
ERL_NIF_TERM enif_packet_unpack(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]) {
AVPacket *packet;
ERL_NIF_TERM map, data;
ERL_NIF_TERM data;

enif_get_packet(env, argv[0], &packet);

void *ptr = enif_make_new_binary(env, packet->size, &data);
memcpy(ptr, packet->data, packet->size);

return data;
}

ERL_NIF_TERM enif_packet_metadata(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]) {
AVPacket *packet;
ERL_NIF_TERM map;
enif_get_packet(env, argv[0], &packet);

map = enif_make_new_map(env);
enif_make_map_put(env, map, enif_make_atom(env, "pts"),
enif_make_long(env, packet->pts), &map);
enif_make_map_put(env, map, enif_make_atom(env, "dts"),
enif_make_long(env, packet->dts), &map);
enif_make_map_put(env, map, enif_make_atom(env, "data"), data, &map);
enif_make_map_put(env, map, enif_make_atom(env, "stream_index"),
enif_make_int(env, packet->stream_index), &map);

return map;
}
Expand Down Expand Up @@ -315,13 +325,6 @@ ERL_NIF_TERM enif_decoder_stream_format(ErlNifEnv *env, int argc,
return map;
}

ERL_NIF_TERM enif_packet_stream_index(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]) {
AVPacket *packet;
enif_get_packet(env, argv[0], &packet);
return enif_make_int(env, packet->stream_index);
}

ERL_NIF_TERM enif_decoder_add_data(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]) {
Decoder *ctx;
Expand Down Expand Up @@ -470,8 +473,7 @@ static ErlNifFunc nif_funcs[] = {
ERL_NIF_DIRTY_JOB_IO_BOUND},
{"decoder_add_data", 2, enif_decoder_add_data, ERL_NIF_DIRTY_JOB_IO_BOUND},
// // General
{"packet_stream_index", 1, enif_packet_stream_index,
ERL_NIF_DIRTY_JOB_IO_BOUND},
{"packet_metadata", 1, enif_packet_metadata, ERL_NIF_DIRTY_JOB_IO_BOUND},
// // TODO
// // Maybe unpack_* would be better function naming.
{"packet_unpack", 1, enif_packet_unpack, ERL_NIF_DIRTY_JOB_IO_BOUND},
Expand Down
4 changes: 2 additions & 2 deletions lib/avx/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ defmodule AVx.NIF do
raise "NIF decoder_stream_format/1 not implemented"
end

def packet_stream_index(_packet) do
raise "NIF packet_stream_index/1 not implemented"
def packet_metadata(_packet) do
raise "NIF packet_metadata/1 not implemented"
end

def packet_unpack(_packet) do
Expand Down
26 changes: 15 additions & 11 deletions lib/avx/packet.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
defmodule AVx.Packet do
@type t :: %__MODULE__{ref: reference(), stream_index: pos_integer()}
@type unpacked :: %{dts: integer(), pts: integer(), data: binary()}
@type t :: %__MODULE__{
ref: reference(),
payload: nil | binary(),
stream_index: pos_integer(),
pts: pos_integer(),
dts: pos_integer()
}

defstruct [:ref, :stream_index]
defstruct ref: nil, payload: nil, stream_index: -1, pts: -1, dts: -1

@spec new(reference()) :: t()
def new(ref) do
%__MODULE__{ref: ref, stream_index: stream_index(ref)}
def new(nil) do
%__MODULE__{}
end

defp stream_index(nil), do: -1

defp stream_index(ref) when is_reference(ref) do
AVx.NIF.packet_stream_index(ref)
def new(ref) do
meta = AVx.NIF.packet_metadata(ref)
%__MODULE__{ref: ref, stream_index: meta.stream_index, pts: meta.pts, dts: meta.dts}
end

@spec unpack(t()) :: Packet.unpacked()
@spec unpack(t()) :: t()
def unpack(packet) do
AVx.NIF.packet_unpack(packet.ref)
%__MODULE__{packet | payload: AVx.NIF.packet_unpack(packet.ref)}
end
end
2 changes: 1 addition & 1 deletion test/avx/demuxer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule AVx.DemuxerTest do
|> Enum.reduce(0, fn {have, want}, count ->
assert have.pts == Map.fetch!(want, "pts")
assert have.dts == Map.fetch!(want, "dts")
assert byte_size(have.data) == Map.fetch!(want, "size") |> String.to_integer()
assert byte_size(have.payload) == Map.fetch!(want, "size") |> String.to_integer()
count + 1
end)

Expand Down

0 comments on commit d0c63c8

Please sign in to comment.