mediamtx/internal/rtmp/message/readwriter.go

53 lines
983 B
Go
Raw Normal View History

2022-06-08 18:47:36 +00:00
package message
import (
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter"
)
// ReadWriter is a message reader/writer.
type ReadWriter struct {
r *Reader
w *Writer
}
// NewReadWriter allocates a ReadWriter.
func NewReadWriter(bc *bytecounter.ReadWriter, checkAcknowledge bool) *ReadWriter {
w := NewWriter(bc.Writer, checkAcknowledge)
2022-06-08 18:47:36 +00:00
r := NewReader(bc.Reader, func(count uint32) error {
return w.Write(&MsgAcknowledge{
2022-06-10 22:31:05 +00:00
Value: count,
2022-06-08 18:47:36 +00:00
})
})
return &ReadWriter{
r: r,
w: w,
}
}
// Read reads a message.
func (rw *ReadWriter) Read() (Message, error) {
msg, err := rw.r.Read()
if err != nil {
return nil, err
}
2022-06-10 22:31:05 +00:00
switch tmsg := msg.(type) {
case *MsgAcknowledge:
2022-06-08 18:47:36 +00:00
rw.w.SetAcknowledgeValue(tmsg.Value)
2022-06-10 22:31:05 +00:00
case *MsgUserControlPingRequest:
rw.w.Write(&MsgUserControlPingResponse{
2022-06-10 22:31:05 +00:00
ServerTime: tmsg.ServerTime,
})
2022-06-08 18:47:36 +00:00
}
return msg, nil
}
// Write writes a message.
func (rw *ReadWriter) Write(msg Message) error {
return rw.w.Write(msg)
}