JeVois  1.2
JeVois Smart Embedded Machine Vision Toolkit
MovieOutput.H
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 #pragma once
19 
23 #include <opencv2/core/version.hpp>
24 #include <opencv2/videoio.hpp> // for cv::VideoCapture
25 #include <future>
26 
27 namespace jevois
28 {
29  //! Video output to a movie file, using OpenCV video encoding
30  /*! This video output mode saved output frames to a file (or series of files). It is useful when developing new
31  algorithms to check the correctness of generated outputs offline, or to save some documentation/demo movies of a
32  module. \ingroup core */
33  class MovieOutput : public VideoOutput
34  {
35  public:
36  //! Constructor
37  MovieOutput(std::string const & fn);
38 
39  //! Virtual destructor for safe inheritance
40  virtual ~MovieOutput();
41 
42  //! Set the video format and frame rate
43  virtual void setFormat(VideoMapping const & m) override;
44 
45  //! Get a pre-allocated image so that we can fill the pixel data and later send out using send()
46  /*! May throw if not buffer is available, i.e., all have been queued to send but have not yet been
47  sent. Application code must balance exactly one send() for each get(). */
48  virtual void get(RawImage & img) override;
49 
50  //! Send an image out
51  /*! May throw if the format is incorrect or std::overflow_error if we have not yet consumed the previous image. */
52  virtual void send(RawImage const & img) override;
53 
54  //! Start streaming
55  virtual void streamOn() override;
56 
57  //! Abort streaming
58  /*! This only cancels future get() and send() calls, one should still call streamOff() to turn off streaming. */
59  virtual void abortStream() override;
60 
61  //! Stop streaming
62  virtual void streamOff() override;
63 
64  protected:
65  std::shared_ptr<VideoBuf> itsBuffer; //!< Our single video buffer
66  VideoMapping itsMapping; //!< Our current video mapping, we resize the input to the mapping's camera dims
67 
68  void run(); //!< Use a thread to encode and save frames
69  std::future<void> itsRunFut; //!< Future for our run() thread
71  jevois::BlockingBehavior::Block> itsBuf; //!< Buffer of frames to encode and write to file
72  std::atomic<bool> itsSaving; //!< True when we are saving to file
73  int itsFileNum; //!< File number, gets incremented on each streamOff() to avoid overwriting previous files
74  std::atomic<bool> itsRunning; //!< True when our run() thread should keep running
75  std::string itsFilename; //!< Current file name to save video to
76  std::string itsFilebase; //!< Current file base to save video to
77  };
78 }
79 
Thread-safe synchronized producer/consumer queue.
Definition: BoundedBuffer.H:37
Block until operation can be completed.
jevois::BoundedBuffer< cv::Mat, jevois::BlockingBehavior::Block, jevois::BlockingBehavior::Block > itsBuf
Buffer of frames to encode and write to file.
Definition: MovieOutput.H:71
virtual void streamOff() override
Stop streaming.
Definition: MovieOutput.C:107
virtual void setFormat(VideoMapping const &m) override
Set the video format and frame rate.
Definition: MovieOutput.C:55
Video output to a movie file, using OpenCV video encoding.
Definition: MovieOutput.H:33
std::future< void > itsRunFut
Future for our run() thread.
Definition: MovieOutput.H:69
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
Simple struct to hold video mapping definitions for the processing Engine.
Definition: VideoMapping.H:41
std::string itsFilename
Current file name to save video to.
Definition: MovieOutput.H:75
int itsFileNum
File number, gets incremented on each streamOff() to avoid overwriting previous files.
Definition: MovieOutput.H:73
VideoMapping itsMapping
Our current video mapping, we resize the input to the mapping's camera dims.
Definition: MovieOutput.H:66
virtual ~MovieOutput()
Virtual destructor for safe inheritance.
Definition: MovieOutput.C:38
std::atomic< bool > itsSaving
True when we are saving to file.
Definition: MovieOutput.H:72
virtual void streamOn() override
Start streaming.
Definition: MovieOutput.C:95
Base class for video output. Gadget, MovieOutput, VideoDisplay, and VideoOutputNone derive from it...
Definition: VideoOutput.H:27
std::atomic< bool > itsRunning
True when our run() thread should keep running.
Definition: MovieOutput.H:74
std::shared_ptr< VideoBuf > itsBuffer
Our single video buffer.
Definition: MovieOutput.H:65
std::string itsFilebase
Current file base to save video to.
Definition: MovieOutput.H:76
virtual void abortStream() override
Abort streaming.
Definition: MovieOutput.C:101
MovieOutput(std::string const &fn)
Constructor.
Definition: MovieOutput.C:31
virtual void send(RawImage const &img) override
Send an image out.
Definition: MovieOutput.C:80
void run()
Use a thread to encode and save frames.
Definition: MovieOutput.C:126