Skip to content

Commit 73ac5f2

Browse files
committed
add test
1 parent c47ae5b commit 73ac5f2

6 files changed

Lines changed: 49 additions & 3 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ XAV_VIDEO_CONVERTER_SO = $(PRIV_DIR)/libxavvideoconverter.so
1616
DECODER_HEADERS = $(XAV_DIR)/xav_decoder.h $(XAV_DIR)/decoder.h $(XAV_DIR)/video_converter.h $(XAV_DIR)/audio_converter.h $(XAV_DIR)/utils.h $(XAV_DIR)/channel_layout.h
1717
DECODER_SOURCES = $(XAV_DIR)/xav_decoder.c $(XAV_DIR)/decoder.c $(XAV_DIR)/video_converter.c $(XAV_DIR)/audio_converter.c $(XAV_DIR)/utils.c
1818

19-
ENCODER_HEADERS = $(XAV_DIR)/xav_encoder.h $(XAV_DIR)/encoder.h $(XAV_DIR)/utils.h
20-
ENCODER_SOURCES = $(XAV_DIR)/xav_encoder.c $(XAV_DIR)/encoder.c $(XAV_DIR)/utils.c
19+
ENCODER_HEADERS = $(XAV_DIR)/xav_encoder.h $(XAV_DIR)/encoder.h $(XAV_DIR)/utils.h $(XAV_DIR)/channel_layout.h
20+
ENCODER_SOURCES = $(XAV_DIR)/xav_encoder.c $(XAV_DIR)/encoder.c $(XAV_DIR)/utils.c $(XAV_DIR)/channel_layout.c
2121

2222
READER_HEADERS = $(XAV_DIR)/xav_reader.h $(XAV_DIR)/reader.h $(XAV_DIR)/video_converter.h $(XAV_DIR)/audio_converter.h $(XAV_DIR)/utils.h $(XAV_DIR)/channel_layout.h
2323
READER_SOURCES = $(XAV_DIR)/xav_reader.c $(XAV_DIR)/reader.c $(XAV_DIR)/video_converter.c $(XAV_DIR)/audio_converter.c $(XAV_DIR)/utils.c

c_src/xav/xav_encoder.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "xav_encoder.h"
2+
#include "channel_layout.h"
23

34
ErlNifResourceType *xav_encoder_resource_type;
45

@@ -132,10 +133,16 @@ ERL_NIF_TERM new (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
132133
if (encoder_config.codec->type == AVMEDIA_TYPE_AUDIO) {
133134
xav_encoder->frame->format = encoder_config.format;
134135
xav_encoder->frame->nb_samples = xav_encoder->encoder->c->frame_size;
136+
// For encoder that accepts dynamic frame size, we set it to 1024.
137+
if (xav_encoder->frame->nb_samples == 0) {
138+
xav_encoder->frame->nb_samples = 1024;
139+
}
140+
135141
if (xav_set_frame_channel_layout(xav_encoder->frame, &encoder_config.channel_layout) < 0) {
136142
ret = xav_nif_raise(env, "failed_to_set_channel_layout");
137143
goto clean;
138144
}
145+
139146
if (av_frame_get_buffer(xav_encoder->frame, 0) < 0) {
140147
ret = xav_nif_raise(env, "failed_to_get_buffer");
141148
goto clean;
@@ -197,6 +204,8 @@ ERL_NIF_TERM encode(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
197204
}
198205
} else {
199206
frame->pts = pts;
207+
frame->nb_samples = input.size / av_get_bytes_per_sample(xav_encoder->encoder->c->sample_fmt);
208+
200209
int nb_channels = xav_get_nb_channels(frame);
201210
ret = av_samples_fill_arrays(frame->data, frame->linesize, input.data, nb_channels,
202211
frame->nb_samples, xav_encoder->encoder->c->sample_fmt, 1);

c_src/xav/xav_encoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "channel_layout.c"
21
#include "encoder.h"
32
#include "utils.h"
43
#include <libavutil/pixfmt.h>

test/encoder_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,33 @@ defmodule Xav.EncoderTest do
111111
assert length(packets) == 20
112112
assert Enum.all?(packets, &(&1.dts == &1.pts)), "dts should be equal to pts"
113113
end
114+
115+
test "encode audio samples" do
116+
audio_file = "test/fixtures/encoder/audio/input-s16le.raw"
117+
ref_file = "test/fixtures/encoder/audio/reference.al"
118+
119+
encoder =
120+
Xav.Encoder.new(:pcm_alaw,
121+
format: :s16,
122+
channel_layout: "mono",
123+
sample_rate: 8000
124+
)
125+
126+
encoded_data =
127+
File.stream!(audio_file, 20)
128+
|> Stream.map(&%Xav.Frame{type: :audio, data: &1, format: :s16, pts: 0})
129+
|> Stream.transform(
130+
fn -> encoder end,
131+
fn frame, encoder ->
132+
{Xav.Encoder.encode(encoder, frame), encoder}
133+
end,
134+
fn encoder -> {Xav.Encoder.flush(encoder), encoder} end,
135+
fn _encoder -> :ok end
136+
)
137+
|> Stream.map(& &1.data)
138+
|> Enum.join()
139+
140+
assert File.read!(ref_file) == encoded_data
141+
end
114142
end
115143
end
334 KB
Binary file not shown.

test/fixtures/encoder/audio/reference.al

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)