mediamtx/internal/protocols/rtmp/bytecounter/writer.go

37 lines
616 B
Go
Raw Normal View History

2022-06-08 18:47:36 +00:00
package bytecounter
import (
"io"
"sync/atomic"
2022-06-08 18:47:36 +00:00
)
// Writer allows to count written bytes.
type Writer struct {
w io.Writer
count uint64
2022-06-08 18:47:36 +00:00
}
// NewWriter allocates a Writer.
func NewWriter(w io.Writer) *Writer {
return &Writer{
w: w,
}
}
// Write implements io.Writer.
func (w *Writer) Write(p []byte) (int, error) {
n, err := w.w.Write(p)
atomic.AddUint64(&w.count, uint64(n))
2022-06-08 18:47:36 +00:00
return n, err
}
// Count returns sent bytes.
func (w *Writer) Count() uint64 {
return atomic.LoadUint64(&w.count)
2022-06-08 18:47:36 +00:00
}
2022-07-16 10:42:48 +00:00
// SetCount sets sent bytes.
func (w *Writer) SetCount(v uint64) {
atomic.StoreUint64(&w.count, v)
2022-07-16 10:42:48 +00:00
}