/
eyegazedatastream.ts
208 lines (179 loc) · 8 KB
/
eyegazedatastream.ts
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
import { EyeGazeData, EyeGazeEvent } from './eyegazeevent';
// tslint:disable:max-classes-per-file
export class EyeGazeEventTarget extends EventTarget { }
export class EyeGazeDataStream {
public static readonly EYE_GAZE_DATA = 'eyegazedata';
public static readonly NEW_SERVER_MESSAGE = 'newservermessage';
public static readonly CONNECTION_STATUS = 'connectionstatus';
public static readonly BINARY_MESSAGE_PARSING_ERROR = 'binarymessageparsingerror';
public static readonly SUCCESSFULLY_CONNECTED_TO_SERVER = 'successfully connected to server';
public static readonly DISCONNECTED_TO_SERVER = 'disconnected to server';
public static readonly CONNECTION_ERROR = 'connection error with server';
protected _webSocket: WebSocket;
protected _eyeGazeDataStreams: EyeGazeDataStreams = new EyeGazeDataStreams();
protected _eyeGazeEventTarget = new EventTarget();
protected generateStreamConfigByte(): Uint8Array {
const configByte = Uint8Array.from([0]);
if (this._eyeGazeDataStreams.gazePosition) {
configByte[0] = configByte[0] | 0b00000001;
}
if (this._eyeGazeDataStreams.gazeOrigin) {
configByte[0] = configByte[0] | 0b00000010;
}
if (this._eyeGazeDataStreams.eyePositionNormalized) {
configByte[0] = configByte[0] | 0b00000100;
}
if (this._eyeGazeDataStreams.headPositionAndRotation) {
configByte[0] = configByte[0] | 0b00001000;
}
if (this._eyeGazeDataStreams.userPresence) {
configByte[0] = configByte[0] | 0b00010000;
}
return configByte;
}
protected performHandshake(): void {
this._eyeGazeDataStreams.recalculateNumberOfFloats();
const configByte = this.generateStreamConfigByte();
this._webSocket.send(configByte);
}
protected parseEyeTrackingData(data: Float32Array): EyeGazeEvent {
if (data.length < this._eyeGazeDataStreams.expectedNumberOfFloats) {
return new CustomEvent(EyeGazeDataStream.BINARY_MESSAGE_PARSING_ERROR);
}
let currentIndexPosition = 0;
const eyeGazeData = new EyeGazeData();
if (this._eyeGazeDataStreams.gazePosition) {
eyeGazeData.gazePositionXY[0] = data[currentIndexPosition++];
eyeGazeData.gazePositionXY[1] = data[currentIndexPosition++];
}
if (this._eyeGazeDataStreams.gazeOrigin) {
eyeGazeData.gazeOriginRightXYZ[0] = data[currentIndexPosition++];
eyeGazeData.gazeOriginRightXYZ[1] = data[currentIndexPosition++];
eyeGazeData.gazeOriginRightXYZ[2] = data[currentIndexPosition++];
eyeGazeData.gazeOriginLeftXYZ[0] = data[currentIndexPosition++];
eyeGazeData.gazeOriginLeftXYZ[1] = data[currentIndexPosition++];
eyeGazeData.gazeOriginLeftXYZ[2] = data[currentIndexPosition++];
}
if (this._eyeGazeDataStreams.eyePositionNormalized) {
eyeGazeData.eyePositionRightNormalizedXYZ[0] = data[currentIndexPosition++];
eyeGazeData.eyePositionRightNormalizedXYZ[1] = data[currentIndexPosition++];
eyeGazeData.eyePositionRightNormalizedXYZ[2] = data[currentIndexPosition++];
eyeGazeData.eyePositionLeftNormalizedXYZ[0] = data[currentIndexPosition++];
eyeGazeData.eyePositionLeftNormalizedXYZ[1] = data[currentIndexPosition++];
eyeGazeData.eyePositionLeftNormalizedXYZ[2] = data[currentIndexPosition++];
}
if (this._eyeGazeDataStreams.headPositionAndRotation) {
eyeGazeData.headPositionXYZ[0] = data[currentIndexPosition++];
eyeGazeData.headPositionXYZ[1] = data[currentIndexPosition++];
eyeGazeData.headPositionXYZ[2] = data[currentIndexPosition++];
eyeGazeData.headRotationXYZ[0] = data[currentIndexPosition++];
eyeGazeData.headRotationXYZ[1] = data[currentIndexPosition++];
eyeGazeData.headRotationXYZ[2] = data[currentIndexPosition++];
}
if (this._eyeGazeDataStreams.userPresence) {
eyeGazeData.userPresence = data[currentIndexPosition] === 0.0 ? false : true;
}
return new CustomEvent(EyeGazeDataStream.EYE_GAZE_DATA, { detail: { eyeGazeData } });
}
protected onOpen(event: Event): void {
this.dispatchEvent(new CustomEvent(EyeGazeDataStream.CONNECTION_STATUS, {
detail:
{ message: EyeGazeDataStream.SUCCESSFULLY_CONNECTED_TO_SERVER, event },
}));
this.performHandshake();
}
protected onClose(event: Event): void {
this.dispatchEvent(new CustomEvent(EyeGazeDataStream.CONNECTION_STATUS, {
detail:
{ message: EyeGazeDataStream.DISCONNECTED_TO_SERVER, event },
}));
}
protected onError(event: Event): void {
this.dispatchEvent(new CustomEvent(EyeGazeDataStream.CONNECTION_STATUS, {
detail:
{ message: EyeGazeDataStream.CONNECTION_ERROR, event },
}));
}
protected async onMessage(event: MessageEvent): Promise<void> {
// handle stream data
if (typeof event.data !== 'string') {
const arrayBuffer = await event.data.arrayBuffer();
const floatData = new Float32Array(arrayBuffer);
this.dispatchEvent(this.parseEyeTrackingData(floatData));
// handle status message data
} else {
this.dispatchEvent(new CustomEvent(EyeGazeDataStream.NEW_SERVER_MESSAGE, {
detail:
{ message: event.data },
}));
}
}
public connect(serverAddress: string): void {
this._webSocket = new WebSocket(serverAddress);
// Arrow functions needed in order to not loose this-context.
this._webSocket.onopen = (event: Event) => {
this.onOpen(event);
};
this._webSocket.onclose = (event: Event) => {
this.onClose(event);
};
this._webSocket.onerror = (event: Event) => {
this.onError(event);
};
this._webSocket.onmessage = (event: MessageEvent) => {
this.onMessage(event);
};
}
// Mediation
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions): void {
this._eyeGazeEventTarget.addEventListener(type, listener, options);
}
dispatchEvent(event: Event): boolean {
return this._eyeGazeEventTarget.dispatchEvent(event);
}
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null,
options?: EventListenerOptions | boolean): void {
this._eyeGazeEventTarget.removeEventListener(type, callback, options);
}
get connectionState(): number {
return this._webSocket.readyState;
}
get eyeGazeDataStreams(): EyeGazeDataStreams {
return this._eyeGazeDataStreams;
}
set eyeGazeDataStreams(eyeGazeDataStreams: EyeGazeDataStreams) {
if (this._eyeGazeDataStreams !== eyeGazeDataStreams) {
this._eyeGazeDataStreams = eyeGazeDataStreams;
}
}
}
export class EyeGazeDataStreams {
public gazePosition = false;
public gazeOrigin = false;
public eyePositionNormalized = false;
public headPositionAndRotation = false;
public userPresence = false;
// The sum of floats from all activated data streams
public expectedNumberOfFloats = 0;
// Is automatically recalculated if the handshake is invoked
public recalculateNumberOfFloats(): void {
let newNumberOfFloats = 0;
if (this.gazePosition) {
newNumberOfFloats += 2;
}
if (this.gazeOrigin) {
newNumberOfFloats += 6;
}
if (this.eyePositionNormalized) {
newNumberOfFloats += 6;
}
if (this.headPositionAndRotation) {
newNumberOfFloats += 6;
}
if (this.userPresence) {
newNumberOfFloats += 1;
}
this.expectedNumberOfFloats = newNumberOfFloats;
}
}