github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/mpeg4audio/stream_mux_config_test.go (about)

     1  package mpeg4audio
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  var streamMuxConfigCases = []struct {
    10  	name string
    11  	enc  []byte
    12  	dec  StreamMuxConfig
    13  }{
    14  	{
    15  		"lc",
    16  		[]byte{0x40, 0x00, 0x26, 0x20, 0x3f, 0xc0},
    17  		StreamMuxConfig{
    18  			Programs: []*StreamMuxConfigProgram{{
    19  				Layers: []*StreamMuxConfigLayer{{
    20  					AudioSpecificConfig: &AudioSpecificConfig{
    21  						Type:         2,
    22  						SampleRate:   24000,
    23  						ChannelCount: 2,
    24  					},
    25  					LatmBufferFullness: 255,
    26  				}},
    27  			}},
    28  		},
    29  	},
    30  	{
    31  		"he-aac",
    32  		[]byte{0x40, 0x00, 0x26, 0x10, 0x3f, 0xc0},
    33  		StreamMuxConfig{
    34  			Programs: []*StreamMuxConfigProgram{{
    35  				Layers: []*StreamMuxConfigLayer{{
    36  					AudioSpecificConfig: &AudioSpecificConfig{
    37  						Type:         2,
    38  						SampleRate:   24000,
    39  						ChannelCount: 1,
    40  					},
    41  					LatmBufferFullness: 255,
    42  				}},
    43  			}},
    44  		},
    45  	},
    46  	{
    47  		"sbr",
    48  		[]byte{0x40, 0x00, 0x56, 0x23, 0x10, 0x1f, 0xe0},
    49  		StreamMuxConfig{
    50  			Programs: []*StreamMuxConfigProgram{{
    51  				Layers: []*StreamMuxConfigLayer{{
    52  					AudioSpecificConfig: &AudioSpecificConfig{
    53  						Type:                2,
    54  						ExtensionType:       5,
    55  						ExtensionSampleRate: 48000,
    56  						SampleRate:          24000,
    57  						ChannelCount:        2,
    58  					},
    59  					LatmBufferFullness: 255,
    60  				}},
    61  			}},
    62  		},
    63  	},
    64  	{
    65  		"ps",
    66  		[]byte{0x40, 0x01, 0xd6, 0x13, 0x10, 0x1f, 0xe0},
    67  		StreamMuxConfig{
    68  			Programs: []*StreamMuxConfigProgram{{
    69  				Layers: []*StreamMuxConfigLayer{{
    70  					AudioSpecificConfig: &AudioSpecificConfig{
    71  						Type:                2,
    72  						ExtensionType:       29,
    73  						ExtensionSampleRate: 48000,
    74  						SampleRate:          24000,
    75  						ChannelCount:        1,
    76  					},
    77  					LatmBufferFullness: 255,
    78  				}},
    79  			}},
    80  		},
    81  	},
    82  	{
    83  		"other data and checksum",
    84  		[]byte{0x40, 0x00, 0x24, 0x10, 0xad, 0xca, 0x00},
    85  		StreamMuxConfig{
    86  			Programs: []*StreamMuxConfigProgram{{
    87  				Layers: []*StreamMuxConfigLayer{{
    88  					AudioSpecificConfig: &AudioSpecificConfig{
    89  						Type:         2,
    90  						SampleRate:   44100,
    91  						ChannelCount: 1,
    92  					},
    93  					FrameLengthType: 2,
    94  				}},
    95  			}},
    96  			OtherDataPresent: true,
    97  			OtherDataLenBits: 220,
    98  			CRCCheckPresent:  true,
    99  			CRCCheckSum:      64,
   100  		},
   101  	},
   102  	{
   103  		"other data > 256 and checksum",
   104  		[]byte{0x40, 0x00, 0x24, 0x10, 0xb0, 0x33, 0x85, 0x0},
   105  		StreamMuxConfig{
   106  			Programs: []*StreamMuxConfigProgram{{
   107  				Layers: []*StreamMuxConfigLayer{{
   108  					AudioSpecificConfig: &AudioSpecificConfig{
   109  						Type:         2,
   110  						SampleRate:   44100,
   111  						ChannelCount: 1,
   112  					},
   113  					FrameLengthType: 2,
   114  				}},
   115  			}},
   116  			OtherDataPresent: true,
   117  			OtherDataLenBits: 880,
   118  			CRCCheckPresent:  true,
   119  			CRCCheckSum:      64,
   120  		},
   121  	},
   122  }
   123  
   124  func TestStreamMuxConfigUnmarshal(t *testing.T) {
   125  	for _, ca := range streamMuxConfigCases {
   126  		t.Run(ca.name, func(t *testing.T) {
   127  			var dec StreamMuxConfig
   128  			err := dec.Unmarshal(ca.enc)
   129  			require.NoError(t, err)
   130  			require.Equal(t, ca.dec, dec)
   131  		})
   132  	}
   133  }
   134  
   135  func TestStreamMuxConfigUnmarshalTruncated(t *testing.T) {
   136  	var dec StreamMuxConfig
   137  	err := dec.Unmarshal([]byte{0x40, 0x00, 0x23, 0x10})
   138  	require.NoError(t, err)
   139  	require.Equal(t, StreamMuxConfig{
   140  		Programs: []*StreamMuxConfigProgram{{
   141  			Layers: []*StreamMuxConfigLayer{{
   142  				AudioSpecificConfig: &AudioSpecificConfig{
   143  					Type:         2,
   144  					SampleRate:   48000,
   145  					ChannelCount: 1,
   146  				},
   147  				LatmBufferFullness: 255,
   148  			}},
   149  		}},
   150  	}, dec)
   151  }
   152  
   153  func TestStreamMuxConfigMarshal(t *testing.T) {
   154  	for _, ca := range streamMuxConfigCases {
   155  		t.Run(ca.name, func(t *testing.T) {
   156  			enc, err := ca.dec.Marshal()
   157  			require.NoError(t, err)
   158  			require.Equal(t, ca.enc, enc)
   159  		})
   160  	}
   161  }
   162  
   163  func FuzzStreamMuxConfigUnmarshal(f *testing.F) {
   164  	f.Fuzz(func(_ *testing.T, b []byte) {
   165  		var conf StreamMuxConfig
   166  		conf.Unmarshal(b) //nolint:errcheck
   167  	})
   168  }