syphon.client

  1from abc import ABC, abstractmethod
  2from typing import Optional, Any
  3
  4import Metal
  5import objc
  6
  7from syphon.server_directory import SyphonServerDescription
  8from syphon.utils import opengl
  9
 10
 11class BaseSyphonClient(ABC):
 12    """
 13    Abstract base class for Syphon clients.
 14
 15    Attributes:
 16    - description (SyphonServerDescription): The description of the Syphon server.
 17    """
 18
 19    def __init__(self, description: SyphonServerDescription):
 20        """
 21        Initialize a BaseSyphonClient.
 22
 23        Parameters:
 24        - description (SyphonServerDescription): The description of the Syphon server.
 25        """
 26        # todo: implement new frame handler callback support
 27        self._description = description
 28
 29    @property
 30    @abstractmethod
 31    def is_valid(self) -> bool:
 32        """
 33        Check if the Syphon client is valid.
 34
 35        Returns:
 36        - bool: True if the client is valid, False otherwise.
 37        """
 38        pass
 39
 40    @property
 41    @abstractmethod
 42    def has_new_frame(self) -> bool:
 43        """
 44        Check if the Syphon client has a new frame.
 45
 46        Returns:
 47        - bool: True if there is a new frame, False otherwise.
 48        """
 49        pass
 50
 51    @abstractmethod
 52    def stop(self):
 53        """
 54        Stop the Syphon client.
 55        """
 56        pass
 57
 58
 59class SyphonMetalClient(BaseSyphonClient):
 60    """
 61    Syphon client for Metal-based rendering.
 62
 63    Attributes:
 64    - description (SyphonServerDescription): The description of the Syphon server.
 65    - device (Any): The Metal device.
 66    - context (Any): The Syphon-Metal context.
 67    """
 68
 69    def __init__(self, description: SyphonServerDescription, device: Optional[Any] = None):
 70        """
 71        Initialize a SyphonMetalClient.
 72
 73        Parameters:
 74        - description (SyphonServerDescription): The description of the Syphon server.
 75        - device (Any, optional): The Metal device. If None, the default system device will be used.
 76        """
 77        super().__init__(description)
 78
 79        self.device = device
 80
 81        # setup device
 82        if self.device is None:
 83            self.device = Metal.MTLCreateSystemDefaultDevice()
 84
 85        # setup syphon-metal context
 86        SyphonMetalClientObjC = objc.lookUpClass("SyphonMetalClient")
 87
 88        self.context = (
 89            SyphonMetalClientObjC
 90            .alloc()
 91            .initWithServerDescription_device_options_newFrameHandler_(
 92                description.raw,
 93                self.device,
 94                None,
 95                None)
 96        )
 97
 98    @property
 99    def is_valid(self) -> bool:
