letmaik / pyvirtualcam

🎥 Send frames to a virtual camera from Python
GNU General Public License v2.0
455 stars 49 forks source link

add macOS support #16

Closed JayFoxRox closed 3 years ago

JayFoxRox commented 3 years ago

Closes #2

This is pretty hacky, as I only needed it for a little project. Very little effort and testing went into this. I'll add some review comments about things which still need work.

A bunch of code was stolen from https://github.com/obsproject/obs-studio/tree/516ed4458dbc74b27b425533789d4e9dd95b923b/plugins/mac-virtualcam Ideally we'd probably have a custom DAL plugin - see https://github.com/johnboiles/coremediaio-dal-minimal-example

I also took the UYVY code from stackoverflow or something, but adapted it. I did not check which colorspace is used or expected.

letmaik commented 3 years ago

Thanks for the contribution! I think it's pretty good, hackyness is on par with the Windows variant :) This package is experimental, so that's not a problem. As long as folks can use it for some funny demos or their research prototypes it's good enough. It sounds like you don't want to invest more time into cleanup, right? I don't own a mac, which complicates it a little but I may be able to rely on GitHub Actions to validate any further changes.

letmaik commented 3 years ago

I added macOS CI, seems like there are some compile issues.

JayFoxRox commented 3 years ago

It sounds like you don't want to invest more time into cleanup, right? I don't own a mac, which complicates it a little but I may be able to rely on GitHub Actions to validate any further changes.

I'd like to see this included in master and am willing to fix the worst bugs I've created. So a bit of cleanup is fine, but I don't want to spend more than a couple of minutes on it (especially due to #19).

CI sounds like a good idea. Make sure it also catches issues like this: https://github.com/letmaik/pyvirtualcam/pull/16#discussion_r584204249.

letmaik commented 3 years ago

CI now passes except for the test that checks whether the target fps are achieved. It seems that it fluctuates a lot judging from the output of the test_sample_simple:

12.0 fps 13.4 fps 11.4 fps 25.3 fps

Compared to Windows:

20.0 fps 20.1 fps 19.9 fps 20.0 fps

Do you have any idea how bad this really was when you tested it? Did it ever cause instability?

JayFoxRox commented 3 years ago

Do you have any idea how bad this really was when you tested it?

I only tested this for about a minute or two.

Tests were done with simple.py and https://github.com/JayFoxRox/Fujifilm-FinePix-Driver on an older MacBook Pro. It's hard to tell framerate from those test cases and I don't have any logs of the FPS numbers at hand, but could get some if really necessary.

The FinePix camera image felt slow, but it's a 320x240 camera from 2002 which sends JPEG frame and is polled at 35ms per frame with naive decoding by my driver.

Did it ever cause instability?

I also had one case where my FinePix camera driver crashed, but I believe it was due to the USB code or a bad USB cable; not pyvirtualcam.

except for the test that checks whether the target fps are achieved

I expect slowness, especially at higher resolutions.

Things which should help:

However, I don't want to spend time on any of that at the moment. For now, this is "good enough" for me, and someone can iterate later.

JayFoxRox commented 3 years ago

I'm on my MacBook Pro (5-inch, 2017) / 2,8 GHz Quad-Core Intel Core i7 now.

With Zoom preferences opened to see the image:

$ python3 samples/simple.py
21.1 fps
20.1 fps
20.4 fps
20.0 fps
19.9 fps
19.4 fps
20.2 fps
20.4 fps
$ python3 samples/animated_gradient.py
Virtual cam started (1280x720 @ 20fps)
21.8 fps
22.1 fps
20.1 fps
20.5 fps
20.7 fps
20.5 fps
20.5 fps
20.3 fps
$ pytest -v -s ../test
=========================================================================================================== test session starts ============================================================================================================
platform darwin -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- /usr/local/opt/python@3.9/bin/python3.9
cachedir: .pytest_cache
rootdir: <...>/pyvirtualcam
collected 5 items

../test/test_camera.py::test_consecutive PASSED
../test/test_camera.py::test_parallel virtual camera output already started
PASSED
../test/test_camera.py::test_sleep_until_next_frame PASSED
../test/test_samples.py::test_sample_simple 20.1 fps
19.8 fps
19.8 fps
20.1 fps
PASSED
../test/test_util.py::test_fps_counter PASSED

============================================================================================================ 5 passed in 13.28s ============================================================================================================

So errors likely stem from CI performance fluctuation.

letmaik commented 3 years ago

I'm on my MacBook Pro (5-inch, 2017) / 2,8 GHz Quad-Core Intel Core i7 now. ... ../test/test_camera.py::test_sleep_until_next_frame PASSED ... So errors likely stem from CI performance fluctuation.

Great, I disabled that test in CI for macOS, all green now.