kappanhang/audiostream.go

172 lines
4.5 KiB
Go
Raw Normal View History

2020-10-18 10:33:47 +02:00
package main
2020-10-18 15:40:25 +02:00
import (
"bytes"
"encoding/binary"
2020-10-19 09:45:49 +02:00
"errors"
2020-10-18 15:40:25 +02:00
"time"
)
const audioTimeoutDuration = 3 * time.Second
2020-10-25 18:02:23 +01:00
const audioRxSeqBufLength = 100 * time.Millisecond
2020-10-18 10:33:47 +02:00
type audioStream struct {
2020-10-18 11:01:53 +02:00
common streamCommon
2020-10-18 15:40:25 +02:00
audio audioStruct
deinitNeededChan chan bool
deinitFinishedChan chan bool
2020-10-18 15:40:25 +02:00
timeoutTimer *time.Timer
receivedAudio bool
2020-10-18 15:40:25 +02:00
lastReceivedAudioSeq uint16
2020-10-25 18:29:31 +01:00
rxSeqBuf seqBuf
rxSeqBufEntryChan chan seqBufEntry
2020-10-18 20:09:12 +02:00
audioSendSeq uint16
2020-10-18 10:33:47 +02:00
}
2020-10-20 14:14:05 +02:00
// sendPart1 expects 1364 bytes of PCM data.
func (s *audioStream) sendPart1(pcmData []byte) error {
err := s.common.pkt0.sendTrackedPacket(&s.common,
append([]byte{0x6c, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0x80, 0x00, byte((s.audioSendSeq - 1) >> 8), byte(s.audioSendSeq - 1), 0x00, 0x00, byte(len(pcmData) >> 8), byte(len(pcmData))},
pcmData...))
if err != nil {
return err
}
2020-10-20 14:14:05 +02:00
s.audioSendSeq++
return nil
2020-10-20 14:14:05 +02:00
}
// sendPart2 expects 556 bytes of PCM data.
func (s *audioStream) sendPart2(pcmData []byte) error {
err := s.common.pkt0.sendTrackedPacket(&s.common, append([]byte{0x44, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2020-10-20 14:14:05 +02:00
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0x80, 0x00, byte((s.audioSendSeq - 1) >> 8), byte(s.audioSendSeq - 1), 0x00, 0x00, byte(len(pcmData) >> 8), byte(len(pcmData))},
pcmData...))
if err != nil {
return err
}
2020-10-20 14:14:05 +02:00
s.audioSendSeq++
return nil
2020-10-20 14:14:05 +02:00
}
func (s *audioStream) handleRxSeqBufEntry(e seqBufEntry) {
gotSeq := uint16(e.seq)
if s.receivedAudio {
expectedSeq := s.lastReceivedAudioSeq + 1
if expectedSeq != gotSeq {
var missingPkts int
if gotSeq > expectedSeq {
missingPkts = int(gotSeq) - int(expectedSeq)
} else {
missingPkts = int(gotSeq) + 65536 - int(expectedSeq)
}
log.Error("lost ", missingPkts, " audio packets")
2020-10-18 15:40:25 +02:00
}
}
s.lastReceivedAudioSeq = gotSeq
s.receivedAudio = true
2020-10-18 15:40:25 +02:00
s.audio.play <- e.data
2020-10-18 15:40:25 +02:00
}
func (s *audioStream) handleAudioPacket(r []byte) error {
2020-10-20 14:14:05 +02:00
if s.timeoutTimer != nil {
s.timeoutTimer.Stop()
s.timeoutTimer.Reset(audioTimeoutDuration)
}
gotSeq := binary.LittleEndian.Uint16(r[6:8])
addedToFront, _ := s.rxSeqBuf.add(seqNum(gotSeq), r[24:])
// If the packet is not added to the front of the seqbuf, then it means that it was an answer for a
// retransmit request (or it was an out of order packet which we don't want start a retransmit).
if !addedToFront {
return nil
2020-10-20 14:14:05 +02:00
}
2020-10-25 18:29:31 +01:00
return s.common.requestRetransmitIfNeeded(gotSeq)
2020-10-20 14:14:05 +02:00
}
func (s *audioStream) handleRead(r []byte) error {
2020-10-18 15:40:25 +02:00
if len(r) >= 580 && (bytes.Equal(r[:6], []byte{0x6c, 0x05, 0x00, 0x00, 0x00, 0x00}) || bytes.Equal(r[:6], []byte{0x44, 0x02, 0x00, 0x00, 0x00, 0x00})) {
return s.handleAudioPacket(r)
2020-10-18 15:40:25 +02:00
}
return nil
2020-10-18 13:52:38 +02:00
}
func (s *audioStream) loop() {
for {
select {
case r := <-s.common.readChan:
if err := s.handleRead(r); err != nil {
reportError(err)
}
case <-s.timeoutTimer.C:
2020-10-23 22:52:14 +02:00
reportError(errors.New("audio stream timeout, try rebooting the radio"))
case e := <-s.rxSeqBufEntryChan:
s.handleRxSeqBufEntry(e)
case d := <-s.audio.rec:
if err := s.sendPart1(d[:1364]); err != nil {
reportError(err)
}
if err := s.sendPart2(d[1364:1920]); err != nil {
reportError(err)
}
case <-s.deinitNeededChan:
s.deinitFinishedChan <- true
return
}
}
}
2020-10-18 10:33:47 +02:00
2020-10-26 09:13:30 +01:00
func (s *audioStream) init(devName string) error {
if err := s.common.init("audio", 50003); err != nil {
return err
}
if err := s.audio.init(devName); err != nil {
return err
}
2020-10-18 13:52:38 +02:00
2020-10-26 08:56:30 +01:00
if err := s.common.start(); err != nil {
return err
}
2020-10-18 15:40:25 +02:00
2020-10-20 19:47:25 +02:00
s.common.pkt7.startPeriodicSend(&s.common, 1, false)
// This stream does not use periodic pkt0 idle packets.
2020-10-18 20:09:12 +02:00
s.audioSendSeq = 1
2020-10-26 08:56:30 +01:00
log.Print("stream started")
2020-10-26 09:13:30 +01:00
s.rxSeqBufEntryChan = make(chan seqBufEntry)
s.rxSeqBuf.init(audioRxSeqBufLength, 0xffff, 0, s.rxSeqBufEntryChan)
2020-10-26 08:56:30 +01:00
s.timeoutTimer = time.NewTimer(audioTimeoutDuration)
s.deinitNeededChan = make(chan bool)
s.deinitFinishedChan = make(chan bool)
go s.loop()
return nil
}
func (s *audioStream) deinit() {
if s.deinitNeededChan != nil {
s.deinitNeededChan <- true
<-s.deinitFinishedChan
}
if s.timeoutTimer != nil {
s.timeoutTimer.Stop()
2020-10-18 13:52:38 +02:00
}
s.common.deinit()
s.rxSeqBuf.deinit()
s.audio.deinit()
2020-10-18 10:33:47 +02:00
}