| 1 |
#ifndef CAPPROC_H |
| 2 |
#define CAPPROC_H |
| 3 |
|
| 4 |
#include <avm_stl.h> |
| 5 |
#include <avm_locker.h> |
| 6 |
#include <infotypes.h> |
| 7 |
|
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
class ClosedCaption; |
| 11 |
class frame_allocator; |
| 12 |
class dsp; |
| 13 |
class v4lxif; |
| 14 |
|
| 15 |
enum Sound_Freqs { NoAudio = -1, F48, F44, F32, F22, F16, F12, F11, F8 }; |
| 16 |
enum Sample_Sizes { S16, S8 }; |
| 17 |
enum Sound_Chans { Mono, Stereo, Lang1, Lang2 }; |
| 18 |
enum Colorspaces { cspYV12, cspYUY2, cspRGB15, cspRGB24, cspRGB32, cspI420 }; |
| 19 |
|
| 20 |
enum Resolutions |
| 21 |
{ |
| 22 |
WNONE =-1, |
| 23 |
W160 =0, |
| 24 |
W192 =1, |
| 25 |
W320 =2, |
| 26 |
W384 =3, |
| 27 |
W400 =4, |
| 28 |
W512 =5, |
| 29 |
W576 =6, |
| 30 |
W640 =7, |
| 31 |
W768 =8, |
| 32 |
}; |
| 33 |
|
| 34 |
struct chunk |
| 35 |
{ |
| 36 |
char* data; |
| 37 |
double timestamp; |
| 38 |
int size; |
| 39 |
}; |
| 40 |
|
| 41 |
struct CaptureProgress |
| 42 |
{ |
| 43 |
static const int HISTORY_SIZE = 256; |
| 44 |
|
| 45 |
avm::PthreadMutex mutex; |
| 46 |
int video_frame_len; |
| 47 |
int buffer_len; |
| 48 |
int max_frames; |
| 49 |
|
| 50 |
int64_t file_size; |
| 51 |
int64_t enc_time; |
| 52 |
int64_t video_bytes; |
| 53 |
int64_t audio_bytes; |
| 54 |
double video_time; |
| 55 |
double audio_time; |
| 56 |
int video_frames; |
| 57 |
int synch_fix; |
| 58 |
int video_frame_sizes[HISTORY_SIZE]; |
| 59 |
int video_frame_head; |
| 60 |
int buffer_occupancies[HISTORY_SIZE]; |
| 61 |
int buffer_head; |
| 62 |
int dropped_cap; |
| 63 |
int dropped_enc; |
| 64 |
|
| 65 |
const char* filename; |
| 66 |
float framerate; |
| 67 |
int xdim; |
| 68 |
int ydim; |
| 69 |
int audio_freq; |
| 70 |
int audio_ssize; |
| 71 |
int audio_channels; |
| 72 |
|
| 73 |
double audio_error,video_error,timestamp_shift; |
| 74 |
|
| 75 |
CaptureProgress() : video_frame_len(0), buffer_len(0), max_frames(0), |
| 76 |
file_size(0), enc_time(0), |
| 77 |
video_bytes(0), audio_bytes(0), |
| 78 |
video_time(0), audio_time(0), |
| 79 |
video_frames(0), synch_fix(0), |
| 80 |
video_frame_head(0), buffer_head(0), |
| 81 |
dropped_cap(0), dropped_enc(0), |
| 82 |
audio_error(0),video_error(0), |
| 83 |
timestamp_shift(0) |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
~CaptureProgress() {} |
| 88 |
void lock() { mutex.Lock(); } |
| 89 |
void unlock() { mutex.Unlock(); } |
| 90 |
void init(const char* fn, float fps, int xd, int yd, int freq, int ssize, int channels, int frames) |
| 91 |
{ |
| 92 |
lock(); |
| 93 |
filename=fn; |
| 94 |
framerate=fps; |
| 95 |
xdim=xd; |
| 96 |
ydim=yd; |
| 97 |
audio_freq=freq; |
| 98 |
audio_ssize=ssize; |
| 99 |
audio_channels=channels; |
| 100 |
max_frames=frames; |
| 101 |
memset(video_frame_sizes, 0, sizeof(video_frame_sizes)); |
| 102 |
unlock(); |
| 103 |
} |
| 104 |
void update(long long etime, double vidtime, double audtime, |
| 105 |
long long audbytes, long long fsize, int synchfix, |
| 106 |
int vframe, int bufstatus, |
| 107 |
int drop_cap, int drop_enc, |
| 108 |
const char* fn) |
| 109 |
{ |
| 110 |
lock(); |
| 111 |
enc_time = etime; |
| 112 |
video_time = vidtime; |
| 113 |
audio_time = audtime; |
| 114 |
if (vframe > 0) |
| 115 |
video_bytes += (vframe & ~0x40000000); |
| 116 |
audio_bytes = audbytes; |
| 117 |
file_size = fsize; |
| 118 |
synch_fix = synchfix; |
| 119 |
if (vframe >= 0) |
| 120 |
{ |
| 121 |
video_frames++; |
| 122 |
video_frame_sizes[video_frame_head]=vframe; |
| 123 |
video_frame_head++; |
| 124 |
video_frame_head %= HISTORY_SIZE; |
| 125 |
if (video_frame_len < HISTORY_SIZE) |
| 126 |
video_frame_len++; |
| 127 |
buffer_occupancies[buffer_head]=bufstatus; |
| 128 |
buffer_head++; |
| 129 |
buffer_head %= HISTORY_SIZE; |
| 130 |
if (buffer_len < HISTORY_SIZE) |
| 131 |
buffer_len++; |
| 132 |
dropped_cap = drop_cap; |
| 133 |
dropped_enc = drop_enc; |
| 134 |
} |
| 135 |
filename = fn; |
| 136 |
unlock(); |
| 137 |
} |
| 138 |
void setVideoError(double val){ |
| 139 |
lock(); |
| 140 |
video_error=val; |
| 141 |
unlock(); |
| 142 |
} |
| 143 |
void setAudioError(double val){ |
| 144 |
lock(); |
| 145 |
audio_error=val; |
| 146 |
unlock(); |
| 147 |
} |
| 148 |
void setTimestampShift(double val){ |
| 149 |
lock(); |
| 150 |
timestamp_shift=val; |
| 151 |
unlock(); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
struct CaptureConfig |
| 156 |
{ |
| 157 |
avm::string filename; |
| 158 |
bool never_overwrite; |
| 159 |
int segment_size; |
| 160 |
// Segmentation is implicit finishing one |
| 161 |
// file and starting next one when old file |
| 162 |
// grows to specified size |
| 163 |
//Kbytes |
| 164 |
//-1 for no segmentation |
| 165 |
avm::VideoEncoderInfo codec; |
| 166 |
// compressor-dependent |
| 167 |
// for IV 5.0 Quick it's ignored |
| 168 |
int frequency; |
| 169 |
// frequency ID |
| 170 |
//-1 for no audio |
| 171 |
avm::string audio_device; |
| 172 |
int audio_compressor; |
| 173 |
int audio_bitrate; |
| 174 |
int samplesize; |
| 175 |
int chan; |
| 176 |
int res_w; |
| 177 |
int res_h; |
| 178 |
int over_t, over_b, over_l, over_r; |
| 179 |
int timelimit; //in seconds. -1 if no limit |
| 180 |
int sizelimit; //in Kbytes. -1 if no limit |
| 181 |
float fps; |
| 182 |
bool deinterlace; |
| 183 |
Colorspaces colorspace; |
| 184 |
void load(); |
| 185 |
void setNamedCodec(avm::string named_codec); |
| 186 |
void setDimensions(Resolutions res); |
| 187 |
void setChannels(Sound_Chans c); |
| 188 |
void setSamplesize(Sample_Sizes ss); |
| 189 |
void setFrequency(Sound_Freqs freq); |
| 190 |
void setFilename(avm::string fname) { filename=fname; }; |
| 191 |
}; |
| 192 |
|
| 193 |
template <class Type> class lockqring : public avm::qring<Type> |
| 194 |
{ |
| 195 |
avm::PthreadMutex mutex; |
| 196 |
public: |
| 197 |
lockqring<Type>(uint_t cap) : avm::qring<Type>(cap) {} |
| 198 |
void push(const Type& m) { avm::Locker locker(mutex); avm::qring<Type>::push(m); } |
| 199 |
void pop() { avm::Locker locker(mutex); avm::qring<Type>::pop(); } |
| 200 |
}; |
| 201 |
|
| 202 |
class CaptureProcess |
| 203 |
{ |
| 204 |
public: |
| 205 |
CaptureProcess(v4lxif* v4l, |
| 206 |
CaptureConfig* conf, |
| 207 |
ClosedCaption* ccap); |
| 208 |
~CaptureProcess(); |
| 209 |
|
| 210 |
void getState(int& finished, avm::string& e) |
| 211 |
{ |
| 212 |
finished = m_quit; |
| 213 |
e = error; |
| 214 |
} |
| 215 |
|
| 216 |
void setMessenger(CaptureProgress* pProgress); |
| 217 |
|
| 218 |
void segment() { segment_flag = 1; } |
| 219 |
void setSegmentName(avm::string name) { segmenting_name=name; } |
| 220 |
|
| 221 |
protected: |
| 222 |
void* vidcap(); |
| 223 |
void* audcap(); |
| 224 |
void* writer(); |
| 225 |
static void* vidcap_starter(void* arg) { return ((CaptureProcess*)arg)->vidcap(); } |
| 226 |
static void* audcap_starter(void* arg) { return ((CaptureProcess*)arg)->audcap(); } |
| 227 |
static void* writer_starter(void* arg) { return ((CaptureProcess*)arg)->writer(); } |
| 228 |
static void capwriter(void*, const char*); |
| 229 |
v4lxif* m_v4l; |
| 230 |
avm::PthreadTask* m_pVidc; |
| 231 |
avm::PthreadTask* m_pAudc; |
| 232 |
avm::PthreadTask* m_pWriter; |
| 233 |
avm::PthreadTask* m_pSub; |
| 234 |
lockqring<chunk> m_qvid; |
| 235 |
lockqring<chunk> m_qaud; |
| 236 |
int m_quit; |
| 237 |
int cnt; |
| 238 |
int cap_drop; |
| 239 |
int comp_drop; |
| 240 |
int ccfd; |
| 241 |
int64_t starttime; |
| 242 |
int64_t lastcctime; |
| 243 |
avm::string lastccstring; |
| 244 |
|
| 245 |
CaptureProgress* m_pProgress; |
| 246 |
ClosedCaption* m_ccap; |
| 247 |
frame_allocator* m_pAllocator; |
| 248 |
CaptureConfig m_conf; |
| 249 |
dsp* m_pDsp; |
| 250 |
|
| 251 |
avm::string error; |
| 252 |
avm::string segmenting_name; |
| 253 |
int segment_flag; |
| 254 |
|
| 255 |
int vid_clear; |
| 256 |
int aud_clear; |
| 257 |
int audioblock; |
| 258 |
|
| 259 |
private: |
| 260 |
avm::string usedname; |
| 261 |
avm::string find_filename(avm::string checkname); |
| 262 |
}; |
| 263 |
|
| 264 |
#endif /* CAPPROC_H */ |