|
| 1 | +package resampler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/asticode/go-astiav" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/xaionaro-go/avpipeline/codec" |
| 10 | + "github.com/xaionaro-go/avpipeline/frame" |
| 11 | +) |
| 12 | + |
| 13 | +func defaultPCMFormat() codec.PCMAudioFormat { |
| 14 | + return codec.PCMAudioFormat{ |
| 15 | + SampleFormat: astiav.SampleFormatFlt, |
| 16 | + SampleRate: 48000, |
| 17 | + ChannelLayout: astiav.ChannelLayoutStereo, |
| 18 | + ChunkSize: 8, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestGetPCMFormatFromFrame(t *testing.T) { |
| 23 | + t.Run("NilFrame", func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + require.Nil(t, getPCMFormatFromFrame(context.Background(), nil)) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("PopulatedFrame", func(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + fr := frame.Pool.Get() |
| 32 | + defer frame.Pool.Put(fr) |
| 33 | + fr.Unref() |
| 34 | + fr.SetSampleFormat(astiav.SampleFormatS16) |
| 35 | + fr.SetSampleRate(44100) |
| 36 | + fr.SetChannelLayout(astiav.ChannelLayoutMono) |
| 37 | + fr.SetNbSamples(123) |
| 38 | + |
| 39 | + got := getPCMFormatFromFrame(context.Background(), fr) |
| 40 | + require.NotNil(t, got) |
| 41 | + |
| 42 | + expected := codec.PCMAudioFormat{ |
| 43 | + SampleFormat: astiav.SampleFormatS16, |
| 44 | + SampleRate: 44100, |
| 45 | + ChannelLayout: astiav.ChannelLayoutMono, |
| 46 | + ChunkSize: 123, |
| 47 | + } |
| 48 | + require.True(t, expected.Equal(*got)) |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func TestResamplerAllocateOutputFrame(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + ctx := context.Background() |
| 56 | + fmt := defaultPCMFormat() |
| 57 | + r, err := New(ctx, fmt) |
| 58 | + require.NoError(t, err) |
| 59 | + t.Cleanup(func() { require.NoError(t, r.Close(ctx)) }) |
| 60 | + |
| 61 | + out, err := r.AllocateOutputFrame(ctx) |
| 62 | + require.NoError(t, err) |
| 63 | + t.Cleanup(func() { frame.Pool.Put(out) }) |
| 64 | + |
| 65 | + require.Equal(t, fmt.ChunkSize, out.NbSamples()) |
| 66 | + require.Equal(t, fmt.ChannelLayout, out.ChannelLayout()) |
| 67 | + require.Equal(t, fmt.SampleFormat, out.SampleFormat()) |
| 68 | + require.Equal(t, fmt.SampleRate, out.SampleRate()) |
| 69 | +} |
| 70 | + |
| 71 | +func TestResamplerReceiveFrameFlow(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + fmt := defaultPCMFormat() |
| 76 | + r, err := New(ctx, fmt) |
| 77 | + require.NoError(t, err) |
| 78 | + t.Cleanup(func() { require.NoError(t, r.Close(ctx)) }) |
| 79 | + |
| 80 | + out, err := r.AllocateOutputFrame(ctx) |
| 81 | + require.NoError(t, err) |
| 82 | + defer frame.Pool.Put(out) |
| 83 | + |
| 84 | + require.ErrorIs(t, r.ReceiveFrame(ctx, out), astiav.ErrEof) |
| 85 | + |
| 86 | + writeSamples(t, r, fmt.ChunkSize/2) |
| 87 | + require.ErrorIs(t, r.ReceiveFrame(ctx, out), astiav.ErrEagain) |
| 88 | + |
| 89 | + writeSamples(t, r, fmt.ChunkSize) |
| 90 | + require.NoError(t, r.ReceiveFrame(ctx, out)) |
| 91 | + require.Equal(t, fmt.ChunkSize, out.NbSamples()) |
| 92 | + |
| 93 | + flushFrame, err := r.AllocateOutputFrame(ctx) |
| 94 | + require.NoError(t, err) |
| 95 | + defer frame.Pool.Put(flushFrame) |
| 96 | + require.NoError(t, r.Flush(ctx, flushFrame)) |
| 97 | + require.Equal(t, fmt.ChunkSize/2, flushFrame.NbSamples()) |
| 98 | + |
| 99 | + require.ErrorIs(t, r.ReceiveFrame(ctx, out), astiav.ErrEof) |
| 100 | +} |
| 101 | + |
| 102 | +func TestResamplerSendFrameValidations(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + |
| 105 | + ctx := context.Background() |
| 106 | + fmt := defaultPCMFormat() |
| 107 | + r, err := New(ctx, fmt) |
| 108 | + require.NoError(t, err) |
| 109 | + t.Cleanup(func() { require.NoError(t, r.Close(ctx)) }) |
| 110 | + |
| 111 | + firstFormat := codec.PCMAudioFormat{ |
| 112 | + SampleFormat: astiav.SampleFormatS16, |
| 113 | + SampleRate: 44100, |
| 114 | + ChannelLayout: astiav.ChannelLayoutMono, |
| 115 | + ChunkSize: 4, |
| 116 | + } |
| 117 | + |
| 118 | + secondFormat := codec.PCMAudioFormat{ |
| 119 | + SampleFormat: astiav.SampleFormatS16, |
| 120 | + SampleRate: 48000, |
| 121 | + ChannelLayout: astiav.ChannelLayoutMono, |
| 122 | + ChunkSize: 4, |
| 123 | + } |
| 124 | + |
| 125 | + firstFrame := buildPCMFrame(t, firstFormat) |
| 126 | + defer frame.Pool.Put(firstFrame) |
| 127 | + |
| 128 | + secondFrame := buildPCMFrame(t, secondFormat) |
| 129 | + defer frame.Pool.Put(secondFrame) |
| 130 | + |
| 131 | + err = r.SendFrame(ctx, firstFrame) |
| 132 | + require.NoError(t, err) |
| 133 | + require.NotNil(t, r.FormatInput) |
| 134 | + require.True(t, firstFormat.Equal(*r.FormatInput)) |
| 135 | + |
| 136 | + err = r.SendFrame(ctx, secondFrame) |
| 137 | + require.Error(t, err) |
| 138 | + require.Contains(t, err.Error(), "input frame format changed") |
| 139 | +} |
| 140 | + |
| 141 | +func TestResamplerUnspecifiedChannelOrder(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + |
| 144 | + ctx := context.Background() |
| 145 | + fmtOut := defaultPCMFormat() |
| 146 | + r, err := New(ctx, fmtOut) |
| 147 | + require.NoError(t, err) |
| 148 | + t.Cleanup(func() { require.NoError(t, r.Close(ctx)) }) |
| 149 | + |
| 150 | + inputFormat := codec.PCMAudioFormat{ |
| 151 | + SampleFormat: astiav.SampleFormatS16, |
| 152 | + SampleRate: 44100, |
| 153 | + ChannelLayout: astiav.ChannelLayoutStereo, |
| 154 | + ChunkSize: fmtOut.ChunkSize, |
| 155 | + } |
| 156 | + inputFormat.ChannelLayout.SetOrder(astiav.ChannelOrderUnspecified) |
| 157 | + |
| 158 | + firstFrame := buildPCMFrame(t, inputFormat) |
| 159 | + defer frame.Pool.Put(firstFrame) |
| 160 | + |
| 161 | + secondFrame := buildPCMFrame(t, inputFormat) |
| 162 | + defer frame.Pool.Put(secondFrame) |
| 163 | + |
| 164 | + require.NoError(t, r.SendFrame(ctx, firstFrame)) |
| 165 | + require.NotNil(t, r.FormatInput) |
| 166 | + |
| 167 | + err = r.SendFrame(ctx, secondFrame) |
| 168 | + require.Error(t, err) |
| 169 | + require.ErrorIs(t, err, astiav.ErrInputChanged) |
| 170 | +} |
| 171 | + |
| 172 | +func writeSamples(t *testing.T, r *Resampler, samples int) { |
| 173 | + t.Helper() |
| 174 | + pcmFrame := buildPCMFrame(t, codec.PCMAudioFormat{ |
| 175 | + SampleFormat: r.FormatOutput.SampleFormat, |
| 176 | + SampleRate: r.FormatOutput.SampleRate, |
| 177 | + ChannelLayout: r.FormatOutput.ChannelLayout, |
| 178 | + ChunkSize: samples, |
| 179 | + }) |
| 180 | + defer func() { |
| 181 | + frame.Pool.Put(pcmFrame) |
| 182 | + }() |
| 183 | + _, err := r.AudioFifo.Write(pcmFrame) |
| 184 | + require.NoError(t, err) |
| 185 | +} |
| 186 | + |
| 187 | +func buildPCMFrame(t *testing.T, fmt codec.PCMAudioFormat) *astiav.Frame { |
| 188 | + t.Helper() |
| 189 | + fr := frame.Pool.Get() |
| 190 | + fr.Unref() |
| 191 | + fr.SetSampleFormat(fmt.SampleFormat) |
| 192 | + fr.SetSampleRate(fmt.SampleRate) |
| 193 | + fr.SetChannelLayout(fmt.ChannelLayout) |
| 194 | + fr.SetNbSamples(fmt.ChunkSize) |
| 195 | + require.NoError(t, fr.AllocBuffer(0)) |
| 196 | + return fr |
| 197 | +} |
0 commit comments