JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
BufferedVideoReader.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 
20 
21 // ####################################################################################################
22 BufferedVideoReader::BufferedVideoReader(std::string const & instance, size_t bufsize) :
23  jevois::Component(instance), itsBuf(bufsize), itsRunning(true)
24 { }
25 
26 // ####################################################################################################
28 { }
29 
30 // ####################################################################################################
32 {
33  // Start our reader thread:
34  itsRunFut = std::async(std::launch::async, &BufferedVideoReader::run, this);
35 }
36 
37 // ####################################################################################################
39 {
40  // Tell run() thread to finish up:
41  itsRunning.store(false);
42  if (itsBuf.filled_size()) itsBuf.pop(); // in case run() is blocked trying to push
43 
44  try { itsRunFut.get(); } catch (...) { jevois::warnAndIgnoreException(); }
45 }
46 
47 // ####################################################################################################
49 { return itsBuf.pop(); }
50 
51 // ####################################################################################################
53 {
54  // Open the video file:
55  cv::VideoCapture vcap(absolutePath(filename::get()));
56  if (vcap.isOpened() == false) { itsBuf.push(cv::Mat()); LERROR("Could not open video file " << filename::get()); }
57 
58  cv::Mat frame;
59  while (itsRunning.load())
60  if (vcap.read(frame)) itsBuf.push(frame); else { itsBuf.push(cv::Mat()); break; }
61 }
62 
std::string warnAndIgnoreException()
size_t filled_size() const
void push(T const &val)
virtual void postUninit() override
Uninit, wait on our run thread and swallow any exception.
~BufferedVideoReader()
Virtual destructor for safe inheritance.
#define LERROR(msg)
void run()
Reader thread.
BufferedVideoReader(std::string const &instance, size_t bufsize=100)
Constructor.
cv::Mat get()
Get the next frame as a BGR cv::Mat, or an empty cv::Mat when the movie is finished.
virtual void postInit() override
Start the thread that loads, decodes and pushes the frames into our buffer.
std::string absolutePath(std::string const &path="")