spdif: Initialize even if bitrate is null, stop after 30 seconds of null bitrate

This commit is contained in:
nemunaire 2024-11-23 10:22:58 +01:00
parent 84d594d695
commit b22259c423

View File

@ -72,6 +72,13 @@ func (s *SPDIFSource) Enable() error {
s.processPlay.Process.Kill() s.processPlay.Process.Kill()
} }
// Update bitrate
sr, err := getCardSampleRate(s.DeviceIn)
if err != nil {
return err
}
s.Bitrate = sr
pipeR, pipeW, err := os.Pipe() pipeR, pipeW, err := os.Pipe()
if err != nil { if err != nil {
return err return err
@ -138,7 +145,7 @@ func getCardSampleRate(cardId string) (sr int64, err error) {
for _, c := range cc { for _, c := range cc {
if len(c.Values) == 1 { if len(c.Values) == 1 {
val, err := strconv.Atoi(c.Values[0]) 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 return int64(val), nil
} }
} }
@ -150,12 +157,32 @@ func getCardSampleRate(cardId string) (sr int64, err error) {
func (s *SPDIFSource) watchBitrate() { func (s *SPDIFSource) watchBitrate() {
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
nbAt0 := 0
loop: loop:
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
sr, err := getCardSampleRate(s.DeviceIn) 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) log.Printf("[SPDIF] Sample rate changes from %d to %d Hz", s.Bitrate, sr)
s.Bitrate = sr s.Bitrate = sr
@ -171,6 +198,8 @@ loop:
s.Enable() s.Enable()
} }
}
}
case <-s.endChan: case <-s.endChan:
break loop break loop
} }