mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-06 17:16:55 +08:00
60 lines
1.2 KiB
Plaintext
60 lines
1.2 KiB
Plaintext
package engine
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
type AudioPack struct {
|
|
Timestamp uint
|
|
Payload []byte
|
|
SequenceNumber uint16
|
|
}
|
|
type AudioTrack struct {
|
|
Track[AudioPack]
|
|
SoundFormat byte //4bit
|
|
SoundRate int //2bit
|
|
SoundSize byte //1bit
|
|
Channels byte //1bit
|
|
ASC []byte //audio special configure
|
|
}
|
|
// Push 来自发布者推送的音频
|
|
func (at *AudioTrack) Push(timestamp uint32, payload []byte) {
|
|
payloadLen := len(payload)
|
|
if payloadLen < 4 {
|
|
return
|
|
}
|
|
audio := at.Buffer
|
|
audio.Current.Timestamp = timestamp
|
|
audio.Current.Payload = payload
|
|
at.Track.GetBPS(payloadLen)
|
|
audio.NextW()
|
|
}
|
|
func (at *AudioTrack) Play(ctx context.Context,callback func(AudioPack)) {
|
|
ring := at.Buffer.SubRing(at.Buffer.Index)
|
|
ring.Current.Wait()
|
|
droped:=0
|
|
var action,send func()
|
|
drop := func(){
|
|
if at.Buffer.Index - ring.Index < 10 {
|
|
action = send
|
|
} else {
|
|
droped++
|
|
}
|
|
}
|
|
send = func(){
|
|
callback(ring.Current.T)
|
|
|
|
//s.BufferLength = pIndex - ring.Index
|
|
//s.Delay = s.AVRing.Timestamp - packet.Timestamp
|
|
if at.Buffer.Index - ring.Index > 128 {
|
|
action = drop
|
|
}
|
|
}
|
|
for action =send;;ring.NextR() {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
action()
|
|
}
|
|
}
|
|
} |