VTK
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 "vtkVideoSource.h"
35 #include "vtkMultiThreader.h" // for ivar
36 #include "vtkNew.h" // for ivar
37 #include <functional> // for audio callback
38 
39 class vtkFFMPEGVideoSourceInternal;
40 
42 class vtkMutexLock;
44 
45 // audio callback struct, outside the class so that we
46 // can forward ref it
48 {
53  int DataType;
54  bool Packed;
55  unsigned char** Data;
57  void *ClientData;
58 };
59 
60 class VTKIOFFMPEG_EXPORT vtkFFMPEGVideoSource : public vtkVideoSource
61 {
62 public:
65 
69  void Record() override;
70 
74  void Play() override;
75 
79  void Stop() override;
80 
84  void Grab() override;
85 
87 
90  void SetFrameSize(int x, int y, int z) override;
91  void SetFrameSize(int dim[3]) override {
92  this->SetFrameSize(dim[0], dim[1], dim[2]); };
94 
98  void SetFrameRate(float rate) override;
99 
103  void SetOutputFormat(int format) override;
104 
109  void Initialize() override;
110 
115  void ReleaseSystemResources() override;
116 
118 
121  vtkSetStringMacro(FileName);
122  vtkGetStringMacro(FileName);
124 
130  void InternalGrab() override;
131 
132  // is the video at the end of file?
133  // Usefull for while loops
134  vtkGetMacro(EndOfFile,bool);
135 
136  // we do not use Invoke Observers here because this callback
137  // will happen in a different thread that could conflict
138  // with events from other threads. In this function you should
139  // not block the thread (for example waiting for audio to play)
140  // instead you should have enough buffering that you can consume
141  // the provided data and return. Typically even 1 second of
142  // buffer storage is enough to prevent blocking.
144  void SetAudioCallback(AudioCallbackType cb, void *clientData)
145  {
146  this->AudioCallback = cb;
147  this->AudioCallbackClientData = clientData;
148  }
149 
151 
156  vtkSetMacro(DecodingThreads, int);
157  vtkGetMacro(DecodingThreads, int);
159 
160 protected:
163 
166 
168 
169  static void *DrainAudioThread(
173 
174  static void *DrainThread(
178 
179  bool EndOfFile;
180 
185  static void *FeedThread(
189 
190  char *FileName;
191 
192  vtkFFMPEGVideoSourceInternal *Internal;
193 
194  void ReadFrame();
195 
196 private:
198  void operator=(const vtkFFMPEGVideoSource&) = delete;
199 };
200 
201 #endif
vtkFFMPEGVideoSource::DecodingThreads
int DecodingThreads
Definition: vtkFFMPEGVideoSource.h:167
vtkFFMPEGVideoSource::DrainAudioThreadId
int DrainAudioThreadId
Definition: vtkFFMPEGVideoSource.h:172
vtkMultiThreader.h
vtkX3D::function
@ function
Definition: vtkX3D.h:249
vtkFFMPEGVideoSource::DrainAudio
void * DrainAudio(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::~vtkFFMPEGVideoSource
~vtkFFMPEGVideoSource()
vtkFFMPEGVideoSource::FeedMutex
vtkNew< vtkMutexLock > FeedMutex
Definition: vtkFFMPEGVideoSource.h:182
vtkFFMPEGVideoSource::Drain
void * Drain(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSourceAudioCallbackData::Data
unsigned char ** Data
Definition: vtkFFMPEGVideoSource.h:55
vtkX3D::data
@ data
Definition: vtkX3D.h:315
vtkMutexLock
mutual exclusion locking class
Definition: vtkMutexLock.h:81
vtkFFMPEGVideoSource::SetFrameSize
void SetFrameSize(int dim[3]) override
Definition: vtkFFMPEGVideoSource.h:91
vtkFFMPEGVideoSource
Reader for ffmpeg supported formats.
Definition: vtkFFMPEGVideoSource.h:61
vtkFFMPEGVideoSource::EndOfFile
bool EndOfFile
Definition: vtkFFMPEGVideoSource.h:179
vtkVideoSource.h
vtkFFMPEGVideoSourceAudioCallbackData::ClientData
void * ClientData
Definition: vtkFFMPEGVideoSource.h:57
vtkFFMPEGVideoSourceAudioCallbackData::NumberOfSamples
int NumberOfSamples
Definition: vtkFFMPEGVideoSource.h:49
vtkFFMPEGVideoSource::AudioCallbackClientData
void * AudioCallbackClientData
Definition: vtkFFMPEGVideoSource.h:165
vtkFFMPEGVideoSource::FeedCondition
vtkNew< vtkConditionVariable > FeedCondition
Definition: vtkFFMPEGVideoSource.h:181
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:104
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:52
vtkFFMPEGVideoSource::FeedThreadId
int FeedThreadId
Definition: vtkFFMPEGVideoSource.h:188
vtkFFMPEGVideoSource::Stop
void Stop() override
Standard VCR functionality: Stop recording or playing.
vtkFFMPEGVideoSource::vtkFFMPEGVideoSource
vtkFFMPEGVideoSource()
vtkFFMPEGVideoSourceAudioCallbackData::NumberOfChannels
int NumberOfChannels
Definition: vtkFFMPEGVideoSource.h:51
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:56
vtkFFMPEGVideoSource::ReleaseSystemResources
void ReleaseSystemResources() override
Free the driver (this is called automatically inside the destructor).
vtkFFMPEGVideoSource::Grab
void Grab() override
Grab a single video frame.
vtkFFMPEGVideoSourceAudioCallbackData::DataType
int DataType
Definition: vtkFFMPEGVideoSource.h:53
vtkFFMPEGVideoSourceAudioCallbackData::BytesPerSample
int BytesPerSample
Definition: vtkFFMPEGVideoSource.h:50
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:48
vtkNew< vtkConditionVariable >
vtkFFMPEGVideoSource::New
static vtkFFMPEGVideoSource * New()
vtkFFMPEGVideoSourceAudioCallbackData::Packed
bool Packed
Definition: vtkFFMPEGVideoSource.h:54
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:150
vtkNew.h
vtkVideoSource::SetFrameSize
virtual void SetFrameSize(int x, int y, int z)
Set the full-frame size.
vtkFFMPEGVideoSource::AudioCallbackType
std::function< void(vtkFFMPEGVideoSourceAudioCallbackData &data)> AudioCallbackType
Definition: vtkFFMPEGVideoSource.h:134
vtkFFMPEGVideoSource::DrainThreadId
int DrainThreadId
Definition: vtkFFMPEGVideoSource.h:177
vtkFFMPEGVideoSource::FeedThread
static void * FeedThread(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::DrainThread
static void * DrainThread(vtkMultiThreader::ThreadInfo *data)
vtkFFMPEGVideoSource::FeedAudioMutex
vtkNew< vtkMutexLock > FeedAudioMutex
Definition: vtkFFMPEGVideoSource.h:184
vtkFFMPEGVideoSource::AudioCallback
AudioCallbackType AudioCallback
Definition: vtkFFMPEGVideoSource.h:164
vtkFFMPEGVideoSource::SetAudioCallback
void SetAudioCallback(AudioCallbackType cb, void *clientData)
Definition: vtkFFMPEGVideoSource.h:144
vtkFFMPEGVideoSource::FeedAudioCondition
vtkNew< vtkConditionVariable > FeedAudioCondition
Definition: vtkFFMPEGVideoSource.h:183
vtkFFMPEGVideoSource::FileName
char * FileName
Definition: vtkFFMPEGVideoSource.h:190
vtkFFMPEGVideoSource::Internal
vtkFFMPEGVideoSourceInternal * Internal
Definition: vtkFFMPEGVideoSource.h:192
vtkVideoSource
Superclass of video input devices for VTK.
Definition: vtkVideoSource.h:44