openairplay / airplay2-receiver

AirPlay 2 Receiver - Python implementation
2.12k stars 131 forks source link

Prep work for PTP #49

Open systemcrash opened 2 years ago

systemcrash commented 2 years ago

@glmnet @LewdNeko

First question: what happens on your systems when you run PTP on port 319+320?

Do you need root privileges/run sudo? Or does it work OK to say, bind to '0.0.0.0' / '::'

I've not found a way that airplay sends PTP to any other port. I think the ports are baked in.

systemcrash commented 2 years ago

It seems that Python 3.7 might be necessary for PTP. Example:

    def time_monotonic_ns(self):
        # From PEP 564: Linux 1MHz. Win 10MHz. my macOS 88ns, ~11.3MHz
        # The following if/else is nearly twice as slow as a direct call...
        if sys.hexversion >= 0x3070000:
            #Needs Python >= 3.7
            return time.monotonic_ns()
        else:
            return int(time.monotonic() * 1e9)

We could structure this as a try: call, and try time.monotonicc_ns() first. Then fall back to older versions. Python 3.7 seems to guarantee much more accurate ns timestamping than int(time.monotonic() * 1e9).

glmnet commented 2 years ago

@glmnet @LewdNeko

First question: what happens on your systems when you run PTP on port 319+320?

Do you need root privileges/run sudo? Or does it work OK to say, bind to '0.0.0.0' / '::'

I've not found a way that airplay sends PTP to any other port. I think the ports are baked in.

ahh sorry not replying before. I'm testing this on Windows only, and have not run into any issue regarding port permissions

glmnet commented 2 years ago

It seems that Python 3.7 might be necessary for PTP. Example:

I would just drop support for Python < 3.7. E.g. 3.6 is quite old and not receiving security security updates in just a few months https://en.wikipedia.org/wiki/History_of_Python#Table_of_versions

systemcrash commented 2 years ago

While I agree that's true, it's still a common baseline for many OSes that people might be on. 3.6 is still solid. I guess it's a balance of convenience vs speed (if we use the example above). Python doesn't have an

ifdef that I know of.

I didn't perf test a try:, only the if/else.

systemcrash commented 2 years ago

A quick search suggests this is a way around the problem: https://stackoverflow.com/a/12524906

TheSpookyCat commented 2 years ago

I'm for dropping Python 3.6 support :)

systemcrash commented 2 years ago

I ended up doing this in the head of the PTP module:

if sys.hexversion >= 0x3070000:
    # Needs Python >= 3.7 - define only once at startup.
    def time_monotonic_ns():
        # TODO: calibrate a timing loop at startup to precalculate avg call time
        # From PEP 564: Linux 1MHz. Win 10MHz. my macOS 88ns, ~11.3MHz
        return time.monotonic_ns()
else:
    def time_monotonic_ns():
        return int(time.monotonic() * 1e9)

This means we can have both. Win.