/
ofxVideoRecorder.h
209 lines (180 loc) · 6.14 KB
/
ofxVideoRecorder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include "ofMain.h"
#include "Poco/Condition.h"
#include <set>
//--------------------------------------------------------------
//--------------------------------------------------------------
template <typename T>
struct lockFreeQueue {
lockFreeQueue(){
list.push_back(T());
iHead = list.begin();
iTail = list.end();
}
void Produce(const T& t){
list.push_back(t);
iTail = list.end();
list.erase(list.begin(), iHead);
}
bool Consume(T& t){
typename TList::iterator iNext = iHead;
++iNext;
if (iNext != iTail)
{
iHead = iNext;
t = *iHead;
return true;
}
return false;
}
int size() { return distance(iHead,iTail)-1; }
typename std::list<T>::iterator getHead() { return iHead; }
typename std::list<T>::iterator getTail() { return iTail; }
private:
typedef std::list<T> TList;
TList list;
typename TList::iterator iHead, iTail;
};
class execThread : public ofThread{
public:
execThread();
void setup(string command);
void threadedFunction();
bool isInitialized() { return initialized; }
private:
string execCommand;
bool initialized;
};
struct audioFrameShort {
short * data;
int size;
};
//--------------------------------------------------------------
//--------------------------------------------------------------
class ofxVideoDataWriterThread : public ofThread {
public:
ofxVideoDataWriterThread();
// void setup(ofFile *file, lockFreeQueue<ofPixels *> * q);
void setup(string filePath, lockFreeQueue<ofPixels *> * q);
void threadedFunction();
void signal();
void setPipeNonBlocking();
bool isWriting() { return bIsWriting; }
void close() { bClose = true; stopThread(); signal(); }
bool bNotifyError;
private:
ofMutex conditionMutex;
Poco::Condition condition;
// ofFile * writer;
string filePath;
int fd;
lockFreeQueue<ofPixels *> * queue;
bool bIsWriting;
bool bClose;
};
//--------------------------------------------------------------
//--------------------------------------------------------------
class ofxAudioDataWriterThread : public ofThread {
public:
ofxAudioDataWriterThread();
// void setup(ofFile *file, lockFreeQueue<audioFrameShort *> * q);
void setup(string filePath, lockFreeQueue<audioFrameShort *> * q);
void threadedFunction();
void signal();
void setPipeNonBlocking();
bool isWriting() { return bIsWriting; }
void close() { bClose = true; stopThread(); signal(); }
bool bNotifyError;
private:
ofMutex conditionMutex;
Poco::Condition condition;
// ofFile * writer;
string filePath;
int fd;
lockFreeQueue<audioFrameShort *> * queue;
bool bIsWriting;
bool bClose;
};
//--------------------------------------------------------------
//--------------------------------------------------------------
class ofxVideoRecorderOutputFileCompleteEventArgs
: public ofEventArgs
{
public:
string fileName;
};
//--------------------------------------------------------------
//--------------------------------------------------------------
class ofxVideoRecorder : public ofThread
{
public:
ofxVideoRecorder();
void threadedFunction();
ofEvent<ofxVideoRecorderOutputFileCompleteEventArgs> outputFileCompleteEvent;
bool setup(string fname, int w, int h, float fps, int sampleRate=0, int channels=0, bool sysClockSync=false, bool silent=false);
bool setupCustomOutput(int w, int h, float fps, string outputString, bool sysClockSync=false, bool silent=false);
bool setupCustomOutput(int w, int h, float fps, int sampleRate, int channels, string outputString, bool sysClockSync=false, bool silent=false);
bool addFrame(const ofPixels &pixels);
void addAudioSamples(float * samples, int bufferSize, int numChannels);
void start();
void close();
void setPaused(bool bPause);
bool hasVideoError();
bool hasAudioError();
void setFfmpegLocation(string loc) { ffmpegLocation = loc; }
void setVideoCodec(string codec) { videoCodec = codec; }
void setAudioCodec(string codec) { audioCodec = codec; }
void setVideoBitrate(string bitrate) { videoBitrate = bitrate; }
void setAudioBitrate(string bitrate) { audioBitrate = bitrate; }
void setPixelFormat( string pixelF){ //rgb24 || gray, default is rgb24
pixelFormat = pixelF;
};
void setOutputPixelFormat(string pixelF) {
outputPixelFormat = pixelF;
}
unsigned long long getNumVideoFramesRecorded() { return videoFramesRecorded; }
unsigned long long getNumAudioSamplesRecorded() { return audioSamplesRecorded; }
int getVideoQueueSize(){ return frames.size(); }
int getAudioQueueSize(){ return audioFrames.size(); }
bool isInitialized(){ return bIsInitialized; }
bool isRecording() { return bIsRecording; };
bool isPaused() { return bIsPaused; };
bool isSyncAgainstSysClock() { return bSysClockSync; };
string getMoviePath(){ return moviePath; }
int getWidth(){return width;}
int getHeight(){return height;}
private:
string fileName;
string moviePath;
string videoPipePath, audioPipePath;
string ffmpegLocation;
string videoCodec, audioCodec, videoBitrate, audioBitrate, pixelFormat, outputPixelFormat;
int width, height, sampleRate, audioChannels;
float frameRate;
bool bIsInitialized;
bool bRecordAudio;
bool bRecordVideo;
bool bIsRecording;
bool bIsPaused;
bool bFinishing;
bool bIsSilent;
bool bSysClockSync;
float startTime;
float recordingDuration;
float totalRecordingDuration;
float systemClock();
lockFreeQueue<ofPixels *> frames;
lockFreeQueue<audioFrameShort *> audioFrames;
unsigned long long audioSamplesRecorded;
unsigned long long videoFramesRecorded;
ofxVideoDataWriterThread videoThread;
ofxAudioDataWriterThread audioThread;
execThread ffmpegThread;
// ofFile videoPipe, audioPipe;
int videoPipeFd, audioPipeFd;
int pipeNumber;
static set<int> openPipes;
static int requestPipeNumber();
static void retirePipeNumber(int num);
void outputFileComplete();
};