forked from MalfoyJW/parsec-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
309 lines (248 loc) · 8.39 KB
/
main.c
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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "parsec-dso.h"
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"
#define PARSEC_APP_CLIPBOARD_MSG 7
#define WINDOW_W 1280
#define WINDOW_H 720
#define CHANNELS 2
#define SAMPLE_RATE 48000
#define FRAMES_PER_PACKET 960
#if defined(_WIN32)
#include <GL/gl.h>
#if !defined(BITS)
#define BITS 64
#endif
#if (BITS == 64)
#define SDK_PATH "../../sdk/windows/parsec.dll"
#else
#define SDK_PATH "../../sdk/windows/parsec32.dll"
#endif
#elif defined(__APPLE__)
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#define SDK_PATH "../../sdk/macos/libparsec.dylib"
#else
#include <GL/gl.h>
#define SDK_PATH "../../sdk/linux/libparsec.so"
#endif
struct context {
bool done;
ParsecDSO *parsec;
// Video
float scale;
SDL_Window *window;
SDL_GLContext *gl;
SDL_Surface *surface;
SDL_Cursor *cursor;
// Audio
SDL_AudioDeviceID audio;
bool playing;
uint32_t minBuffer;
uint32_t maxBuffer;
};
static void logCallback(ParsecLogLevel level, const char *msg, void *opaque)
{
opaque;
printf("[%s] %s\n", level == LOG_DEBUG ? "D" : "I", msg);
}
static void audio(const int16_t *pcm, uint32_t frames, void *opaque)
{
struct context *context = (struct context *) opaque;
uint32_t size = SDL_GetQueuedAudioSize(context->audio);
uint32_t queued_frames = size / (CHANNELS * sizeof(int16_t));
uint32_t queued_packets = queued_frames / FRAMES_PER_PACKET;
if (context->playing && queued_packets > context->maxBuffer) {
SDL_ClearQueuedAudio(context->audio);
SDL_PauseAudioDevice(context->audio, 1);
context->playing = false;
} else if (!context->playing && queued_packets >= context->minBuffer) {
SDL_PauseAudioDevice(context->audio, 0);
context->playing = true;
}
SDL_QueueAudio(context->audio, pcm, frames * CHANNELS * sizeof(int16_t));
}
static void userData(struct context *context, uint32_t id, uint32_t bufferKey)
{
char *msg = ParsecGetBuffer(context->parsec, bufferKey);
if (msg && id == PARSEC_APP_CLIPBOARD_MSG)
SDL_SetClipboardText(msg);
ParsecFree(context->parsec, msg);
}
static void cursor(struct context *context, ParsecCursor *cursor, uint32_t bufferKey)
{
if (cursor->imageUpdate) {
uint8_t *image = ParsecGetBuffer(context->parsec, bufferKey);
if (image) {
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(image, cursor->width, cursor->height,
32, cursor->width * 4, 0xff, 0xff00, 0xff0000, 0xff000000);
SDL_Cursor *sdlCursor = SDL_CreateColorCursor(surface, cursor->hotX, cursor->hotY);
SDL_SetCursor(sdlCursor);
SDL_FreeCursor(context->cursor);
context->cursor = sdlCursor;
SDL_FreeSurface(context->surface);
context->surface = surface;
ParsecFree(context->parsec, image);
}
}
if (cursor->modeUpdate) {
if (SDL_GetRelativeMouseMode() && !cursor->relative) {
SDL_SetRelativeMouseMode(SDL_DISABLE);
SDL_WarpMouseInWindow(context->window, cursor->positionX, cursor->positionY);
} else if (!SDL_GetRelativeMouseMode() && cursor->relative) {
SDL_SetRelativeMouseMode(SDL_ENABLE);
}
}
}
static int32_t audioThread(void *opaque)
{
struct context *context = (struct context *) opaque;
while (!context->done)
ParsecClientPollAudio(context->parsec, audio, 100, context);
return 0;
}
static int32_t renderThread(void *opaque)
{
struct context *context = (struct context *) opaque;
SDL_GL_MakeCurrent(context->window, context->gl);
SDL_GL_SetSwapInterval(1);
while (!context->done) {
int32_t w = 0, h = 0;
SDL_GetWindowSize(context->window, &w, &h);
ParsecClientSetDimensions(context->parsec, DEFAULT_STREAM, w, h, context->scale);
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ParsecClientGLRenderFrame(context->parsec, DEFAULT_STREAM, NULL, NULL, 100);
SDL_GL_SwapWindow(context->window);
glFinish();
}
ParsecClientGLDestroy(context->parsec, DEFAULT_STREAM);
SDL_GL_DeleteContext(context->gl);
return 0;
}
int32_t main(int32_t argc, char **argv)
{
if (argc < 3) {
printf("Usage: client sessionID peerID\n");
return 1;
}
struct context context = {0};
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER);
SDL_AudioSpec want = {0}, have;
want.freq = SAMPLE_RATE;
want.format = AUDIO_S16;
want.channels = CHANNELS;
want.samples = 2048;
context.minBuffer = 1; // The number of audio packets (960 frames) to buffer before we begin playing
context.maxBuffer = 6; // The number of audio packets (960 frames) to buffer before overflow and clear
context.audio = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
context.window = SDL_CreateWindow("Parsec Client Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_W, WINDOW_H, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
context.gl = SDL_GL_CreateContext(context.window);
#if !defined(__APPLE__)
SDL_GL_MakeCurrent(context.window, NULL);
#endif
int32_t glW = 0;
SDL_GL_GetDrawableSize(context.window, &glW, NULL);
context.scale = (float) glW / (float) WINDOW_W;
ParsecStatus e = ParsecInit(NULL, NULL, SDK_PATH, &context.parsec);
if (e != PARSEC_OK) goto except;
ParsecSetLogCallback(context.parsec, logCallback, NULL);
ParsecClientConnect(context.parsec, NULL, argv[1], argv[2]);
SDL_Thread *render_thread = SDL_CreateThread(renderThread, "renderThread", &context);
SDL_Thread *audio_thread = SDL_CreateThread(audioThread, "audioThread", &context);
while (!context.done) {
for (SDL_Event msg; SDL_PollEvent(&msg);) {
ParsecMessage pmsg = {0};
switch (msg.type) {
case SDL_QUIT:
context.done = true;
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
pmsg.type = MESSAGE_KEYBOARD;
pmsg.keyboard.code = (ParsecKeycode) msg.key.keysym.scancode;
pmsg.keyboard.mod = msg.key.keysym.mod;
pmsg.keyboard.pressed = msg.key.type == SDL_KEYDOWN;
break;
case SDL_MOUSEMOTION:
pmsg.type = MESSAGE_MOUSE_MOTION;
pmsg.mouseMotion.relative = SDL_GetRelativeMouseMode();
pmsg.mouseMotion.x = pmsg.mouseMotion.relative ? msg.motion.xrel : msg.motion.x;
pmsg.mouseMotion.y = pmsg.mouseMotion.relative ? msg.motion.yrel : msg.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
pmsg.type = MESSAGE_MOUSE_BUTTON;
pmsg.mouseButton.button = msg.button.button;
pmsg.mouseButton.pressed = msg.button.type == SDL_MOUSEBUTTONDOWN;
break;
case SDL_MOUSEWHEEL:
pmsg.type = MESSAGE_MOUSE_WHEEL;
pmsg.mouseWheel.x = msg.wheel.x;
pmsg.mouseWheel.y = msg.wheel.y;
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
pmsg.type = MESSAGE_GAMEPAD_BUTTON;
pmsg.gamepadButton.id = msg.cbutton.which;
pmsg.gamepadButton.button = msg.cbutton.button;
pmsg.gamepadButton.pressed = msg.cbutton.type == SDL_CONTROLLERBUTTONDOWN;
break;
case SDL_CONTROLLERAXISMOTION:
pmsg.type = MESSAGE_GAMEPAD_AXIS;
pmsg.gamepadAxis.id = msg.caxis.which;
pmsg.gamepadAxis.axis = msg.caxis.axis;
pmsg.gamepadAxis.value = msg.caxis.value;
break;
case SDL_CONTROLLERDEVICEADDED:
SDL_GameControllerOpen(msg.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
pmsg.type = MESSAGE_GAMEPAD_UNPLUG;
pmsg.gamepadUnplug.id = msg.cdevice.which;
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(msg.cdevice.which));
break;
case SDL_CLIPBOARDUPDATE:
ParsecClientSendUserData(context.parsec, PARSEC_APP_CLIPBOARD_MSG, SDL_GetClipboardText());
break;
}
if (pmsg.type != 0)
ParsecClientSendMessage(context.parsec, &pmsg);
e = ParsecClientGetStatus(context.parsec, NULL);
if (e != PARSEC_CONNECTING && e != PARSEC_OK)
context.done = true;
}
for (ParsecClientEvent event; ParsecClientPollEvents(context.parsec, 0, &event);) {
switch (event.type) {
case CLIENT_EVENT_CURSOR:
cursor(&context, &event.cursor.cursor, event.cursor.key);
break;
case CLIENT_EVENT_USER_DATA:
userData(&context, event.userData.id, event.userData.key);
break;
case CLIENT_EVENT_RUMBLE:
break;
default:
break;
}
}
SDL_Delay(1);
}
SDL_WaitThread(audio_thread, NULL);
SDL_WaitThread(render_thread, NULL);
except:
if (e != PARSEC_OK) {
char error[32];
snprintf(error, 32, "Parsec error: %d\n", e);
SDL_ShowSimpleMessageBox(0, "Parsec Error", error, NULL);
}
ParsecDestroy(context.parsec);
SDL_DestroyWindow(context.window);
SDL_CloseAudioDevice(context.audio);
SDL_Quit();
return 0;
}