forked from LiquidFenrir/3ds-webcam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver_frames.py
More file actions
113 lines (101 loc) · 2.86 KB
/
receiver_frames.py
File metadata and controls
113 lines (101 loc) · 2.86 KB
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
import numpy as np
import qoi
import socket
import struct
import sounddevice
# import scipy.signal
import pyvirtualcam
import sys
import traceback
# packsize = int(sys.argv[2])
biggest_yet = bytearray(640 * 480 * 4 + 16)
print("socket!")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("connecting!")
sock.connect((sys.argv[1], 9975))
print("connected!")
try:
dat = sock.recv(4)
assert(bytes(dat) == b"meta")
dat = sock.recv(2)
scr_w = struct.unpack("!H", dat)[0]
dat = sock.recv(2)
scr_h = struct.unpack("!H", dat)[0]
dat = sock.recv(2)
aud_sr = struct.unpack("!H", dat)[0]
except Exception as e:
print("setup exc:", e)
sock.shutdown(0)
sock.close()
exit(1)
# blksz = 32730//15
blksz = 3072
previous_audio = b""
with pyvirtualcam.Camera(width=scr_w, height=scr_h, fps=60, device="OBS Virtual Camera", backend="obs") as cam:
try:
dt = np.dtype('<i2')
while True:
# print("receiving!")
dat = sock.recv(4)
if len(dat) != 4:
print(dat)
print("errored")
break
datalen = struct.unpack("!L", dat)[0]
# print(datalen, "bytes total (", dat, ")")
if datalen > len(biggest_yet):
biggest_yet = bytearray(datalen)
data = biggest_yet
idx = 0
yoink = 0
while idx != datalen:
dat = sock.recv(datalen - idx)
l = len(dat)
# print(f"loop {yoink}: {l}")
data[idx:idx+l] = dat
idx += l
yoink += 1
# quot, rem = divmod(datalen, packsize)
# print("have to receive ", quot, "packets and", rem, "extra bytes")
# for i in range(quot):
# dat = sock.recv(packsize)
# # print(dat)
# if len(dat) != packsize:
# raise Exception(f"problem loop: {len(dat)} vs {packsize} at loop {idx // packsize}")
# data[idx:idx+packsize] = dat
# idx += packsize
# if rem:
# dat = sock.recv(rem)
# # print(dat)
# if len(dat) != rem:
# raise Exception(f"problem rem: {len(dat)} vs {rem}")
# data[idx:idx+rem] = dat
# print("done receiving packets")
head = bytes(data[:4])
# print("early bytes:", head)
if head == b"PCMA":
samps = np.frombuffer(previous_audio + data[4:datalen], dtype=dt) / 32768
# print(f"audio, {samps.shape} samples ({samps.shape[0]/blksz} blocks)")
# number_of_samples = round(len(samps) * float(48000) / 32730)
l = samps.shape[0]
# resamped = scipy.signal.resample(samps, number_of_samples).astype(dtype=np.float32)
q, r = divmod(l, blksz)
for i in range(q):
idx = i * blksz
tow = samps[idx:idx+blksz]
tow = np.column_stack((tow, np.zeros_like(tow)))
if r:
previous_audio = data[(datalen - (r * 2)):datalen]
else:
previous_audio = b""
elif head == b"qoif":
# print("image")
cam.send(qoi.decode(bytes(data[:datalen])))
except Exception as e:
print("runtime exc:", e)
print(traceback.format_exc())
except KeyboardInterrupt:
print("stopping")
sock.shutdown(0)
sock.close()
print("Finish")