100        """
101        Check if the SyphonMetalClient is valid.
102
103        Returns:
104        - bool: True if the client is valid, False otherwise.
105        """
106        return self.context.isValid()
107
108    @property
109    def has_new_frame(self) -> bool:
110        """
111        Check if the SyphonMetalClient has a new frame.
112
113        Returns:
114        - bool: True if there is a new frame, False otherwise.
115        """
116        return self.context.hasNewFrame()
117
118    @property
119    def new_frame_image(self) -> Any:
120        """
121        Get the new frame image.
122
123        Returns:
124        - Any: The new frame image.
125        """
126        return self.context.newFrameImage()
127
128    def stop(self):
129        """
130        Stop the SyphonMetalClient.
131        """
132        self.context.stop()
133
134
135class SyphonOpenGLClient(BaseSyphonClient):
136    """
137    Syphon client for OpenGL-based rendering.
138
139    Attributes:
140    - description (SyphonServerDescription): The description of the Syphon server.
141    - cgl_context_obj (Any): The CGL context object.
142    - context (Any): The Syphon-OpenGL context.
143    """
144
145    def __init__(self, description: SyphonServerDescription, cgl_context_obj: Optional[Any] = None):
146        """
147        Initialize a SyphonOpenGLClient.
148
149        Parameters:
150        - description (SyphonServerDescription): The description of the Syphon server.
151        - cgl_context_obj (Any, optional): The CGL context object. If None, the current context will be used.
152        """
153        super().__init__(description)
154
155        # store CGL context object
156        self.cgl_context_obj = opengl.get_current_cgl_context_obj() if cgl_context_obj is None else cgl_context_obj
157
158        # create syphon gl client
159        SyphonOpenGLClientObjC = objc.lookUpClass("SyphonOpenGLClient")
160        self.context = (
161            SyphonOpenGLClientObjC
162            .alloc()
163            .initWithServerDescription_context_options_newFrameHandler_(
164                description.raw,
165                self.cgl_context_obj,
166                None,
167                None)
168        )
169
170    @property
171    def is_valid(self) -> bool:
172        """
173        Check if the SyphonOpenGLClient is valid.
174
175        Returns:
176        - bool: True if the client is valid, False otherwise.
177        """
178        return self.context.isValid()
179
180    @property
181    def has_new_frame(self) -> bool:
182        """
183        Check if the SyphonOpenGLClient has a new frame.
184
185        Returns:
186        - bool: True if there is a new frame, False otherwise.
187        """
188        return self.context.hasNewFrame()
189
190    @property
191    def new_frame_image(self) -> Any:
192        """
193        Get the new frame image.
194
195        Returns:
196        - Any: The new frame image.
197        """
198        return self.context.newFrameImage()
199
200    def stop(self):
201        """
202        Stop the SyphonOpenGLClient.
203        """
204        self.context.stop()
class BaseSyphonClient(abc.ABC):
12class BaseSyphonClient(ABC):
13    """
14    Abstract base class for Syphon clients.
15
16    Attributes:
17    - description (SyphonServerDescription): The description of the Syphon server.
18    """
19
20    def __init__(self, description: SyphonServerDescription):
21        """
22        Initialize a BaseSyphonClient.
23
24        Parameters:
25        - description (SyphonServerDescription): The description of the Syphon server.
26        """
27        # todo: implement new frame handler callback support
28        self._description = description
29
30    @property
31    @abstractmethod
32    def is_valid(self) -> bool:
33        """
34        Check if the Syphon client is valid.
35
36        Returns:
37        - bool: True if the client is valid, False otherwise.
38        """
39        pass
40
41    @property
42    @abstractmethod
43    def has_new_frame(self) -> bool:
44        """
45        Check if the Syphon client has a new frame.
46
47        Returns:
48        - bool: True if there is a new frame, False otherwise.
49        """
50        pass
51
52    @abstractmethod
53    def stop(self):
54        """
55        Stop the Syphon client.
56        """
57        pass

Abstract base class for Syphon clients.

Attributes:

  • description (SyphonServerDescription): The description of the Syphon server.
BaseSyphonClient(description: SyphonServerDescription)
20    def __init__(self, description: SyphonServerDescription):
21        """
22        Initialize a BaseSyphonClient.
23
24        Parameters:
25        - description (SyphonServerDescription): The description of the Syphon server.
26        """
27        # todo: implement new frame handler callback support
28        self._description = description

Initialize a BaseSyphonClient.

Parameters:

  • description (SyphonServerDescription): The description of the Syphon server.
is_valid: bool

Check if the Syphon client is valid.

Returns:

  • bool: True if the client is valid, False otherwise.
has_new_frame: bool

Check if the Syphon client has a new frame.

Returns:

  • bool: True if there is a new frame, False otherwise.
@abstractmethod
def stop(self):
52    @abstractmethod
53    def stop(self):
54        """
55        Stop the Syphon client.
56        """
57        pass

Stop the Syphon client.

