summaryrefslogtreecommitdiff
path: root/sphinxdoc/player.rst
blob: ba9115e9da5443d24414c4ef032df289062a4c69 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
Player & Canvas
===============

.. automodule:: libavg.avg
    :no-members:

    .. inheritance-diagram:: OffscreenCanvas
        :parts: 1

    .. inheritance-diagram:: Player
        :parts: 1

    This section describes the classes that provide a framework for rendering. The 
    :py:class:`Player` class is an interface to the avg renderer. The 
    :py:class:`Canvas` class and its descendant :py:class:`OffscreenCanvas` provide 
    areas to draw on. 
    
    .. autoclass:: Canvas

        A Canvas is a tree of nodes. It is the place where a scenegraph is displayed. In
        a libavg session, there is one main canvas that corresponds to the screen (which
        is of class :py:class:`Canvas`) and zero or more canvases that are rendered 
        offscreen (which are of class :py:class:`OffscreenCanvas`). 

        .. py:method:: getElementByID(id) -> Node

            Returns the element in the canvas's tree that has the :py:attr:`id`
            given.
        
        .. py:method:: screenshot() -> Bitmap

            Returns the image the canvas has last rendered as :py:class:`Bitmap`. For
            the main canvas, this is a real screenshot. For offscreen canvases, this 
            is the image rendered offscreen.
        
        .. py:method:: getRootNode() -> CanvasNode

            Returns the root of the scenegraph. For the main canvas, this is an 
            :py:class:`AVGNode`. For an offscreen canvas, this is a 
            :py:class:`CanvasNode`.
        
    .. autoclass:: OffscreenCanvas

        An OffscreenCanvas is a Canvas that is rendered to a texture. It can be
        referenced in the href attribute of an image node. See 
        https://www.libavg.de/wiki/ProgrammersGuide/OffscreenRendering for an in-depth 
        explanation of using offscreen rendering. Offscreen canvases are created by
        calling :py:meth:`Player.createCanvas`, :py:meth:`Player.loadCanvasFile` or
        :py:meth:`Player.loadCanvasString`.

        .. py:attribute:: autorender

            Turns autorendering on or off. Default is :py:const:`True`.

        .. py:attribute:: handleevents

            :py:const:`True` if events that arrive at an image node that is displaying 
            this canvas are routed to the offscreen canvas. Read-only.

        .. py:attribute:: mipmap

            :py:const:`True` if mipmaps are generated and used for the canvas. This is 
            used instead of RasterNode.mipmap for images that render the canvas. 
            Read-only.

        .. py:attribute:: multisamplesamples

            Number of samples per pixel to use for multisampling. Setting this to
            1 disables multisampling. Read-only.

        .. py:method:: getID() -> string

            Returns the id of the canvas. This is the same as
            calling :samp:`canvas.getRootNode().getID()`.

        .. py:method:: getNumDependentCanvases

            Returns the number of canvases that reference this canvas. Used mainly
            for unit tests.
                
        .. py:method:: registerCameraNode

        .. py:method:: render()

            Forces an immediate redraw of the offscreen canvas. This makes sure that
            following calls to screenshot() get a current version of the canvas and 
            is usually used in combination with :samp:`autorender=False`.

        .. py:method:: unregisterCameraNode

        .. py:classmethod:: isMultisampleSupported() -> bool

            :py:const:`True` if the machine's OpenGL implementation supports offscreen 
            multisampling.

    .. autoclass:: Player

        The class used to load and play avg files and the main interface to the avg
        renderer. Player is a singleton. There is only one instance, accessed by 
        :py:attr:`avg.player`.

        **Messages:**

            To get these messages, call :py:meth:`Publisher.subscribe`.

            .. py:method:: KEY_DOWN(keyEvent)
            
                Called whenever a key is pressed.
            
            .. py:method:: KEY_UP(keyEvent)

                Called whenever a key is released.

            .. py:method:: ON_FRAME()

                Called each frame.

        .. py:attribute:: pluginPath

            A list of directories where the player searches for plugins when 
            :py:meth:`loadPlugin()` is called. The separator between path entries is a 
            semicolon (';') under Windows and a colon (':') under Mac and Linux.

        .. py:attribute:: volume

            Total audio playback volume. 0 is silence, 1 passes media file
            volume through unchanged. Values higher than 1 can be used to
            amplify playback. A limiter prevents distortion when the volume
            is set too high.

        .. py:method:: addInputDevice(inputDevice)

            Registers an :py:class:`InputDevice` with the system.

        .. py:method:: areFullShadersSupported() -> bool

            Returns :py:const:`True` if the current OpenGL configuration has full shader
            support. Platforms without full shader support (this includes OpenGL ES) disable
            several :py:class:`FXNode` types. Calling this when playback is not running is
            an error.

        .. py:method:: assumePixelsPerMM(ppmm)

            Tells the system to assume a resolution for the physical screen, overriding 
            operating system information. The parameter is the number of pixels per
            millimeter as a :samp:`float`. This function affects the values returned by 
            :py:meth:`getPhysicalScreenDimensions` and :py:meth:`getPixelsPerMM`. It is
            useful for situations in which the OS cannot know the resolution (e.g. 
            projectors) and when the automatic functions return wrong values (which 
            happens, unfortunately, because of operating system deficiencies).

        .. py:method:: callFromThread(pyfunc)

            Executes :py:attr:`pyfunc` in the main thread of execution, in the next event
            handling phase. This method is the only libavg method that is thread-safe and
            can be called from secondary threads of execution. :py:attr:`pyfunc` can be 
            any python callable, including any libavg methods.

        .. py:method:: clearInterval(id) -> bool

            Stops a timeout, an interval or an onFrameHandler from being called.
            Returns :py:const:`True` if there was an interval with the given 
            :py:attr:`id`, :py:const:`False` if not.
            
            :param int id: 
            
                An id returned by :py:meth:`setInterval`, :py:meth:`setTimeout` 
                or :py:meth:`setOnFrameHandler`.

        .. py:method:: createCanvas(*params) -> OffscreenCanvas

            Creates an empty offscreen canvas. Parameters are given under 
            :py:class:`OffscreenCanvas`.

        .. py:method:: createMainCanvas(*params) -> Canvas

            Creates an empty canvas with a render window and an AVGNode as root node. 
            Parameters are given under :py:class:`AVGNode`.

        .. py:method:: createNode(xml) -> Node

            Creates a new Node. This node can be used as parameter to 
            :py:meth:`DivNode.appendChild()` and :py:meth:`DivNode.insertChild()`.
            This method will create any type of node, including :samp:`<div>` nodes
            with children.

            :param xml: 
            
                xml string conforming to the avg dtd that specifies the node to create.

        .. py:method:: createNode(type, args) -> Node

            Creates a new Node. This node can be used as parameter to 
            :py:meth:`DivNode.appendChild()` and :py:meth:`DivNode.insertChild()`.
            This method will only create one node at a time.

            :param string type: 
            
                Type string of the node to create (For example, :samp:`image` and 
                :samp:`words` are valid type strings).

            :param dict args: a dictionary specifying attributes of the node.

        .. py:method:: deleteCanvas(id)

            Removes the canvas given by id from the player's internal list of
            canvases. It is an error to delete a canvas that is still referenced by
            an image node.

        .. py:method:: enableGLErrorChecks(enable)

            Enables or disables checking for errors after each OpenGL call. By default,
            this is :py:const:`False`, since it hurts performance in some setups. It is
            enabled by the tests. You do not need this method unless you are looking for
            errors inside libavg.

        .. py:method:: enableMouse(enable)
        
            Enables or disable mouse event handling.
            
        .. py:method:: enableMultitouch()

            Enables multitouch event handling. Several drivers are available that 
            generate multitouch events. To choose a driver, set the environment
            variable :envvar:`AVG_MULTITOUCH_DRIVER` to the appropriate value:

            :samp:`TUIO`:
                Listens for TUIO events from a tracker that conforms to the TUIO 
                protocol (http://www.tuio.org), a de-facto standard for multitouch
                events. By default, it listens to events on the default TUIO UDP port
                3333, but this can be configured using the environment variable 
                :envvar:`AVG_TUIO_PORT`.

            :samp:`APPLETRACKPAD`:
                Uses the trackpad built into Mac Book Pros to generate events.

            :samp:`LINUXMTDEV`:
                Uses the linux mtdev library to interface to multitouch devices.
                The environment variable :envvar:`AVG_LINUX_MULTITOUCH_DEVICE` is used
                to determine which device file to open. Default is 
                :samp:`/dev/input/event3`.

            :samp:`TRACKER`:
                Enables the internal camera-based tracker. Configuring this tracker is
                described under https://www.libavg.de/wiki/ProgrammersGuide/Tracker.

            :samp:`WIN7TOUCH`:
                Enables handling of Windows 7 touch events. This works with all devices
                which have Windows 7 drivers.

            :samp:`XINPUT`:
                Uses X11-based multitouch detection. This needs X11 with XInput >= 2.1
                support. For backwards compatibility reasons, XINPUT21 is supported as 
                value as well.

            If :envvar:`AVG_MULTITOUCH_DRIVER` is not set, the driver defaults to 
            a plattform-specific one. Under Linux, the default is :samp:`XINPUT` if
            XInput multitouch is supported on the system, otherwise :samp:`LINUXMTDEV`.
            Under Windows, the default is :samp:`WIN7TOUCH`.

            :py:meth:`enableMultitouch` throws an exception if the chosen driver is not
            available or no multitouch device could be found. (Exception: Since there is
            no way to determine if a TUIO device is available, :py:meth:`enableMultitouch`
            always appears to succeed in this case.)

        .. py:method:: getCanvas(id) -> OffscreenCanvas

            Returns the offscreen canvas with the :py:attr:`id` given.

        .. py:method:: getCurrentEvent() -> Event

            Must be called inside an event handler and returns the event that's being
            processed. Throws an exception if called outside an event handler.

        .. py:method:: getEffectiveFramerate() -> float

            Returns the framerate that the player is actually achieving. The
            value returned is not averaged and reflects only the current frame.

        .. py:method:: getElementByID(id) -> Node

            Returns an element in the main avg tree.
            
            :param id: id attribute of the node to return.

        .. py:method:: getEventHook() -> pyfunc

            Returns the last event hook set using :py:meth:`setEventHook`.

        .. py:method:: getFrameDuration() -> float

            Returns the number of milliseconds that have elapsed since the last
            frame (i.e. the last display update).

        .. py:method:: getFramerate() -> float

            Returns the current target framerate in frames per second. To get the 
            actual framerate that the player is currently achieving, call 
            :py:meth:`getEffectiveFramerate`.

        .. py:method:: getFrameTime() -> int

            Returns the number of milliseconds that have elapsed since playback
            has started. Honors FakeFPS. The time returned stays constant for an
            entire frame; it is the time of the last display update.

        .. py:method:: getKeyModifierState() -> KeyModifier

            Returns the current modifier keys pressed, or'ed together. For a list of
            possible values, see :py:attr:`KeyEvent.modifiers`.

        .. py:method:: getMainCanvas() -> Canvas

            Returns the main canvas. This is the canvas loaded using :py:meth:`loadFile`
            or :py:meth:`loadString` and displayed on screen.

        .. py:method:: getMouseState() -> MouseEvent

            Returns the last mouse event generated.

        .. py:method:: getPhysicalScreenDimensions() -> Point2D

            Returns the size of the primary screen in millimeters.

        .. py:method:: getPixelsPerMM() -> float

            Returns the number of dots per millimeter of the primary display. Assumes
            square pixels.

        .. py:method:: getRootNode() -> Node

            Returns the outermost element in the main avg tree.

        .. py:method:: getScreenResolution() -> Point2D

            Returns the size in pixels of the current screen.

        .. py:method:: getTestHelper

        .. py:method:: getTracker() -> Tracker

            Returns a tracker previously created using :py:meth:`enableMultitouch` with
            the internal tracker configured.

        .. py:method:: getVideoMemInstalled() -> int

            Returns the amount of dedicated video memory installed in the system in 
            bytes (which might be zero in case of shared-memory graphics cards).
            Only available when using NVidia drivers.

        .. py:method:: getVideoMemUsed() -> int

            Returns the amount of dedicated video memory used in bytes. This
            is the total amount used by all programs. Only available when using NVidia
            drivers.

        .. py:method:: getVideoRefreshRate() -> float

            Returns the current hardware video refresh rate in number of
            refreshes per second.

        .. py:method:: isCursorShown()

            Returns :py:const:`True` if the mouse cursor is visible.
            
        .. py:method:: isFullscreen()

            Returns :py:const:`True` if the player is running in fullscreen mode.
            
        .. py:method:: isMultitouchAvailable() -> bool

            Returns :py:const:`True` if a multitouch device has been configured and is
            active, :py:const:`False` if not. Must be called after :py:meth:`play()`.

        .. py:method:: isPlaying() -> bool

            Returns :py:const:`True` if :py:meth:`play()` is currently executing, 
            :py:const:`False` if not.

        .. py:method:: keepWindowOpen()

            Tells the player to keep the playback window open after :py:meth:`play()`
            has returned. This makes it possible to reuse the window with another scene
            and :py:meth:`play()` call. It is used by the tests to keep flickering to a
            minimum and increase speed.

        .. py:method:: loadCanvasFile(filename) -> OffscreenCanvas

            Loads the canvas file specified in filename and adds it to the
            registered offscreen canvases.

        .. py:method:: loadCanvasString(avgString) -> OffscreenCanvas

            Parses avgString, loads the nodes it contains and adds the hierarchy
            to the registered offscreen canvases.
            
            :param string avgString: An xml string containing an avg node hierarchy.

        .. py:method:: loadFile(filename) -> Canvas

            Loads the avg file specified in filename. Returns the canvas loaded.
            The canvas is the main canvas displayed onscreen.

        .. py:method:: loadPlugin(name)

            Load a Plugin.
        
            :param string name: filename of the plugin without directory and
                file extension.

        .. py:method:: loadString(avgString) -> Canvas

            Parses avgString and loads the nodes it contains. Returns the canvas
            loaded. The canvas is the main canvas displayed onscreen.

            :param string avgString: An xml string containing an avg node hierarchy.

        .. py:method:: play()

            Opens a playback window or screen and starts playback. play returns
            when playback has ended.

        .. py:method:: screenshot() -> Bitmap

            Returns the contents of the current screen as a bitmap.

        .. py:method:: setCursor(bitmap, hotspot)

            Sets the mouse cursor to the bitmap given. The bitmap must have a size
            divisible by 8 and an RGBA pixel format. The cursor generated is
            binary black and white with a binary transparency channel. hotspot is
            the relative position of the actual pointing coordinate in the
            bitmap.

        .. py:method:: setEventHook(pyfunc)

            Set a callable which will receive all events before the standard event 
            handlers receive them. If this callable returns :py:const:`True`,
            the event is not propagated to the standard event handlers.
        
            Generally, :py:meth:`setEventHook` should be used as a last resort. In most
            cases, standard event handlers are a lot cleaner. Also, setting several event
            hooks is not supported by libavg. To get around this limitation, you can
            use :py:meth:`getEventHook` to chain event hook functions.
            
            Note that :py:attr:`event.node` is not set in the callback, since the
            system hasn't determined the node to send the event to at that
            point. 
            
        .. py:method:: setFakeFPS(fps)

            Sets a fixed number of virtual frames per second that are used as
            clock source for video playback, animations and other time-based
            actions. If a value of :samp:`-1` is given as parameter, the real clock is
            used. :py:meth:`setFakeFPS` can be used to get reproducible results for 
            recordings or automated tests. Setting FakeFPS has the side-effect of
            disabling audio.

        .. py:method:: setFramerate(framerate)

            Sets the desired framerate for playback. Turns off syncronization
            to the vertical blanking interval.

        .. py:method:: setGamma(red, green, blue)

            Sets display gamma. This is a control for overall brightness and
            contrast that leaves black and white unchanged but adjusts greyscale
            values. :samp:`1.0` is identity, higher values give a brighter image, lower
            values a darker one.

        .. py:method:: setInterval(time, pyfunc) -> int

            Sets a python callable object that should be executed regularly.
            :py:meth:`setInterval` returns an id that can be used to
            call :py:meth:`clearInterval()` to stop the function from being called. The
            callback is called at most once per frame.

            :param int time: Number of milliseconds between two calls.

            :param pyfunc: Python callable to execute.

        .. py:method:: setMousePos(pos)

            Sets the position of the mouse cursor. Generates a mouse motion event.

        .. py:method:: setMultiSampleSamples(multiSampleSamples)

            Sets the number of samples per pixel to compute.
            This costs performance and smoothes the edges of polygons. A value of
            :samp:`1`  turns multisampling (also knowna as FSAA - Full-Screen
            Antialiasing) off. Good values are dependent on the graphics driver and 
            the performance of the graphics card.

        .. py:method:: setOGLOptions(usePOW2Textures, usePixelBuffers, multiSampleSamples, shaderUsage=AUTO, useDebugContext=False)

            Determines which OpenGL extensions to check for and use if possible.
            This method is mainly used for debugging purposes while developing libavg, 
            but can also be used to work around buggy drivers. The values set here
            override those in the :file:`avgrc` file. Note that with the exception of
            multiSampleSamples, fallbacks are always used - if a feature is
            specified that the system doesn't support, a less demanding one will
            be used.

            Must be called before :py:meth:`play`.

            :param bool usePOW2Textures: 
            
                If :py:const:`True`, restricts textures to power-of-two dimensions.

            :param bool usePixelBuffers: 
                
                If :py:const:`False`, disables the use of OpenGL pixel buffer objects.

            :param int multiSampleSamples: 
            
                The number of samples per pixel to compute.
                This costs performance and smoothes the edges of polygons. A value of
                :samp:`1` turns multisampling (also known as FSAA - Full-Screen 
                Antialiasing) off. Good values are dependent on the graphics driver and 
                the performance of the graphics card.

            :param shaderUsage: 
            
                Either :py:const:`FULL`, :py:const:`MINIMAL` or :py:const:`AUTO`. 
                :py:const:`MINIMAL` restricts shader usage to a subset that doesn't use
                much GPU power.

            :param useDebugContext:

                Uses an OpenGL Debug Context for rendering if the graphics driver supports
                it. This causes more verbose error messages and warnings in the case of
                OpenGL errors.
                
                
        .. py:method:: setOnFrameHandler(pyfunc) -> int

            Sets a python callable object that should be executed once per frame.
            This is the same as :samp:`setInterval(0, pyfunc)`. Returns an id that can 
            be used to call :py:meth:`clearInterval()` to stop the function from being 
            called.

            :param pyfunc: Python callable to execute.

        .. py:method:: setResolution(fullscreen, width, height, bpp)

            Sets display engine parameters. Must be called before :py:meth:`loadFile` or
            :py:meth:`loadString`.
                
            :param bool fullscreen: 
            
                :py:const:`True` if the avg file should be rendered fullscreen.

            :param int width, height: 
            
                The window size (if fullscreen is :py:const:`False`)
                or screen resolution (if fullscreen is :py:const:`True`).

            :param int bpp: 
            
                Number of bits per pixel to use. Valid values are :samp:`15`, :samp:`16`,
                :samp:`24` and :samp:`32`.

        .. py:method:: setTimeout(time, pyfunc) -> int

            Sets a python callable object that should be executed after a set
            amount of time. :py:meth:`setTimeout` returns an id that can be used to
            call :py:meth:`clearInterval()` to stop the function from being called.

            :param int time: Number of milliseconds before the call.

            :param pyfunc: Python callable to execute.

        .. py:method:: setVBlankFramerate(rate)

            Sets the desired number of monitor refreshes before the next
            frame is displayed. The resulting framerate is determined by the
            monitor refresh rate divided by the rate parameter.

            :param int rate: 
            
                Number of vertical blanking intervals to wait. On Mac OS X, only :samp:`1`
                is supported as rate.

        .. py:method:: setWindowFrame(hasWindowFrame)

            :py:attr:`hasWindowFrame` should be set to :py:const:`True` if a 
            non-fullscreen player should have a window frame. If set to 
            :py:const:`False`, the player runs with no title bar or window frame. Must
            be called before :py:meth:`play` is called.

        .. py:method:: setWindowPos(x, y)

            Sets the location of the player window. Must be called before loadFile
            or loadString.

        .. py:method:: setWindowTitle(title)

            Sets the string displayed in the title bar of the libavg window. Default is
            'libavg'.

        .. py:method:: showCursor(show)

            Shows or hides the mouse cursor.
            
            :param bool show: :py:const:`True` if the mouse cursor should be visible.

        .. py:method:: stop()

            Stops playback and resets the video mode if necessary.

        .. py:method:: stopOnEscape(stop)

            Toggles player stop upon escape keystroke. If stop is :py:const:`True` 
            (the default), if player will halt playback when :kbd:`Esc` is pressed.

        .. py:method:: useGLES(gles)

            Chooses whether to use OpenGL ES or desktop OpenGL for rendering.
            Must be called before :py:meth:`play`.

            :param bool gles: :py:const:`True` if OpenGL ES should be used.

        .. py:classmethod:: get() -> Player

            .. deprecated:: 1.8
                Use :attr:`avg.player` instead.

            This method gives access to the player instance. If no player has been 
            created yet, a player is created.