| 1 | /* |
| 2 | This file is part of the CVD Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | // // |
| 10 | // videoplay.C // |
| 11 | // // |
| 12 | // Test program for videofilebuffer // |
| 13 | // // |
| 14 | // Paul Smith 30 April 2004 // |
| 15 | // // |
| 16 | ///////////////////////////////////////////////////////////////////////////// |
| 17 | |
| 18 | #include <atomic> |
| 19 | #include <condition_variable> |
| 20 | #include <cstdlib> |
| 21 | #include <cstring> |
| 22 | #include <cvd/byte.h> |
| 23 | #include <cvd/gl_helpers.h> |
| 24 | #include <cvd/glwindow.h> |
| 25 | #include <cvd/image.h> |
| 26 | #include <cvd/rgb.h> |
| 27 | #include <cvd/videosource.h> |
| 28 | #include <mutex> |
| 29 | #include <thread> |
| 30 | #include <typeinfo> |
| 31 | |
| 32 | #ifdef __GNUC__ |
| 33 | #pragma GCC diagnostic push |
| 34 | #endif |
| 35 | #pragma GCC diagnostic ignored "-Wconversion" |
| 36 | #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" |
| 37 | #include "progs/tinyformat.h" |
| 38 | #ifdef __GNUC__ |
| 39 | #pragma GCC diagnostic pop |
| 40 | #endif |
| 41 | |
| 42 | #ifdef CVD_HAVE_V4LBUFFER |
| 43 | #include <cvd/Linux/v4lbuffer.h> |
| 44 | #endif |
| 45 | |
| 46 | #if unix |
| 47 | #include <unistd.h> |
| 48 | #else |
| 49 | // From here: https://github.com/iotivity/iotivity/blob/master/resource/c_common/windows/src/getopt.c |
| 50 | // with bug fixes |
| 51 | /* ***************************************************************** |
| 52 | * |
| 53 | * Copyright 2016 Microsoft |
| 54 | * |
| 55 | * |
| 56 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 57 | * you may not use this file except in compliance with the License. |
| 58 | * You may obtain a copy of the License at |
| 59 | * |
| 60 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 61 | * |
| 62 | * Unless required by applicable law or agreed to in writing, software |
| 63 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 64 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 65 | * See the License for the specific language governing permissions and |
| 66 | * limitations under the License. |
| 67 | * |
| 68 | ******************************************************************/ |
| 69 | char* optarg = NULL; |
| 70 | int optind = 1; |
| 71 | char optopt = 0; |
| 72 | int opterr = 0; |
| 73 | |
| 74 | int getopt(int argc, char* const argv[], const char* optstring) |
| 75 | { |
| 76 | if((optind >= argc) || (argv[optind][0] != '-') || (argv[optind][0] == 0)) |
| 77 | { |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | int opt = optopt = argv[optind][1]; |
| 82 | const char* p = strchr(optstring, opt); |
| 83 | |
| 84 | if(p == NULL) |
| 85 | { |
| 86 | return '?'; |
| 87 | } |
| 88 | optind++; |
| 89 | if(p[1] == ':') |
| 90 | { |
| 91 | if(optind >= argc) |
| 92 | { |
| 93 | return '?'; |
| 94 | } |
| 95 | optarg = argv[optind]; |
| 96 | optind++; |
| 97 | } |
| 98 | return opt; |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | using namespace CVD; |
| 103 | using std::cerr; |
| 104 | using std::clog; |
| 105 | using std::cout; |
| 106 | using std::endl; |
| 107 | using std::istringstream; |
| 108 | using std::lock_guard; |
| 109 | using std::make_pair; |
| 110 | using std::make_unique; |
| 111 | using std::mutex; |
| 112 | using std::pair; |
| 113 | using std::string; |
| 114 | using std::thread; |
| 115 | using std::unique_lock; |
| 116 | using std::unique_ptr; |
| 117 | using std::vector; |
| 118 | using namespace std::literals; |
| 119 | |
| 120 | class Actions : public GLWindow::EventHandler |
| 121 | { |
| 122 | public: |
| 123 | int paused; |
| 124 | int advance_one; |
| 125 | int back_one; |
| 126 | int expose; |
| 127 | int quit; |
| 128 | int recording; |
| 129 | int new_recording; |
| 130 | |
| 131 | Actions() |
| 132 | : paused(0) |
| 133 | , advance_one(0) |
| 134 | , expose(0) |
| 135 | , quit(0) |
| 136 | , recording(0) |
| 137 | , new_recording(0) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | void clear() |
| 142 | { |
| 143 | advance_one = 0; |
| 144 | expose = 0; |
| 145 | back_one = 0; |
| 146 | new_recording = 0; |
| 147 | } |
| 148 | |
| 149 | virtual void on_key_down(GLWindow&, int key) |
| 150 | { |
| 151 | if(key == ' ' || key == 'p') |
| 152 | paused = !paused; |
| 153 | else if(key == 'q' || key == 27) |
| 154 | { |
| 155 | recording = 0; |
| 156 | quit = 1; |
| 157 | } |
| 158 | else if(key == '.') |
| 159 | { |
| 160 | advance_one = 1; |
| 161 | paused = 1; |
| 162 | } |
| 163 | else if(key == ',') |
| 164 | { |
| 165 | back_one = 1; |
| 166 | paused = 1; |
| 167 | } |
| 168 | else if(key == 'r') |
| 169 | { |
| 170 | recording = !recording; |
| 171 | if(recording) |
| 172 | new_recording = true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | virtual void on_event(GLWindow&, int e) |
| 177 | { |
| 178 | if(e == GLWindow::EVENT_EXPOSE) |
| 179 | expose = 1; |
| 180 | else if(e == GLWindow::EVENT_CLOSE) |
| 181 | quit = 1; |
| 182 | } |
| 183 | |
| 184 | virtual void on_resize(GLWindow&, ImageRef size) |
| 185 | { |
| 186 | glViewport(0, 0, size.x, size.y); |
| 187 | glMatrixMode(GL_PROJECTION); |
| 188 | glLoadIdentity(); |
| 189 | glRasterPos2f(-1, 1); |
| 190 | glOrtho(-0.375, size.x - 0.375, size.y - 0.375, -0.375, -1, 1); //offsets to make (0,0) the top left pixel (rather than off the display) |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | template <class C> |
| 195 | class MessageQueue |
| 196 | { |
| 197 | private: |
| 198 | std::deque<C> data; |
| 199 | std::mutex queue_mutex; |
| 200 | std::atomic<size_t> length; |
| 201 | std::condition_variable empty; |
| 202 | |
| 203 | public: |
| 204 | MessageQueue() |
| 205 | { |
| 206 | length = 0; |
| 207 | } |
| 208 | void push(const C& a) |
| 209 | { |
| 210 | lock_guard<mutex> lock(queue_mutex); |
| 211 | data.push_back(a); |
| 212 | length = data.size(); |
| 213 | empty.notify_one(); |
| 214 | } |
| 215 | |
| 216 | void push(C&& a) |
| 217 | { |
| 218 | lock_guard<mutex> lock(queue_mutex); |
| 219 | data.emplace_back(move(a)); |
| 220 | length = data.size(); |
| 221 | empty.notify_one(); |
| 222 | } |
| 223 | |
| 224 | C pop() |
| 225 | { |
| 226 | unique_lock<mutex> lock(queue_mutex); |
| 227 | |
| 228 | while(data.empty()) |
| 229 | empty.wait(lock); |
| 230 | |
| 231 | C a = move(data.front()); |
| 232 | data.pop_front(); |
| 233 | length = data.size(); |
| 234 | return a; |
| 235 | } |
| 236 | |
| 237 | size_t get_length() const |
| 238 | { |
| 239 | return length; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | template <class C> |
| 244 | void play(string s, string fmt, unsigned int decimate) |
| 245 | { |
| 246 | VideoBuffer<C>* buffer = open_video_source<C>(s); |
| 247 | |
| 248 | GLWindow display(buffer->size()); |
| 249 | glDrawBuffer(GL_BACK); |
| 250 | |
| 251 | double dt = 1.001 / buffer->frame_rate(); |
| 252 | |
| 253 | cout << "FPS: " << buffer->frame_rate() << " " << dt << endl; |
| 254 | cout << "Size: " << buffer->size() << endl; |
| 255 | |
| 256 | RawVideoBuffer* root = buffer->root_buffer(); |
| 257 | |
| 258 | cout << typeid(*root).name() << endl; |
| 259 | |
| 260 | #ifdef CVD_HAVE_V4LBUFFER |
| 261 | if(dynamic_cast<V4L::RawV4LBuffer*>(root)) |
| 262 | { |
| 263 | cout << "Using V4LBuffer.\n"; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | GLenum texTarget; |
| 268 | #ifdef GL_TEXTURE_RECTANGLE_ARB |
| 269 | texTarget = GL_TEXTURE_RECTANGLE_ARB; |
| 270 | #else |
| 271 | #ifdef GL_TEXTURE_RECTANGLE_NV |
| 272 | texTarget = GL_TEXTURE_RECTANGLE_NV; |
| 273 | #else |
| 274 | texTarget = GL_TEXTURE_RECTANGLE_EXT; |
| 275 | #endif |
| 276 | #endif |
| 277 | |
| 278 | VideoFrame<C>* frame = 0; |
| 279 | bool f = 1; |
| 280 | GLWindow::EventSummary e; |
| 281 | glDisable(GL_BLEND); |
| 282 | glEnable(texTarget); |
| 283 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 284 | glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 285 | glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 286 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 287 | bool new_frame = 0; |
| 288 | |
| 289 | Actions a; |
| 290 | |
| 291 | string action = "Playing"; |
| 292 | string rec; |
| 293 | bool old_paused = 0; |
| 294 | |
| 295 | int rec_sequence = -1; |
| 296 | int rec_number = 0; |
| 297 | |
| 298 | MessageQueue<pair<unique_ptr<Image<C>>, string>> save_queue; |
| 299 | |
| 300 | vector<thread> savers; |
| 301 | |
| 302 | //Spawn some saver threads |
| 303 | for(unsigned int i = 0; i < thread::hardware_concurrency(); i++) |
| 304 | { |
| 305 | savers.push_back(thread([&]() { |
| 306 | for(;;) |
| 307 | { |
| 308 | auto s = save_queue.pop(); |
| 309 | |
| 310 | //This essentially indicates a quit. |
| 311 | if(s.first == nullptr) |
| 312 | break; |
| 313 | |
| 314 | try |
| 315 | { |
| 316 | img_save(*(s.first), s.second); |
| 317 | } |
| 318 | catch(const CVD::Exceptions::All& e) |
| 319 | { |
| 320 | cerr << "Error saving: " << fmt << ": " << e.what() << endl; |
| 321 | } |
| 322 | } |
| 323 | })); |
| 324 | } |
| 325 | |
| 326 | for(;;) |
| 327 | { |
| 328 | a.clear(); |
| 329 | display.handle_events(a); |
| 330 | |
| 331 | if(a.new_recording) |
| 332 | { |
| 333 | rec_sequence++; |
| 334 | rec_number = 0; |
| 335 | } |
| 336 | |
| 337 | if(a.quit && save_queue.get_length() == 0) |
| 338 | break; |
| 339 | |
| 340 | new_frame = 0; |
| 341 | if(!a.paused || a.advance_one || a.back_one) |
| 342 | { |
| 343 | if(a.back_one) |
| 344 | { |
| 345 | buffer->seek_to(frame->timestamp() - dt); |
| 346 | } |
| 347 | |
| 348 | if(frame) |
| 349 | buffer->put_frame(frame); |
| 350 | frame = buffer->get_frame(); |
| 351 | new_frame = 1; |
| 352 | |
| 353 | if(a.recording && (rec_number % decimate) == 0) |
| 354 | { |
| 355 | rec = tfm::format(fmt.c_str(), rec_sequence, rec_number); |
| 356 | unique_ptr<Image<C>> img = make_unique<Image<C>>(); |
| 357 | img->copy_from(*frame); |
| 358 | save_queue.push(move(make_pair(move(img), rec))); |
| 359 | } |
| 360 | else |
| 361 | rec = ""; |
| 362 | |
| 363 | rec_number++; |
| 364 | |
| 365 | glTexImage2D(*frame, 0, GL_TEXTURE_RECTANGLE_NV); |
| 366 | } |
| 367 | |
| 368 | if(old_paused != a.paused) |
| 369 | new_frame = true; |
| 370 | old_paused = a.paused; |
| 371 | |
| 372 | if(a.paused) |
| 373 | action = "Paused"; |
| 374 | else if(a.recording) |
| 375 | action = "Recording"; |
| 376 | else if(a.quit) |
| 377 | action = "Quitting"; |
| 378 | else |
| 379 | action = "Playing"; |
| 380 | |
| 381 | if(a.expose || new_frame) |
| 382 | { |
| 383 | glEnable(texTarget); |
| 384 | glBegin(GL_QUADS); |
| 385 | glTexCoord2i(0, 0); |
| 386 | glVertex2i(0, 0); |
| 387 | glTexCoord2i(frame->size().x, 0); |
| 388 | glVertex2i(display.size().x, 0); |
| 389 | glTexCoord2i(frame->size().x, frame->size().y); |
| 390 | glVertex2i(display.size().x, display.size().y); |
| 391 | glTexCoord2i(0, frame->size().y); |
| 392 | glVertex2i(0, display.size().y); |
| 393 | glEnd(); |
| 394 | glDisable(texTarget); |
| 395 | |
| 396 | glPushMatrix(); |
| 397 | glTranslatef(10, 30, 0); |
| 398 | glScalef(20, -20, 20); |
| 399 | glColor3f(1, 0, 0); |
| 400 | glDrawText(action + " " + rec); |
| 401 | glTranslatef(0, -1.5, 0); |
| 402 | if(save_queue.get_length() > 0) |
| 403 | glDrawText(tfm::format("%03i queued writes", save_queue.get_length())); |
| 404 | glPopMatrix(); |
| 405 | |
| 406 | glFlush(); |
| 407 | display.swap_buffers(); |
| 408 | } |
| 409 | |
| 410 | if(f) |
| 411 | { |
| 412 | cout << "frame size: " << frame->size() << endl; |
| 413 | f = 0; |
| 414 | } |
| 415 | |
| 416 | if(a.paused) |
| 417 | std::this_thread::sleep_for(100ms); |
| 418 | } |
| 419 | |
| 420 | if(frame) |
| 421 | buffer->put_frame(frame); |
| 422 | |
| 423 | cout << "Joining all threads...\n"; |
| 424 | //Quit all the threads. |
| 425 | for(unsigned int i = 0; i < savers.size(); i++) |
| 426 | save_queue.push(make_pair(nullptr, "")); |
| 427 | |
| 428 | for(auto& t : savers) |
| 429 | t.join(); |
| 430 | |
| 431 | cout << "Exiting\n"; |
| 432 | } |
| 433 | |
| 434 | int main(int argc, char* argv[]) |
| 435 | { |
| 436 | int help = 0; |
| 437 | int type = 0; |
| 438 | int error = 0; |
| 439 | unsigned int decimate = 1; |
| 440 | |
| 441 | string fmt; |
| 442 | int c; |
| 443 | |
| 444 | opterr = 0; |
| 445 | while((c = getopt(argc, argv, "hmf:d:")) != -1) |
| 446 | { |
| 447 | if(c == 'h') |
| 448 | help = 1; |
| 449 | else if(c == 'm') |
| 450 | type = 1; |
| 451 | else if(c == 'f') |
| 452 | fmt = optarg; |
| 453 | else if(c == 'd') |
| 454 | { |
| 455 | istringstream d(optarg); |
| 456 | d >> decimate; |
| 457 | |
| 458 | if(!d.good()) |
| 459 | { |
| 460 | cerr << "Error: could not parse " << optarg << " as argument for -d\n"; |
| 461 | } |
| 462 | } |
| 463 | else if(c == '?') |
| 464 | { |
| 465 | error = 1; |
| 466 | if(optopt == 'f' || optopt == 'd') |
| 467 | cerr << "Error: Option -f requires an argumnt\n"; |
| 468 | else |
| 469 | cerr << "Error: Unknown option -" << optopt << endl; |
| 470 | } |
| 471 | else |
| 472 | abort(); |
| 473 | } |
| 474 | |
| 475 | if(optind != argc - 1) |
| 476 | { |
| 477 | cerr << "Error: specify the video source\n"; |
| 478 | error = 1; |
| 479 | } |
| 480 | |
| 481 | if(help || error) |
| 482 | { |
| 483 | clog << "Usage: [-h] [-m] [-f fmt] buffer \n" |
| 484 | << " -h Display this message\n" |
| 485 | << " -m Run the buffer in mono mode\n" |
| 486 | << " -f Format string for recording video frames, e.g. image-%03i-%05i.png\n" |
| 487 | << " The numbers are respectively the sequence number and the frame number\n" |
| 488 | << " within the sequence.\n" |
| 489 | << " -d Decimation factor for recording. 1 = record every frame.\n" |
| 490 | << "Keys: \n" |
| 491 | << "<space> pause\n" |
| 492 | << ", reverse one frame (requires seekable buffer)\n" |
| 493 | << ". advance one frame (requires seekable buffer)\n" |
| 494 | << "r start/stop recording.\n" |
| 495 | << "q quit (this may take a while if writing is in progress)\n"; |
| 496 | |
| 497 | return (error != false); |
| 498 | } |
| 499 | |
| 500 | try |
| 501 | { |
| 502 | if(type == 1) |
| 503 | play<byte>(argv[optind], fmt, decimate); |
| 504 | else |
| 505 | play<Rgb<byte>>(argv[optind], fmt, decimate); |
| 506 | } |
| 507 | catch(CVD::Exceptions::All& e) |
| 508 | { |
| 509 | cout << "Error: " << e.what() << endl; |
| 510 | } |
| 511 | } |