-
Notifications
You must be signed in to change notification settings - Fork 1
/
broadcastView.py
129 lines (95 loc) · 3.76 KB
/
broadcastView.py
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
import io
import time
import traceback
import tkinter as tk
from enum import Enum
from queue import Queue
from threading import Thread
from PIL import Image, ImageTk
modes = Enum('Modes', ('none', 'native', 'ndi'))
types = Enum('Types', ('items', 'map'))
class BroadcastView:
tkClaimed = False
def __init__(self, queue, type):
self.queue = queue
self.actualWidth = -1
self.actualHeight = -1
self.mode = modes.none
self.type = type
self.bgColor = None
self.ndiThread = None
def setMode(self, mode, bgColor=None):
self.bgColor = bgColor
if self.mode == modes.native and mode != modes.native:
self.queue.put((self.root.destroy, []))
if self.mode != modes.native and mode == modes.native:
self.queue.put((self.mainNativeInit, []))
if self.mode != modes.ndi and mode == modes.ndi:
self.ndiFrames = Queue()
self.ndiThread = Thread(target=self.ndiTask, daemon=True)
self.ndiThread.start()
self.mode = mode
def updateImage(self, bytes):
if self.mode == modes.native:
self.queue.put((self.mainUpdateNativeImage, (bytes,)))
if self.mode == modes.ndi:
self.ndiFrames.put(bytes)
def updateWindow(self):
if self.mode == modes.native:
self.root.update()
def mainNativeInit(self):
if not BroadcastView.tkClaimed:
self.tk = tk.Tk()
self.tk.withdraw()
BroadcastView.tkClaimed = True
self.root = tk.Toplevel()
self.root.config(bg=self.bgColor)
self.root.title(f"Magpie {'Items' if self.type == types.items else 'Map'} Broadcast View")
self.label = tk.Label(self.root)
self.label.pack()
def mainUpdateNativeImage(self, bytes):
try:
self.root.state() # Throws a TclError that we catch and ignore if the user closed the window
except tk.TclError:
return
self.root.update()
stream = io.BytesIO(bytes)
image = Image.open(stream)
if self.bgColor:
# Weird transparency stuff happens if the browser zooms in, do our best to strip it out
bgColor = (int(self.bgColor[1:3], 16), int(self.bgColor[3:5], 16), int(self.bgColor[5:7], 16), 255)
width, height = image.size
data = image.load()
for y in range(height):
for x in range(width):
if data[x, y][3] != 255:
data[x, y] = bgColor
tkImage = ImageTk.PhotoImage(image)
self.label.config(image=tkImage)
self.label.image = tkImage
w = self.root.winfo_width()
h = self.root.winfo_height()
if w != self.actualWidth or h != self.actualHeight:
x = self.root.winfo_x()
y = self.root.winfo_y()
self.root.geometry(f"{image.width}x{image.height}+{x}+{y}")
self.root.maxsize(image.width, image.height)
self.root.minsize(image.width, image.height)
self.actualWidth = self.root.winfo_width()
self.actualHeight = self.root.winfo_height()
self.root.update()
def ndiTask(self):
import NDIlib as ndi
from ndi import NdiStream
ndi.initialize()
stream = NdiStream(f"Magpie-{'Items' if self.type == types.items else 'Map'}")
while self.mode == modes.ndi:
try:
if not self.ndiFrames.empty():
stream.LoadImage(self.ndiFrames.get())
stream.Send()
except Exception as e:
print(f"Error in NDI stream: {e}, {traceback.format_exc()}")
time.sleep(0.25)
stream.Destroy()
ndi.destroy()