kappanhang/audiostream.go

211 lines
5.8 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"
"github.com/nonoo/kappanhang/log"
)
const audioTimeoutDuration = 3 * time.Second
2020-10-20 14:14:05 +02:00
const rxSeqBufLength = 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-20 14:14:05 +02: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.send(append([]byte{0x6c, 0x05, 0x00, 0x00, 0x00, 0x00, byte(s.audioSendSeq), byte(s.audioSendSeq >> 8),
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
}
// sendPart2 expects 556 bytes of PCM data.
func (s *audioStream) sendPart2(pcmData []byte) error {
err := s.common.send(append([]byte{0x44, 0x02, 0x00, 0x00, 0x00, 0x00, byte(s.audioSendSeq), byte(s.audioSendSeq >> 8),
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) sendRetransmitRequest(seqNum uint16) error {
2020-10-20 11:44:07 +02:00
p := []byte{0x10, 0x00, 0x00, 0x00, 0x01, 0x00, byte(seqNum), byte(seqNum >> 8),
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)}
if err := s.common.send(p); err != nil {
return err
}
if err := s.common.send(p); err != nil {
return err
}
return nil
2020-10-20 11:44:07 +02:00
}
type seqNumRange [2]uint16
func (s *audioStream) sendRetransmitRequestForRanges(seqNumRanges []seqNumRange) error {
2020-10-20 11:44:07 +02:00
seqNumBytes := make([]byte, len(seqNumRanges)*4)
for i := 0; i < len(seqNumRanges); i++ {
seqNumBytes[i*2] = byte(seqNumRanges[i][0])
seqNumBytes[i*2+1] = byte(seqNumRanges[i][0] >> 8)
seqNumBytes[i*2+2] = byte(seqNumRanges[i][1])
seqNumBytes[i*2+3] = byte(seqNumRanges[i][1] >> 8)
}
p := append([]byte{0x18, 0x00, 0x00, 0x00, 0x01, 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)},
seqNumBytes...)
if err := s.common.send(p); err != nil {
return err
}
if err := s.common.send(p); err != nil {
return err
}
return nil
2020-10-20 11:44:07 +02:00
}
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
}
2020-10-20 14:14:05 +02:00
func (s *audioStream) handleAudioPacket(r []byte) {
if s.timeoutTimer != nil {
s.timeoutTimer.Stop()
s.timeoutTimer.Reset(audioTimeoutDuration)
}
gotSeq := binary.LittleEndian.Uint16(r[6:8])
err := s.rxSeqBuf.add(seqNum(gotSeq), r[24:])
if err != nil {
log.Error(err)
}
}
2020-10-18 13:52:38 +02:00
func (s *audioStream) handleRead(r []byte) {
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})) {
s.handleAudioPacket(r)
}
2020-10-18 13:52:38 +02:00
}
func (s *audioStream) loop() {
for {
select {
case r := <-s.common.readChan:
s.handleRead(r)
case <-s.timeoutTimer.C:
reportError(errors.New("audio stream timeout"))
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
func (s *audioStream) start(devName string) error {
if err := s.audio.init(devName); err != nil {
return err
}
2020-10-18 13:52:38 +02:00
if err := s.common.sendPkt3(); err != nil {
return err
}
if err := s.common.waitForPkt4Answer(); err != nil {
return err
}
if err := s.common.sendPkt6(); err != nil {
return err
}
if err := s.common.waitForPkt6Answer(); err != nil {
return err
}
log.Print("stream started")
2020-10-18 13:52:38 +02:00
2020-10-18 15:40:25 +02:00
s.timeoutTimer = time.NewTimer(audioTimeoutDuration)
2020-10-20 19:47:25 +02:00
s.common.pkt7.startPeriodicSend(&s.common, 1, false)
2020-10-18 13:52:38 +02:00
2020-10-18 20:09:12 +02:00
s.audioSendSeq = 1
s.deinitNeededChan = make(chan bool)
s.deinitFinishedChan = make(chan bool)
go s.loop()
return nil
}
func (s *audioStream) init() error {
if err := s.common.init("audio", 50003); err != nil {
return err
}
s.rxSeqBufEntryChan = make(chan seqBufEntry)
s.rxSeqBuf.init(rxSeqBufLength, 0xffff, 0, s.rxSeqBufEntryChan)
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
}