mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-23 23:22:26 +00:00
53 lines
983 B
Go
53 lines
983 B
Go
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)
|
|
|
|
r := NewReader(bc.Reader, func(count uint32) error {
|
|
return w.Write(&MsgAcknowledge{
|
|
Value: count,
|
|
})
|
|
})
|
|
|
|
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
|
|
}
|
|
|
|
switch tmsg := msg.(type) {
|
|
case *MsgAcknowledge:
|
|
rw.w.SetAcknowledgeValue(tmsg.Value)
|
|
|
|
case *MsgUserControlPingRequest:
|
|
rw.w.Write(&MsgUserControlPingResponse{
|
|
ServerTime: tmsg.ServerTime,
|
|
})
|
|
}
|
|
|
|
return msg, nil
|
|
}
|
|
|
|
// Write writes a message.
|
|
func (rw *ReadWriter) Write(msg Message) error {
|
|
return rw.w.Write(msg)
|
|
}
|