-
Notifications
You must be signed in to change notification settings - Fork 0
/
multimonitor.cpp
147 lines (120 loc) · 3.42 KB
/
multimonitor.cpp
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
#include <SDL.h>
#include <SDL_thread.h>
#include <assert.h>
#include "net.hpp"
using namespace chrono;
static inline SDL_Rect build_rect(int x, int y, int w, int h) {
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
static inline SDL_Rect get_rect(int width, int height, int num, int tot) {
switch (tot) {
case 1:
return build_rect(0, 0, width, height);
case 2:
switch (num) {
case 0:
return build_rect(0, 0, width, height / 2);
case 1:
return build_rect(0, height / 2, width, height / 2);
}
case 3:
switch (num) {
case 0:
return build_rect(0, 0, width, height / 2);
case 1:
return build_rect(0, height / 2, width / 2, height / 2);
case 2:
return build_rect(width / 2, height / 2, width / 2, height / 2);
}
case 4:
switch (num) {
case 0:
return build_rect(0, 0, width / 2, height / 2);
case 1:
return build_rect(width / 2, 0, width / 2, height / 2);
case 2:
return build_rect(0, height / 2, width / 2, height / 2);
case 3:
return build_rect(width / 2, height / 2, width / 2, height / 2);
}
}
assert(false);
}
int main() {
int width = 800;
int height = 600;
double fps = 15.0;
SDL_Surface *screen;
// Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(width, height, 0, SDL_RESIZABLE);
if(!screen) {
fprintf(stderr, "SDL: could not set video mode - exiting\n");
exit(1);
}
vector< ImageServerConf > servers;
servers.emplace_back("localhost", "2204", 10.0);
servers.emplace_back("localhost", "2205", 10.0);
ImageMultiClient client(servers, IM_OP_YUV_DECODE);
vector< SDL_Overlay* > overlays;
overlays.resize(servers.size());
bool running = true;
while (running) {
// Get a new set of frames (FIXME: implement frame skipping)
vector< const Image * > res = client.advance_to_last(false);
client.print_status();
// Show the frames
if (res.size() > 0) {
for (unsigned int i = 0; i < servers.size(); i++) {
auto &image = res[i];
if (image != NULL) {
auto &overlay = overlays[i];
if (overlay == NULL) {
overlay = SDL_CreateYUVOverlay(image->width, image->height, SDL_YV12_OVERLAY, screen);
}
SDL_LockYUVOverlay(overlay);
copy_yuv_to_planes(image, overlay->pitches, overlay->pixels, 1);
SDL_UnlockYUVOverlay(overlay);
SDL_Rect rect = get_rect(width, height, i, servers.size());
SDL_DisplayYUVOverlay(overlay, &rect);
}
}
}
// Query for SDL events
while (true) {
SDL_Event event;
int res;
res = SDL_PollEvent(&event);
if (!res) break;
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_VIDEORESIZE:
// Braces to limit the scope of resevent and avoid triggering
// warnings related to switch jumps
{
SDL_ResizeEvent &resevent = event.resize;
width = resevent.w;
height = resevent.h;
screen = SDL_SetVideoMode(width, height, 0, SDL_RESIZABLE);
}
break;
default:
break;
}
}
// Wait a bit before next frame
this_thread::sleep_for(microseconds(int(1e6 / fps)));
}
SDL_Quit();
return 0;
}