github.com/noriah/catnip@v1.8.5/input/ffmpeg/sndio.go (about)

     1  package ffmpeg
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/noriah/catnip/input"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  func init() {
    12  	input.RegisterBackend("ffmpeg-sndio", Sndio{})
    13  }
    14  
    15  // Sndio is the sndio input for FFmpeg.
    16  type Sndio struct{}
    17  
    18  func (p Sndio) Init() error {
    19  	return nil
    20  }
    21  
    22  func (p Sndio) Close() error {
    23  	return nil
    24  }
    25  
    26  // Devices returns a list of sndio devices from /dev/audio*. This is
    27  // kernel-specific and is only known to work on OpenBSD.
    28  func (p Sndio) Devices() ([]input.Device, error) {
    29  	n, err := filepath.Glob("/dev/audio*")
    30  	if err != nil {
    31  		return nil, errors.Wrap(err, "failed to glob /dev/audio")
    32  	}
    33  
    34  	devices := make([]input.Device, len(n))
    35  	for i, path := range n {
    36  		devices[i] = SndioDevice(path)
    37  	}
    38  
    39  	return devices, nil
    40  }
    41  
    42  func (p Sndio) DefaultDevice() (input.Device, error) {
    43  	return SndioDevice("/dev/audio0"), nil
    44  }
    45  
    46  func (p Sndio) Start(cfg input.SessionConfig) (input.Session, error) {
    47  	dv, ok := cfg.Device.(SndioDevice)
    48  	if !ok {
    49  		return nil, fmt.Errorf("invalid device type %T", cfg.Device)
    50  	}
    51  
    52  	return NewSession(dv, cfg)
    53  }
    54  
    55  // SndioDevice is a string that is the path to /dev/audioN.
    56  type SndioDevice string
    57  
    58  func (d SndioDevice) InputArgs() []string {
    59  	return []string{"-f", "sndio", "-i", string(d)}
    60  }
    61  
    62  func (d SndioDevice) String() string {
    63  	return string(d)
    64  }