untangledco / streaming

Media streaming and broadcast systems in Go
https://twitch.tv/untangledco
ISC License
70 stars 7 forks source link

cmcd: differentiating zero and not set #27

Open ollytom opened 1 month ago

ollytom commented 1 month ago

For example, in Request, we have BufLength:

type Request struct {
    BufLength time.Duration
    ...
}

What if the buffer length was never sent in CMCD info? How can we tell if a player actually requested cotent of zero length? I'm not sure if this is possible or not, but it was a good question brought up by someone on video-dev.slack.com:

:waipu: 1 month ago I essentially wrote the same CMCD parser two years ago. However, I was also interested in knowing if a variable was absent. For example, in your code for Request.BufLength, you don't know if BufLength was zero or wasn't set (because the player implementation doesn't support it). :waipu: 1 month ago I solved it this way:

package nan

import "time"

type storable interface { int | float64 | string | time.Duration | time.Time | bool }

// Null is a generic struct to avoid using of pointers to hold the information about "is the value set" type Null[T storable] struct { data T defined bool }

// Defined returns true, if the value was set and false if it's nan func (n Null[T]) Defined() bool { return n.defined }

// Value returns the value of Null func (n Null[T]) Value() (v T) { return n.data }

// Set initializes a new Null with type of storable func Set[T storable](v T) Null[T] { return Null[T]{ data: v, defined: true, } }

type Request struct { // BufferLength is the buffer length associated with the media object being requested. BufferLength nan.Null[time.Duration] }

I don't want to re-invent NULL or NaN in Go, so twitch.tv/swills6 suggested using a bool to store that kind of info. For example:

type Request struct {
    BufLengthSet bool
    BufLength time.Duration
    ...
}

This is what we do in other packages in this module. We could also store BufLength as a *time.Duration but that doesn't feel right to introduce nil checking, panics etc. when it's just a int64 under the hood.

ollytom commented 1 month ago

swills6 suggests something more specific rather than wording "Set", maybe... type Request struct { BufLengthRequested bool BufLength time.Duration ... }

Asked the original question asker if they ever ran into any players/devices which sent information with something like "I made a request for content of 0 milliseconds". Probably going to close this, but will see if we get a response, maybe some weird player bugs that we could fix... for you know... geek cred...?

ollytom commented 1 month ago

Thread containing discussion on video-dev chat: https://video-dev.slack.com/archives/C5PNWM562/p1714536663841469