VTK  9.0.1
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 =========================================================================*/
27 #ifndef vtkFFMPEGVideoSource_h
28 #define vtkFFMPEGVideoSource_h
29 
30 #include "vtkIOFFMPEGModule.h" // For export macro
31 #include "vtkMultiThreader.h" // for ivar
32 #include "vtkNew.h" // for ivar
33 #include "vtkVideoSource.h"
34 #include <functional> // for audio callback
35 
36 class vtkFFMPEGVideoSourceInternal;
37 
39 class vtkMutexLock;
41 
42 // audio callback struct, outside the class so that we
43 // can forward ref it
45 {
50  int DataType;
51  bool Packed;
52  unsigned char** Data;
54  void* ClientData;
55 };
56 
57 // video callback struct, outside the class so that we
58 // can forward ref it
60 {
61  int Height;
62  int LineSize[8];
63  unsigned char* Data[8]; // nullptr for empty planes
65  void* ClientData;
66 };
67 
68 class VTKIOFFMPEG_EXPORT vtkFFMPEGVideoSource : public vtkVideoSource
69 {
70 public:
73 
77  void Record() override;
78 
82  void Play() override;
83 
87  void Stop() override;
88 
92  void Grab() override;
93 
95 
98  void SetFrameSize(int x, int y, int z) override;
99  void SetFrameSize(int dim[3]) override { this->SetFrameSize(dim[0], dim[1], dim[2]); }
101 
105  void SetFrameRate(float rate) override;
106 
110  void SetOutputFormat(int format) override;
111 
116  void Initialize() override;
117 
122  void ReleaseSystemResources() override;
123 
125 
128  vtkSetStringMacro(FileName);
129  vtkGetStringMacro(FileName);
131 
137  void InternalGrab() override;
138 
139  // is the video at the end of file?
140  // Useful for while loops
141  vtkGetMacro(EndOfFile, bool);
142 
143  // Is the video stream stereo 3d
144  vtkGetMacro(Stereo3D, bool);
145 
146  // we do not use Invoke Observers here because this callback
147  // will happen in a different thread that could conflict
148  // with events from other threads. In this function you should
149  // not block the thread (for example waiting for audio to play)
150  // instead you should have enough buffering that you can consume
151  // the provided data and return. Typically even 1 second of
152  // buffer storage is enough to prevent blocking.
154  void SetAudioCallback(AudioCallbackType cb, void* clientData)
155  {
156  this->AudioCallback = cb;
157  this->AudioCallbackClientData = clientData;
158  }
159 
160  // we do not use Invoke Observers here because this callback
161  // will happen in a different thread that could conflict
162  // with events from other threads. In this function you should
163  // not block the thread (for example waiting for video to play)
164  // instead you should have enough buffering that you can consume
165  // the provided data and return.
167  void SetVideoCallback(VideoCallbackType cb, void* clientData)
168  {
169  this->VideoCallback = cb;
170  this->VideoCallbackClientData = clientData;
171  }
172 
174 
179  vtkSetMacro(DecodingThreads, int);
180  vtkGetMacro(DecodingThreads, int);
182 
183 protected:
186 
189 
191 
195 
199 
200  bool EndOfFile;
201 
209 
210  char* FileName;
211 
212  vtkFFMPEGVideoSourceInternal* Internal;
213 
214  void ReadFrame();
215 
216  bool Stereo3D;
217 
220 
221 private:
223  void operator=(const vtkFFMPEGVideoSource&) = delete;
224 };
225 
226 #endif
vtkFFMPEGVideoSource::DecodingThreads
int DecodingThreads
Definition: vtkFFMPEGVideoSource.h:190
vtkFFMPEGVideoSource::DrainAudioThreadId
int DrainAudioThreadId
Definition: vtkFFMPEGVideoSource.h:194
vtkMultiThreader.h
vtkX3D::function
@ function
Definition: vtkX3D.h:255
vtkFFMPEGVideoSource::DrainAudio
void * DrainAudio(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::VideoCallbackType
std::function< void(vtkFFMPEGVideoSourceVideoCallbackData const &data)> VideoCallbackType
Definition: vtkFFMPEGVideoSource.h:166
vtkFFMPEGVideoSource::VideoCallback
VideoCallbackType VideoCallback
Definition: vtkFFMPEGVideoSource.h:218
vtkFFMPEGVideoSource::FeedMutex
vtkNew< vtkMutexLock > FeedMutex
Definition: vtkFFMPEGVideoSource.h:203
vtkFFMPEGVideoSourceVideoCallbackData::ClientData
void * ClientData
Definition: vtkFFMPEGVideoSource.h:65
vtkFFMPEGVideoSource::Drain
void * Drain(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSourceAudioCallbackData::Data
unsigned char ** Data
Definition: vtkFFMPEGVideoSource.h:52
vtkX3D::data
@ data
Definition: vtkX3D.h:321
vtkMutexLock
mutual exclusion locking class
Definition: vtkMutexLock.h:77
vtkFFMPEGVideoSource::SetFrameSize
void SetFrameSize(int dim[3]) override
Definition: vtkFFMPEGVideoSource.h:99
vtkFFMPEGVideoSource
Reader for ffmpeg supported formats.
Definition: vtkFFMPEGVideoSource.h:69
vtkFFMPEGVideoSource::EndOfFile
bool EndOfFile
Definition: vtkFFMPEGVideoSource.h:200
vtkFFMPEGVideoSourceVideoCallbackData::Height
int Height
Definition: vtkFFMPEGVideoSource.h:61
vtkVideoSource.h
vtkFFMPEGVideoSourceAudioCallbackData::ClientData
void * ClientData
Definition: vtkFFMPEGVideoSource.h:54
vtkFFMPEGVideoSourceAudioCallbackData::NumberOfSamples
int NumberOfSamples
Definition: vtkFFMPEGVideoSource.h:46
vtkFFMPEGVideoSource::SetVideoCallback
void SetVideoCallback(VideoCallbackType cb, void *clientData)
Definition: vtkFFMPEGVideoSource.h:167
vtkFFMPEGVideoSource::AudioCallbackClientData
void * AudioCallbackClientData
Definition: vtkFFMPEGVideoSource.h:188
vtkFFMPEGVideoSource::FeedCondition
vtkNew< vtkConditionVariable > FeedCondition
Definition: vtkFFMPEGVideoSource.h:202
vtkFFMPEGVideoSource::ReadFrame
void ReadFrame()
vtkMultiThreader::ThreadInfo
This is the structure that is passed to the thread that is created from the SingleMethodExecute,...
Definition: vtkMultiThreader.h:98
vtkFFMPEGVideoSource::SetFrameRate
void SetFrameRate(float rate) override
Request a particular frame rate (default 30 frames per second).
vtkFFMPEGVideoSource::DrainAudioThread
static void * DrainAudioThread(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::Record
void Record() override
Standard VCR functionality: Record incoming video.
vtkFFMPEGVideoSourceAudioCallbackData::SampleRate
int SampleRate
Definition: vtkFFMPEGVideoSource.h:49
vtkFFMPEGVideoSource::AudioCallbackType
std::function< void(vtkFFMPEGVideoSourceAudioCallbackData const &data)> AudioCallbackType
Definition: vtkFFMPEGVideoSource.h:144
vtkFFMPEGVideoSource::FeedThreadId
int FeedThreadId
Definition: vtkFFMPEGVideoSource.h:208
vtkFFMPEGVideoSource::Stop
void Stop() override
Standard VCR functionality: Stop recording or playing.
vtkFFMPEGVideoSource::vtkFFMPEGVideoSource
vtkFFMPEGVideoSource()
vtkFFMPEGVideoSourceAudioCallbackData::NumberOfChannels
int NumberOfChannels
Definition: vtkFFMPEGVideoSource.h:48
vtkFFMPEGVideoSource::Feed
void * Feed(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::Play
void Play() override
Standard VCR functionality: Play recorded video.
vtkFFMPEGVideoSourceAudioCallbackData::Caller
vtkFFMPEGVideoSource * Caller
Definition: vtkFFMPEGVideoSource.h:53
vtkFFMPEGVideoSource::ReleaseSystemResources
void ReleaseSystemResources() override
Free the driver (this is called automatically inside the destructor).
vtkFFMPEGVideoSource::Stereo3D
bool Stereo3D
Definition: vtkFFMPEGVideoSource.h:216
vtkFFMPEGVideoSourceVideoCallbackData
Definition: vtkFFMPEGVideoSource.h:60
vtkFFMPEGVideoSource::Grab
void Grab() override
Grab a single video frame.
vtkFFMPEGVideoSourceAudioCallbackData::DataType
int DataType
Definition: vtkFFMPEGVideoSource.h:50
vtkFFMPEGVideoSource::~vtkFFMPEGVideoSource
~vtkFFMPEGVideoSource() override
vtkFFMPEGVideoSourceVideoCallbackData::LineSize
int LineSize[8]
Definition: vtkFFMPEGVideoSource.h:62
vtkFFMPEGVideoSourceVideoCallbackData::Caller
vtkFFMPEGVideoSource * Caller
Definition: vtkFFMPEGVideoSource.h:64
vtkFFMPEGVideoSourceAudioCallbackData::BytesPerSample
int BytesPerSample
Definition: vtkFFMPEGVideoSource.h:47
vtkFFMPEGVideoSource::InternalGrab
void InternalGrab() override
The internal function which actually does the grab.
vtkFFMPEGVideoSource::SetFrameSize
void SetFrameSize(int x, int y, int z) override
Request a particular frame size (set the third value to 1).
vtkFFMPEGVideoSourceAudioCallbackData
Definition: vtkFFMPEGVideoSource.h:45
vtkNew< vtkConditionVariable >
vtkFFMPEGVideoSource::New
static vtkFFMPEGVideoSource * New()
vtkFFMPEGVideoSourceAudioCallbackData::Packed
bool Packed
Definition: vtkFFMPEGVideoSource.h:51
vtkFFMPEGVideoSource::SetOutputFormat
void SetOutputFormat(int format) override
Request a particular output format (default: VTK_RGB).
vtkFFMPEGVideoSource::Initialize
void Initialize() override
Initialize the driver (this is called automatically when the first grab is done).
vtkConditionVariable
mutual exclusion locking class
Definition: vtkConditionVariable.h:148
vtkNew.h
vtkFFMPEGVideoSource::VideoCallbackClientData
void * VideoCallbackClientData
Definition: vtkFFMPEGVideoSource.h:219
vtkFFMPEGVideoSource::DrainThreadId
int DrainThreadId
Definition: vtkFFMPEGVideoSource.h:198
vtkFFMPEGVideoSource::FeedThread
static void * FeedThread(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::DrainThread
static void * DrainThread(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::FeedAudioMutex
vtkNew< vtkMutexLock > FeedAudioMutex
Definition: vtkFFMPEGVideoSource.h:205
vtkFFMPEGVideoSource::AudioCallback
AudioCallbackType AudioCallback
Definition: vtkFFMPEGVideoSource.h:187
vtkFFMPEGVideoSource::SetAudioCallback
void SetAudioCallback(AudioCallbackType cb, void *clientData)
Definition: vtkFFMPEGVideoSource.h:154
vtkFFMPEGVideoSource::FeedAudioCondition
vtkNew< vtkConditionVariable > FeedAudioCondition
Definition: vtkFFMPEGVideoSource.h:204
vtkFFMPEGVideoSourceVideoCallbackData::Data
unsigned char * Data[8]
Definition: vtkFFMPEGVideoSource.h:63
vtkFFMPEGVideoSource::FileName
char * FileName
Definition: vtkFFMPEGVideoSource.h:210
vtkFFMPEGVideoSource::Internal
vtkFFMPEGVideoSourceInternal * Internal
Definition: vtkFFMPEGVideoSource.h:212
vtkVideoSource
Superclass of video input devices for VTK.
Definition: vtkVideoSource.h:44