audio: syntax and error checking

This commit is contained in:
Trek H 2019-06-20 18:04:58 +09:30
parent 370aa19c23
commit 95fc69b3c5
3 changed files with 9 additions and 11 deletions

View File

@ -24,8 +24,6 @@ LICENSE
package codecutil
import "fmt"
// numCodecs is the number of entries in the list of codecs.
const numCodecs = 5
@ -39,10 +37,7 @@ const (
MJPEG
)
// Validate recieves an int representing a codec and checks if it is valid.
func Validate(codec uint8) error {
if codec < 0 || codec >= numCodecs {
return fmt.Errorf("invalid codec")
}
return nil
// IsValid recieves an int representing a codec and checks if it is valid.
func IsValid(codec uint8) bool {
return 0 <= codec && codec < numCodecs
}

View File

@ -197,7 +197,7 @@ func TestEncodePcm(t *testing.T) {
for i+PacketSize <= len(clip) {
// Check MTS packet
if !(pkt.PID() == AudioPid) {
if pkt.PID() != AudioPid {
i += PacketSize
if i+PacketSize <= len(clip) {
copy(pkt[:], clip[i:i+PacketSize])

View File

@ -94,7 +94,10 @@ type OpenError error
// NewDevice initializes and returns an Device which can be started, read from, and stopped.
func NewDevice(cfg *Config, l Logger) (*Device, error) {
validate(cfg)
err := validate(cfg)
if err != nil {
return nil, err
}
d := &Device{
Config: cfg,
@ -102,7 +105,7 @@ func NewDevice(cfg *Config, l Logger) (*Device, error) {
}
// Open the requested audio device.
err := d.open()
err = d.open()
if err != nil {
d.l.Log(logger.Error, pkg+"failed to open device")
return nil, err