class SyphonMetalClient(BaseSyphonClient):
 60class SyphonMetalClient(BaseSyphonClient):
 61    """
 62    Syphon client for Metal-based rendering.
 63
 64    Attributes:
 65    - description (SyphonServerDescription): The description of the Syphon server.
 66    - device (Any): The Metal device.
 67    - context (Any): The Syphon-Metal context.
 68    """
 69
 70    def __init__(self, description: SyphonServerDescription, device: Optional[Any] = None):
 71        """
 72        Initialize a SyphonMetalClient.
 73
 74        Parameters:
 75        - description (SyphonServerDescription): The description of the Syphon server.
 76        - device (Any, optional): The Metal device. If None, the default system device will be used.
 77        """
 78        super().__init__(description)
 79
 80        self.device = device
 81
 82        # setup device
 83        if self.device is None:
 84            self.device = Metal.MTLCreateSystemDefaultDevice()
 85
 86        # setup syphon-metal context
 87        SyphonMetalClientObjC = objc.lookUpClass("SyphonMetalClient")
 88
 89        self.context = (
 90            SyphonMetalClientObjC
 91            .alloc()
 92            .initWithServerDescription_device_options_newFrameHandler_(
 93                description.raw,
 94                self.device,
 95                None,
 96                None)
 97        )
 98
 99    @property
100    def is_valid(self) -> bool:
101        """
102        Check if the SyphonMetalClient is valid.
103
104        Returns:
105        - bool: True if the client is valid, False otherwise.
106        """
107        return self.context.isValid()
108
109    @property
110    def has_new_frame(self) -> bool:
111        """
112        Check if the SyphonMetalClient has a new frame.
113
114        Returns:
115        - bool: True if there is a new frame, False otherwise.
116        """
117        return self.context.hasNewFrame()
118
119    @property
120    def new_frame_image(self) -> Any:
121        """
122        Get the new frame image.
123
124        Returns:
125        - Any: The new frame image.
126        """
127        return self.context.newFrameImage()
128
129    def stop(self):
130        """
131        Stop the SyphonMetalClient.
132        """
133        self.context.stop()

Syphon client for Metal-based rendering.

Attributes:

  • description (SyphonServerDescription): The description of the Syphon server.
  • device (Any): The Metal device.
  • context (Any): The Syphon-Metal context.
SyphonMetalClient( description: SyphonServerDescription, device: Optional[Any] = None)
70    def __init__(self, description: SyphonServerDescription, device: Optional[Any] = None):
71        """
72        Initialize a SyphonMetalClient.
73
74        Parameters:
75        - description (SyphonServerDescription): The description of the Syphon server.
76        - device (Any, optional): The Metal device. If None, the default system device will be used.
77        """
78        super().__init__(description)
79
80        self.device = device
81
82        # setup device
83        if self.device is None:
84            self.device = Metal.MTLCreateSystemDefaultDevice()
85
86        # setup syphon-metal context
87        SyphonMetalClientObjC = objc.lookUpClass("SyphonMetalClient")
88
89        self.context = (
90            SyphonMetalClientObjC
91            .alloc()
92            .initWithServerDescription_device_options_newFrameHandler_(
93                description.raw,
94                self.device,
95                None,
96                None)
97        )

Initialize a SyphonMetalClient.

Parameters:

  • description (SyphonServerDescription): The description of the Syphon server.
  • device (Any, optional): The Metal device. If None, the default system device will be used.
device
context
is_valid: bool

Check if the SyphonMetalClient is valid.

Returns:

  • bool: True if the client is valid, False otherwise.
has_new_frame: bool

Check if the SyphonMetalClient has a new frame.

Returns:

  • bool: True if there is a new frame, False otherwise.
new_frame_image: Any

Get the new frame image.

Returns:

  • Any: The new frame image.
def stop(self):
129    def stop(self):
130        """
131        Stop the SyphonMetalClient.
132        """
133        self.context.stop()

Stop the SyphonMetalClient.

