Skip to content

Instantly share code, notes, and snippets.

@carterpeel
Created January 30, 2022 18:54
Show Gist options
  • Save carterpeel/5c72902fe9fd46fca660baeb29217642 to your computer and use it in GitHub Desktop.
Save carterpeel/5c72902fe9fd46fca660baeb29217642 to your computer and use it in GitHub Desktop.
Player
func (p *audioPlayer) Play(session *rtsp.Session) {
log.Logger.WithField("category", "AirPlay Player").Warnf("Starting new session")
p.curSession = session
decoder := codec.GetCodec(session)
go func(dc *codec.Handler) {
var ok bool
for {
select {
/* Is using a pre-allocated buffer for a chan receiver more ideal than:
[buf, ok := <- session.DataChan]? */
case p.recvBuf, ok = <-session.DataChan:
if !ok {
return
}
func() {
/* What's the cost of this? [dc.Decode()] will panic if [p.recvBuf] is malformed, so [recover()] is important here. */
defer func() {
if err := recover(); err != nil {
log.Logger.WithField("category", "AirPlay Player").Warnf("Recovered from panic during playStream: %v\n", err)
}
}()
/* Should this if statement be checking if a bool is true, or should it check if [p.apClient] != nil? */
if p.doEncodedSend {
/* Send the contents of the encoded recv buffer to the remote client */
_, _ = p.apClient.DataConn.Write(p.recvBuf)
}
/* If we need to decode the buffer before sending it to an arbitrary number of outputs, do that here. */
if p.numOutputs > 0 {
p.recvBuf = dc.Decode(p.recvBuf)
codec.NormalizeAudio(p.recvBuf, p.volume)
for i := 0; i < p.numOutputs; i++ {
_, _ = p.outputs[i].Write(p.recvBuf)
}
}
}()
case <-p.quit:
log.Logger.WithField("category", "AirPlay Player").Warnf("Session with peer '%s' closed", session.Description.ConnectData.ConnectionAddress)
return
}
}
}(decoder)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment