-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVideoPlayer.cpp
220 lines (191 loc) · 7.48 KB
/
VideoPlayer.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
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
#include <iomanip>
#include <SDL2/SDL.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "Decoder.h"
const char* video_url = "tcp://127.0.0.1:5001";
// const char * video_url = "tcp://10.152.4.207:5000";
// const char * video_url = "tcp://10.152.4.195:5000";
int port = 5002;
const char* target_ip = "127.0.0.1";
bool video = true;
bool fullscreen = false;
int vsync = 1;
const int targetWidth = 1280, targetHeight = 720;
int height = 720, width = 1280; // will be overwritten by the video dimensions in the decoder; definition here
// necessary if no video is used
bool readyToQuit = false; // Quits all threads
uint8_t** argb_src = (uint8_t**)malloc(sizeof(uint8_t*) * 4); // Pointer to the decoded image
bool newImage =
false; // Set to true by the decoder when it decodes a frame. Set to false after oculus as copied a decoded frame
#ifdef ARTIFICIAL_DELAY
unsigned int listlength = 0;
#endif
using namespace std;
int main(int argc, char* argv[])
{
if (argc == 2)
{
video_url = argv[1];
}
else
{
cout << "Decoding video from default url:" << video_url << ". For other sources, use e.g. '" << argv[0]
<< " tcp://10.152.4.207:5000'\n";
}
// SDL Inits
if (SDL_Init(SDL_INIT_VIDEO) < 0)
cout << "Failed to initialize SDL! SDL Error :" << SDL_GetError() << endl;
int display = 0;
// if (SDL_GetNumVideoDisplays() > 1) {
// cout << "Choose display for video output.\n\n 0 = default desktop,\n 1 = secondary screen,\n etc." <<
//endl;
// cin >> display;
// }
SDL_Event* ev = new SDL_Event();
uint8_t* pixel_data = (uint8_t*)malloc(targetWidth * targetHeight * 4 * sizeof(uint8_t));
uint8_t** pixels;
int* argb_stride;
pixels = (uint8_t**)malloc(sizeof(uint8_t*) * 4);
pixels[0] = pixel_data;
pixels[1] = pixel_data + targetWidth * targetHeight;
pixels[2] = pixel_data + targetWidth * targetHeight * 2;
pixels[3] = pixel_data + targetWidth * targetHeight * 3;
argb_stride = (int*)malloc(sizeof(int) * 1);
argb_stride[0] = 4 * targetWidth;
int test[1];
test[0] = targetWidth * 4;
thread dec;
if (video)
dec = thread(Decoder, video_url, argb_src, &newImage);
//------------------ UDP port setup ---------------
struct sockaddr_in remaddr;
int fd, slen = sizeof(remaddr);
//-------------------------- UDP Port setup -----------------------
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
cout << "Could not create socket!" << endl;
memset((char*)&remaddr, 0, sizeof(remaddr));
remaddr.sin_family = AF_INET;
remaddr.sin_port = htons(port);
if (inet_aton(target_ip, &remaddr.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
// Waiting for the first decoded image
if (video)
{
// cout << "Waiting for first decoded image..." << endl;
while (!newImage)
{
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
cout << "Got first image, now displaying" << endl;
}
SDL_Window* win = SDL_CreateWindow("Videoplayer",
SDL_WINDOWPOS_CENTERED_DISPLAY(display),
SDL_WINDOWPOS_CENTERED_DISPLAY(display),
targetWidth,
targetHeight,
SDL_WINDOW_OPENGL);
if (!win)
cout << "Failed to create Window! SDL Error :" << SDL_GetError() << endl;
SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
if (!ren)
cout << "Failed to create Renderer! SDL Error :" << SDL_GetError() << endl;
SDL_Texture* tex =
SDL_CreateTexture(ren, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, targetWidth, targetHeight);
if (!tex)
cout << "Failed to create Texture! SDL Error :" << SDL_GetError() << endl;
if (fullscreen)
{
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);
SDL_GL_SetSwapInterval(vsync);
}
// std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time, m_end_time;
// Main displaying loop
while (!readyToQuit)
{
// Polling for user signal to end
while (SDL_PollEvent(ev))
{
if (ev->type == SDL_KEYDOWN)
{
if ((ev->key.keysym.sym == SDLK_q) || (ev->key.keysym.sym == SDLK_RETURN) ||
(ev->key.keysym.sym == SDLK_SPACE) || (ev->key.keysym.sym == SDLK_ESCAPE))
readyToQuit = true;
const char* msg;
switch (ev->key.keysym.sym)
{
case SDLK_m:
msg = "m";
break; // Increase QP by 5
case SDLK_n:
msg = "n";
break; // Decrease QP by 5
case SDLK_c:
msg = "c";
break; // Detect plane and insert new cube
case SDLK_r:
msg = "r";
break; // Reset remaining hits
case SDLK_w:
msg = "g";
break; // Increase box size
case SDLK_s:
msg = "s";
break; // Decrease box size
case SDLK_o:
if (listlength > 0)
listlength = max((unsigned int)0, listlength - 1);
std::cout << "\n" << listlength << "\n";
msg = "o";
break; // Decrease delay
case SDLK_p:
listlength++;
std::cout << "\n" << listlength << "\n";
msg = "p";
break; // Increase delay
default:
break;
}
sendto(fd, msg, sizeof(char), 0, (struct sockaddr*)&remaddr, slen);
}
else
{
if ((ev->type == SDL_MOUSEBUTTONDOWN))
{ // Shoot!
sendto(fd, "t", sizeof(char), 0, (struct sockaddr*)&remaddr, slen);
}
}
}
// Updating image if new one has been decoded
if (newImage)
{
SDL_LockTexture(tex, NULL, reinterpret_cast<void**>(pixels), test);
// Copy decoded image into retrieved pointer from locking
// m_start_time = std::chrono::high_resolution_clock::now();
if (video)
memcpy(*pixels, *argb_src, targetWidth * targetHeight * 4 * sizeof(uint8_t));
// m_end_time = std::chrono::high_resolution_clock::now();
SDL_UnlockTexture(tex);
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
// Reset the newImage flag
newImage = false;
// std::cout << "t_cpy=" << std::chrono::duration_cast<std::chrono::microseconds>(m_end_time -
//m_start_time).count() << "\n";
}
}
readyToQuit = true;
if (video)
dec.join();
// Shutting down SDL
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
cout << "Quit video player" << endl;
return 0;
}