class SyphonOpenGLClient(BaseSyphonClient):
136class SyphonOpenGLClient(BaseSyphonClient):
137    """
138    Syphon client for OpenGL-based rendering.
139
140    Attributes:
141    - description (SyphonServerDescription): The description of the Syphon server.
142    - cgl_context_obj (Any): The CGL context object.
143    - context (Any): The Syphon-OpenGL context.
144    """
145
146    def __init__(self, description: SyphonServerDescription, cgl_context_obj: Optional[Any] = None):
147        """
148        Initialize a SyphonOpenGLClient.
149
150        Parameters:
151        - description (SyphonServerDescription): The description of the Syphon server.
152        - cgl_context_obj (Any, optional): The CGL context object. If None, the current context will be used.
153        """
154        super().__init__(description)
155
156        # store CGL context object
157        self.cgl_context_obj = opengl.get_current_cgl_context_obj() if cgl_context_obj is None else cgl_context_obj
158
159        # create syphon gl client
160        SyphonOpenGLClientObjC = objc.lookUpClass("SyphonOpenGLClient")
161        self.context = (
162            SyphonOpenGLClientObjC
163            .alloc()
164            .initWithServerDescription_context_options_newFrameHandler_(
165                description.raw,
166                self.cgl_context_obj,
167                None,
168                None)
169        )
170
171    @property
172    def is_valid(self) -> bool:
173        """
174        Check if the SyphonOpenGLClient is valid.
175
176        Returns:
177        - bool: True if the client is valid, False otherwise.
178        """
179        return self.context.isValid()
180
181    @property
182    def has_new_frame(self) -> bool:
183        """
184        Check if the SyphonOpenGLClient has a new frame.
185
186        Returns:
187        - bool: True if there is a new frame, False otherwise.
188        """
189        return self.context.hasNewFrame()
190
191    @property
192    def new_frame_image(self) -> Any:
193        """
194        Get the new frame image.
195
196        Returns:
197        - Any: The new frame image.
198        """
199        return self.context.newFrameImage()
200
201    def stop(self):
202        """
203        Stop the SyphonOpenGLClient.
204        """
205        self.context.stop()

Syphon client for OpenGL-based rendering.

Attributes:

  • description (SyphonServerDescription): The description of the Syphon server.
  • cgl_context_obj (Any): The CGL context object.
  • context (Any): The Syphon-OpenGL context.
SyphonOpenGLClient( description: SyphonServerDescription, cgl_context_obj: Optional[Any] = None)
146    def __init__(self, description: SyphonServerDescription, cgl_context_obj: Optional[Any] = None):
147        """
148        Initialize a SyphonOpenGLClient.
149
150        Parameters:
151        - description (SyphonServerDescription): The description of the Syphon server.
152        - cgl_context_obj (Any, optional): The CGL context object. If None, the current context will be used.
153        """
154        super().__init__(description)
155
156        # store CGL context object
157        self.cgl_context_obj = opengl.get_current_cgl_context_obj() if cgl_context_obj is None else cgl_context_obj
158
159        # create syphon gl client
160        SyphonOpenGLClientObjC = objc.lookUpClass("SyphonOpenGLClient")
161        self.context = (
162            SyphonOpenGLClientObjC
163            .alloc()
164            .initWithServerDescription_context_options_newFrameHandler_(
165                description.raw,
166                self.cgl_context_obj,
167                None,
168                None)
169        )

Initialize a SyphonOpenGLClient.

Parameters:

  • description (SyphonServerDescription): The description of the Syphon server.
  • cgl_context_obj (Any, optional): The CGL context object. If None, the current context will be used.
cgl_context_obj
context
is_valid: bool

Check if the SyphonOpenGLClient is valid.

Returns:

  • bool: True if the client is valid, False otherwise.
has_new_frame: bool

Check if the SyphonOpenGLClient has a new frame.

Returns:

  • bool: True if there is a new frame, False otherwise.
new_frame_image: Any

Get the new frame image.

Returns:

  • Any: The new frame image.
def stop(self):
201    def stop(self):
202        """
203        Stop the SyphonOpenGLClient.
204        """
205        self.context.stop()

Stop the SyphonOpenGLClient.