File size: 1,764 Bytes
632bd54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import threading
import time
import pyvirtualcam


class StreamWriter:
    FPS = 30
    VCam = None
    Active = False
    THREAD_LOCK_STREAM = threading.Lock()
    time_last_process = None
    timespan_min = 0.0

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.Close()

    def __init__(self, size, fps):
        self.time_last_process = time.perf_counter()
        self.FPS = fps
        self.timespan_min = 1.0 / fps
        print("Detecting virtual cam devices")
        self.VCam = pyvirtualcam.Camera(
            width=size[0],
            height=size[1],
            fps=fps,
            fmt=pyvirtualcam.PixelFormat.BGR,
            print_fps=False,
        )
        if self.VCam is None:
            print("No virtual camera found!")
            return
        print(f"Using virtual camera: {self.VCam.device}")
        print(f"Using {self.VCam.native_fmt}")
        self.Active = True

    def LimitFrames(self):
        while True:
            current_time = time.perf_counter()
            time_passed = current_time - self.time_last_process
            if time_passed >= self.timespan_min:
                break

    # First version used a queue and threading. Surprisingly this
    # totally simple, blocking version is 10 times faster!
    def WriteToStream(self, frame):
        if self.VCam is None:
            return
        with self.THREAD_LOCK_STREAM:
            self.LimitFrames()
            self.VCam.send(frame)
            self.time_last_process = time.perf_counter()

    def Close(self):
        self.Active = False
        if self.VCam is None:
            self.VCam.close()
            self.VCam = None