2019-05-08 10:51:21 +03:00
|
|
|
package revid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-05-21 06:09:10 +03:00
|
|
|
"github.com/yobert/alsa"
|
2019-05-09 05:41:02 +03:00
|
|
|
)
|
2019-05-08 10:51:21 +03:00
|
|
|
|
2019-05-21 06:09:10 +03:00
|
|
|
func TestAudioDevice(t *testing.T) {
|
|
|
|
// We want to open a device with a standard configuration.
|
|
|
|
ac := &AudioConfig{
|
|
|
|
SampleRate: 8000,
|
|
|
|
Channels: 1,
|
|
|
|
RecPeriod: 1,
|
|
|
|
BitDepth: 16,
|
|
|
|
Codec: ADPCM,
|
2019-05-09 05:41:02 +03:00
|
|
|
}
|
2019-05-08 11:37:33 +03:00
|
|
|
|
2019-05-21 06:09:10 +03:00
|
|
|
// Check that a device exists with the desired parameters.
|
|
|
|
cards, err := alsa.OpenCards()
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("skipping, no audio card found")
|
|
|
|
}
|
|
|
|
defer alsa.CloseCards(cards)
|
|
|
|
var testDev *alsa.Device
|
|
|
|
for _, card := range cards {
|
|
|
|
devices, err := card.Devices()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, dev := range devices {
|
|
|
|
if dev.Type != alsa.PCM || !dev.Record {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testDev = dev
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if testDev == nil {
|
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
|
|
|
_, err = testDev.NegotiateChannels(2)
|
2019-05-09 05:41:02 +03:00
|
|
|
if err != nil {
|
2019-05-21 06:09:10 +03:00
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
|
|
|
foundRate := false
|
|
|
|
for i := 0; i < len(rates) && !foundRate; i++ {
|
|
|
|
if rates[i] < ac.SampleRate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rates[i]%ac.SampleRate == 0 {
|
|
|
|
_, err = testDev.NegotiateRate(rates[i])
|
|
|
|
if err == nil {
|
|
|
|
foundRate = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundRate {
|
|
|
|
_, err = testDev.NegotiateRate(defaultSampleRate)
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
2019-05-09 05:41:02 +03:00
|
|
|
}
|
2019-05-21 06:09:10 +03:00
|
|
|
_, err = testDev.NegotiateFormat(alsa.S16_LE)
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
|
|
|
_, err = testDev.NegotiateBufferSize(8192, 16384)
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
|
|
|
if err = testDev.Prepare(); err != nil {
|
|
|
|
t.Skip("skipping, no suitable audio device found")
|
|
|
|
}
|
|
|
|
testDev.Close()
|
|
|
|
|
|
|
|
ai := NewAudioDevice(ac)
|
|
|
|
|
|
|
|
ai.Start()
|
2019-05-08 11:37:33 +03:00
|
|
|
|
2019-05-21 06:09:10 +03:00
|
|
|
ai.Stop()
|
2019-05-08 10:51:21 +03:00
|
|
|
}
|