VTK  9.2.6
vtkFFMPEGVideoSource.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkFFMPEGVideoSource.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
30 #ifndef vtkFFMPEGVideoSource_h
31 #define vtkFFMPEGVideoSource_h
32 
33 #include "vtkIOFFMPEGModule.h" // For export macro
34 #include "vtkMultiThreader.h" // for ivar
35 #include "vtkNew.h" // for ivar
36 #include "vtkVideoSource.h"
37 #include <condition_variable> // for std::condition_variable_any
38 #include <functional> // for audio callback
39 #include <mutex> // for std::mutex
40 
41 class vtkFFMPEGVideoSourceInternal;
43 
44 // audio callback struct, outside the class so that we
45 // can forward ref it
47 {
52  int DataType;
53  bool Packed;
54  unsigned char** Data;
56  void* ClientData;
57 };
58 
59 // video callback struct, outside the class so that we
60 // can forward ref it
62 {
63  int Height;
64  int LineSize[8];
65  unsigned char* Data[8]; // nullptr for empty planes
67  void* ClientData;
68 };
69 
70 class VTKIOFFMPEG_EXPORT vtkFFMPEGVideoSource : public vtkVideoSource
71 {
72 public:
73  static vtkFFMPEGVideoSource* New();
75  void PrintSelf(ostream& os, vtkIndent indent) override;
76 
80  void Record() override;
81 
85  void Play() override;
86 
90  void Stop() override;
91 
95  void Grab() override;
96 
98 
101  void SetFrameSize(int x, int y, int z) override;
102  void SetFrameSize(int dim[3]) override { this->SetFrameSize(dim[0], dim[1], dim[2]); }
104 
108  void SetFrameRate(float rate) override;
109 
113  void SetOutputFormat(int format) override;
114 
119  void Initialize() override;
120 
125  void ReleaseSystemResources() override;
126 
128 
131  vtkSetFilePathMacro(FileName);
132  vtkGetFilePathMacro(FileName);
134 
140  void InternalGrab() override;
141 
142  // is the video at the end of file?
143  // Useful for while loops
144  vtkGetMacro(EndOfFile, bool);
145 
146  // Is the video stream stereo 3d
147  vtkGetMacro(Stereo3D, bool);
148 
149  // we do not use Invoke Observers here because this callback
150  // will happen in a different thread that could conflict
151  // with events from other threads. In this function you should
152  // not block the thread (for example waiting for audio to play)
153  // instead you should have enough buffering that you can consume
154  // the provided data and return. Typically even 1 second of
155  // buffer storage is enough to prevent blocking.
156  typedef std::function<void(vtkFFMPEGVideoSourceAudioCallbackData const& data)> AudioCallbackType;
157  void SetAudioCallback(AudioCallbackType cb, void* clientData)
158  {
159  this->AudioCallback = cb;
160  this->AudioCallbackClientData = clientData;
161  }
162 
163  // we do not use Invoke Observers here because this callback
164  // will happen in a different thread that could conflict
165  // with events from other threads. In this function you should
166  // not block the thread (for example waiting for video to play)
167  // instead you should have enough buffering that you can consume
168  // the provided data and return.
169  typedef std::function<void(vtkFFMPEGVideoSourceVideoCallbackData const& data)> VideoCallbackType;
170  void SetVideoCallback(VideoCallbackType cb, void* clientData)
171  {
172  this->VideoCallback = cb;
173  this->VideoCallbackClientData = clientData;
174  }
175 
177 
182  vtkSetMacro(DecodingThreads, int);
183  vtkGetMacro(DecodingThreads, int);
185 
186 protected:
188  ~vtkFFMPEGVideoSource() override;
189 
190  AudioCallbackType AudioCallback;
192 
194 
195  static void* DrainAudioThread(vtkMultiThreader::ThreadInfo* data);
196  void* DrainAudio(vtkMultiThreader::ThreadInfo* data);
198 
199  static void* DrainThread(vtkMultiThreader::ThreadInfo* data);
200  void* Drain(vtkMultiThreader::ThreadInfo* data);
202 
203  bool EndOfFile;
204 
205  std::condition_variable_any FeedCondition;
206  std::mutex FeedMutex;
207  std::condition_variable_any FeedAudioCondition;
208  std::mutex FeedAudioMutex;
209  static void* FeedThread(vtkMultiThreader::ThreadInfo* data);
210  void* Feed(vtkMultiThreader::ThreadInfo* data);
212 
213  char* FileName;
214 
215  vtkFFMPEGVideoSourceInternal* Internal;
216 
217  void ReadFrame();
218 
219  bool Stereo3D;
220 
221  VideoCallbackType VideoCallback;
223 
224 private:
226  void operator=(const vtkFFMPEGVideoSource&) = delete;
227 };
228 
229 #endif
VideoCallbackType VideoCallback
virtual void Stop()
Stop recording or playing.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SetAudioCallback(AudioCallbackType cb, void *clientData)
std::function< void(vtkFFMPEGVideoSourceVideoCallbackData const &data)> VideoCallbackType
std::condition_variable_any FeedCondition
virtual void Initialize()
Initialize the hardware.
std::function< void(vtkFFMPEGVideoSourceAudioCallbackData const &data)> AudioCallbackType
AudioCallbackType AudioCallback
vtkFFMPEGVideoSourceInternal * Internal
This is the structure that is passed to the thread that is created from the SingleMethodExecute, MultipleMethodExecute or the SpawnThread method.
void SetFrameSize(int dim[3]) override
Request a particular frame size (set the third value to 1).
Superclass of video input devices for VTK.
virtual void ReleaseSystemResources()
Release the video driver.
std::condition_variable_any FeedAudioCondition
a simple class to control print indentation
Definition: vtkIndent.h:39
virtual void Record()
Record incoming video at the specified FrameRate.
Reader for ffmpeg supported formats.
virtual void SetOutputFormat(int format)
Set the output format.
static vtkVideoSource * New()
virtual void SetFrameSize(int x, int y, int z)
Set the full-frame size.
virtual void Play()
Play through the 'tape' sequentially at the specified frame rate.
virtual void Grab()
Grab a single video frame.
virtual void SetFrameRate(float rate)
Request a particular frame rate (default 30 frames per second).
virtual void InternalGrab()
The internal function which actually does the grab.
void SetVideoCallback(VideoCallbackType cb, void *clientData)