• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2
3import sys
4import selectors
5import pykms
6
7w = 640
8h = 480
9fmt = pykms.PixelFormat.YUYV
10
11card = pykms.Card()
12res = pykms.ResourceManager(card)
13conn = res.reserve_connector()
14crtc = res.reserve_crtc(conn)
15plane = res.reserve_overlay_plane(crtc, fmt)
16
17mode = conn.get_default_mode()
18modeb = mode.to_blob(card)
19
20req = pykms.AtomicReq(card)
21req.add(conn, "CRTC_ID", crtc.id)
22req.add(crtc, {"ACTIVE": 1,
23        "MODE_ID": modeb.id})
24req.commit_sync(allow_modeset = True)
25
26NUM_BUFS = 5
27
28fbs = []
29for i in range(NUM_BUFS):
30    fb = pykms.DumbFramebuffer(card, w, h, fmt)
31    fbs.append(fb)
32
33vidpath = pykms.VideoDevice.get_capture_devices()[0]
34
35vid = pykms.VideoDevice(vidpath)
36cap = vid.capture_streamer
37cap.set_port(0)
38cap.set_format(fmt, w, h)
39cap.set_queue_size(NUM_BUFS)
40
41for fb in fbs:
42    cap.queue(fb)
43
44cap.stream_on()
45
46
47def readvid(conn, mask):
48    fb = cap.dequeue()
49
50    if card.has_atomic:
51        plane.set_props({
52            "FB_ID": fb.id,
53            "CRTC_ID": crtc.id,
54            "SRC_W": fb.width << 16,
55            "SRC_H": fb.height << 16,
56            "CRTC_W": fb.width,
57            "CRTC_H": fb.height,
58        })
59    else:
60        crtc.set_plane(plane, fb, 0, 0, fb.width, fb.height,
61            0, 0, fb.width, fb.height)
62
63    cap.queue(fb)
64
65def readkey(conn, mask):
66    #print("KEY EVENT");
67    sys.stdin.readline()
68    exit(0)
69
70sel = selectors.DefaultSelector()
71sel.register(cap.fd, selectors.EVENT_READ, readvid)
72sel.register(sys.stdin, selectors.EVENT_READ, readkey)
73
74while True:
75    events = sel.select()
76    for key, mask in events:
77        callback = key.data
78        callback(key.fileobj, mask)
79