2019-08-08 08:53:09 +03:00
|
|
|
/*
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-08-08 08:09:55 +03:00
|
|
|
package revid
|
|
|
|
|
|
|
|
import (
|
2019-08-08 08:27:09 +03:00
|
|
|
"time"
|
|
|
|
|
2019-11-12 05:58:53 +03:00
|
|
|
"bitbucket.org/ausocean/av/device/alsa"
|
2019-08-08 08:27:09 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-08-08 08:09:55 +03:00
|
|
|
)
|
|
|
|
|
2019-11-12 05:58:53 +03:00
|
|
|
// startAudioDevice is used to start capturing audio from an ALSA audio device and processing it.
|
2019-08-08 08:09:55 +03:00
|
|
|
// It returns a function that can be used to stop the device and any errors that occur.
|
|
|
|
func (r *Revid) startAudioDevice() (func() error, error) {
|
|
|
|
// Create audio device.
|
2019-11-12 05:58:53 +03:00
|
|
|
ai := alsa.NewALSA(r.cfg.Logger)
|
2019-08-08 08:09:55 +03:00
|
|
|
|
2019-11-11 09:35:10 +03:00
|
|
|
err := ai.Set(r.cfg)
|
2019-08-08 08:09:55 +03:00
|
|
|
if err != nil {
|
2019-11-12 05:58:53 +03:00
|
|
|
r.cfg.Logger.Log(logger.Fatal, pkg+"failed to setup ALSA device", "error", err.Error())
|
2019-08-08 08:09:55 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 05:58:53 +03:00
|
|
|
// Start ALSA audio device
|
2019-08-08 08:09:55 +03:00
|
|
|
err = ai.Start()
|
|
|
|
if err != nil {
|
2019-11-12 05:58:53 +03:00
|
|
|
r.cfg.Logger.Log(logger.Fatal, pkg+"failed to start ALSA device", "error", err.Error())
|
2019-08-08 08:09:55 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 05:58:53 +03:00
|
|
|
// Process output from ALSA audio device.
|
2019-11-06 09:57:10 +03:00
|
|
|
r.cfg.ChunkSize = ai.ChunkSize()
|
2019-08-08 08:09:55 +03:00
|
|
|
r.wg.Add(1)
|
2019-11-06 09:57:10 +03:00
|
|
|
go r.processFrom(ai, time.Duration(float64(time.Second)/r.cfg.WriteRate))
|
2019-08-08 08:09:55 +03:00
|
|
|
return func() error {
|
|
|
|
ai.Stop()
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}
|