Compare commits

..

3 Commits

Author SHA1 Message Date
484e0f5295 spdif: When active indicate the bitrate
All checks were successful
continuous-integration/drone/push Build is passing
2024-11-23 10:30:48 +01:00
7c0d1a8ca5 spdif: Call end chan when process exit 2024-11-23 10:30:17 +01:00
b22259c423 spdif: Initialize even if bitrate is null, stop after 30 seconds of null bitrate 2024-11-23 10:30:13 +01:00

View File

@ -53,6 +53,9 @@ func init() {
}
func (s *SPDIFSource) GetName() string {
if s.IsActive() {
return fmt.Sprintf("S/PDIF %.1f kHz", float32(s.Bitrate)/1000)
}
return "S/PDIF"
}
@ -72,6 +75,13 @@ func (s *SPDIFSource) Enable() error {
s.processPlay.Process.Kill()
}
// Update bitrate
sr, err := getCardSampleRate(s.DeviceIn)
if err != nil {
return err
}
s.Bitrate = sr
pipeR, pipeW, err := os.Pipe()
if err != nil {
return err
@ -111,6 +121,7 @@ func (s *SPDIFSource) Enable() error {
}
pipeW.Close()
s.endChan <- true
s.processRec = nil
}()
@ -124,7 +135,6 @@ func (s *SPDIFSource) Disable() error {
if s.processPlay != nil && s.processPlay.Process != nil {
s.processPlay.Process.Kill()
}
s.endChan <- true
return nil
}
@ -138,7 +148,7 @@ func getCardSampleRate(cardId string) (sr int64, err error) {
for _, c := range cc {
if len(c.Values) == 1 {
val, err := strconv.Atoi(c.Values[0])
if c.Name == "RX Sample Rate" && err == nil && val > 0 {
if c.Name == "RX Sample Rate" && err == nil {
return int64(val), nil
}
}
@ -150,12 +160,32 @@ func getCardSampleRate(cardId string) (sr int64, err error) {
func (s *SPDIFSource) watchBitrate() {
ticker := time.NewTicker(time.Second)
nbAt0 := 0
loop:
for {
select {
case <-ticker.C:
sr, err := getCardSampleRate(s.DeviceIn)
if err == nil && s.Bitrate/10 != sr/10 {
if err == nil {
if sr == 0 {
nbAt0 += 1
if nbAt0 >= 30 {
log.Printf("[SPDIF] Sample rate is at %d Hz for %d seconds, disabling", sr, nbAt0)
s.Disable()
// Wait process exited
for {
if s.processPlay == nil && s.processRec == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
} else if nbAt0 == 1 || nbAt0%5 == 0 {
log.Printf("[SPDIF] Sample rate is at %d Hz for %d seconds", sr, nbAt0)
}
} else {
nbAt0 = 0
if s.Bitrate/10 != sr/10 {
log.Printf("[SPDIF] Sample rate changes from %d to %d Hz", s.Bitrate, sr)
s.Bitrate = sr
@ -171,6 +201,8 @@ loop:
s.Enable()
}
}
}
case <-s.endChan:
break loop
}