2018-04-11 07:35:00 +00:00
|
|
|
package sine
|
2018-04-10 15:51:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.icedream.tech/icedream/uplink/app/channels"
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
|
|
"github.com/viert/lame"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pluginInstance struct {
|
2018-04-11 15:55:15 +00:00
|
|
|
channelManager *channels.ChannelManager
|
2018-04-10 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (instance *pluginInstance) SetChannelManager(channelManager *channels.ChannelManager) {
|
2018-04-11 15:55:15 +00:00
|
|
|
instance.channelManager = channelManager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instance *pluginInstance) Init() {
|
|
|
|
channelManager := instance.channelManager
|
2018-04-10 15:51:03 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-04-11 15:55:15 +00:00
|
|
|
time.Sleep(2 * time.Second) // give burst cache a chance to realize
|
|
|
|
|
|
|
|
c, err := channelManager.Open("sine")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR: sine channel could not be opened:", err)
|
|
|
|
log.Println("Skipping sine channel creation")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
lastTime := time.Now()
|
|
|
|
for {
|
|
|
|
lastTime = lastTime.Add(time.Second)
|
|
|
|
time.Sleep(time.Until(lastTime))
|
|
|
|
|
|
|
|
c.SetMetadata(map[string]string{
|
|
|
|
"StreamTitle": "beep - time: " + time.Now().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
outputStream := c.AddOutputStream("mp3")
|
|
|
|
defer outputStream.Close()
|
|
|
|
|
|
|
|
outputContainer := c.AddOutputContainer("mp3")
|
|
|
|
defer outputContainer.Close()
|
|
|
|
|
|
|
|
w := io.MultiWriter(
|
|
|
|
outputContainer,
|
|
|
|
outputStream,
|
|
|
|
)
|
|
|
|
|
|
|
|
wr := lame.NewWriter(w)
|
|
|
|
wr.Encoder.SetBitrate(192)
|
|
|
|
wr.Encoder.SetQuality(1)
|
|
|
|
wr.Encoder.SetInSamplerate(44100)
|
|
|
|
wr.Encoder.SetNumChannels(2)
|
|
|
|
wr.Encoder.InitParams()
|
|
|
|
|
2018-04-10 15:51:03 +00:00
|
|
|
log.Println("Sine stream goroutine started")
|
|
|
|
|
|
|
|
sine := new(SineStream)
|
|
|
|
sine.Samplerate = 44100
|
|
|
|
sine.Frequency = 990
|
|
|
|
sine.Beep = true
|
|
|
|
sine.Timestamp = time.Now()
|
|
|
|
|
|
|
|
log.Println("Will now broadcast sine stream")
|
|
|
|
n, err := io.Copy(wr, sine)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Sine stream copy failed:", err)
|
|
|
|
}
|
|
|
|
log.Println("Sine stream finished, written", humanize.Bytes(uint64(n)), "bytes")
|
|
|
|
}()
|
|
|
|
}
|