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:
63  static vtkFFMPEGVideoSource *New();
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(
171  void *DrainAudio(vtkMultiThreader::ThreadInfo *data);
173 
174  static void *DrainThread(
176  void *Drain(vtkMultiThreader::ThreadInfo *data);
178 
179  bool EndOfFile;
180 
185  static void *FeedThread(
187  void *Feed(vtkMultiThreader::ThreadInfo *data);
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
vtkVideoSource::Play
virtual void Play()
Play through the 'tape' sequentially at the specified frame rate.
vtkFFMPEGVideoSource::DecodingThreads
int DecodingThreads
Definition: vtkFFMPEGVideoSource.h:167
vtkFFMPEGVideoSource::DrainAudioThreadId
int DrainAudioThreadId
Definition: vtkFFMPEGVideoSource.h:172
vtkMultiThreader.h
vtkX3D::function
Definition: vtkX3D.h:249
vtkFFMPEGVideoSource::FeedMutex
vtkNew< vtkMutexLock > FeedMutex
Definition: vtkFFMPEGVideoSource.h:182
vtkFFMPEGVideoSourceAudioCallbackData::Data
unsigned char ** Data
Definition: vtkFFMPEGVideoSource.h:55
vtkX3D::data
Definition: vtkX3D.h:315
vtkMutexLock
mutual exclusion locking class
Definition: vtkMutexLock.h:80
vtkFFMPEGVideoSource::SetFrameSize
void SetFrameSize(int dim[3]) override
Definition: vtkFFMPEGVideoSource.h:91
vtkVideoSource::InternalGrab
virtual void InternalGrab()
The internal function which actually does the grab.
vtkFFMPEGVideoSource
Reader for ffmpeg supported formats.
Definition: vtkFFMPEGVideoSource.h:60
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
vtkVideoSource::Stop
virtual void Stop()
Stop recording or playing.
vtkFFMPEGVideoSource::FeedCondition
vtkNew< vtkConditionVariable > FeedCondition
Definition: vtkFFMPEGVideoSource.h:181
vtkMultiThreader::ThreadInfo
This is the structure that is passed to the thread that is created from the SingleMethodExecute,...
Definition: vtkMultiThreader.h:103
vtkFFMPEGVideoSourceAudioCallbackData::SampleRate
int SampleRate
Definition: vtkFFMPEGVideoSource.h:52
vtkFFMPEGVideoSource::FeedThreadId
int FeedThreadId
Definition: vtkFFMPEGVideoSource.h:188
vtkFFMPEGVideoSourceAudioCallbackData::NumberOfChannels
int NumberOfChannels
Definition: vtkFFMPEGVideoSource.h:51
vtkFFMPEGVideoSourceAudioCallbackData::Caller
vtkFFMPEGVideoSource * Caller
Definition: vtkFFMPEGVideoSource.h:56
vtkVideoSource::ReleaseSystemResources
virtual void ReleaseSystemResources()
Release the video driver.
vtkFFMPEGVideoSourceAudioCallbackData::DataType
int DataType
Definition: vtkFFMPEGVideoSource.h:53
vtkVideoSource::New
static vtkVideoSource * New()
vtkFFMPEGVideoSourceAudioCallbackData::BytesPerSample
int BytesPerSample
Definition: vtkFFMPEGVideoSource.h:50
vtkFFMPEGVideoSourceAudioCallbackData
Definition: vtkFFMPEGVideoSource.h:47
vtkNew< vtkConditionVariable >
vtkVideoSource::SetOutputFormat
virtual void SetOutputFormat(int format)
Set the output format.
vtkVideoSource::Grab
virtual void Grab()
Grab a single video frame.
vtkFFMPEGVideoSourceAudioCallbackData::Packed
bool Packed
Definition: vtkFFMPEGVideoSource.h:54
vtkConditionVariable
mutual exclusion locking class
Definition: vtkConditionVariable.h:149
vtkVideoSource::SetFrameRate
virtual void SetFrameRate(float rate)
Request a particular frame rate (default 30 frames per second).
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
vtkVideoSource::Record
virtual void Record()
Record incoming video at the specified FrameRate.
vtkFFMPEGVideoSource::DrainThreadId
int DrainThreadId
Definition: vtkFFMPEGVideoSource.h:177
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
vtkVideoSource::Initialize
virtual void Initialize()
Initialize the hardware.
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:43