From 39d8cf72fe3541a0e68c07526749284ef1afc7fc Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 11 Jul 2021 22:44:50 +0200 Subject: [PATCH] Define a type maybeUint32 and use it in diskwriter. --- diskwriter/diskwriter.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/diskwriter/diskwriter.go b/diskwriter/diskwriter.go index 39ec2ea..1f88904 100644 --- a/diskwriter/diskwriter.go +++ b/diskwriter/diskwriter.go @@ -250,6 +250,22 @@ func openDiskFile(directory, username string) (*os.File, error) { return nil, errors.New("couldn't create file") } +type maybeUint32 uint64 + +const none maybeUint32 = 0 + +func some(value uint32) maybeUint32 { + return maybeUint32(uint64(1 << 32) | uint64(value)) +} + +func valid(m maybeUint32) bool { + return (m & (1 << 32)) != 0 +} + +func value(m maybeUint32) uint32 { + return uint32(m) +} + type diskTrack struct { remote conn.UpTrack conn *diskConn @@ -257,8 +273,7 @@ type diskTrack struct { writer webm.BlockWriteCloser builder *samplebuilder.SampleBuilder - // bit 32 is a boolean indicating that the origin is valid - origin uint64 + origin maybeUint32 kfRequested time.Time lastKf time.Time @@ -546,10 +561,10 @@ func (t *diskTrack) Write(buf []byte) (int, error) { continue } - if t.origin == 0 { - t.origin = uint64(ts) | (1 << 32) + if !valid(t.origin) { + t.origin = some(ts) } - ts -= uint32(t.origin) + ts -= value(t.origin) tm := ts / (t.remote.Codec().ClockRate / 1000) _, err := t.writer.Write(keyframe, int64(tm), sample.Data)