1 #ifndef _avidump__hpp__included__
2 #define _avidump__hpp__included__
12 #define REALLY_USE_THREADS 1
16 #define REALLY_USE_THREADS 1
20 #ifdef REALLY_USE_THREADS
22 #include <condition_variable>
28 typedef std::thread thread_class
;
31 * Class of condition variables.
33 typedef std::condition_variable cv_class
;
38 typedef std::mutex mutex_class
;
41 * Class of unique mutexes (for condition variable waiting).
43 typedef std::unique_lock
<std::mutex
> umutex_class
;
55 template<typename T
, typename
... args
>
56 thread_class(T obj
, args
... a
) {}
66 typedef struct mutex_class
79 * Class of condition variables.
86 void wait(umutex_class
& m
) {}
96 * Size of audio buffer (enough to buffer 3 frames).
98 #define AVIDUMPER_AUDIO_BUFFER 4096
101 * Information about frame in AVI.
108 * parameter _flags: Flags for frame.
109 * parameter _type: AVI type for frame (big-endian!).
110 * parameter _offset: Offset of frame from start of MOVI.
111 * parameter _size: Size of frame data.
113 avi_frame(uint32_t _flags
, uint32_t _type
, uint32_t _offset
, uint32_t _size
);
116 * Write the index entry for frame.
118 * parameter buf: Buffer to write to.
120 void write(uint8_t* buf
);
144 * Parameters for AVI dumping.
149 * Zlib compression level (0-9).
151 unsigned compression_level
;
154 * Audio sampling rate to write to AVI.
156 uint32_t audio_sampling_rate
;
159 * Interval of keyframes (WARNING: >1 gives non-keyframes which AVISource() doesn't like).
161 uint32_t keyframe_interval
;
163 * Maximum number of frames per segement (0 => infinite)
165 uint32_t max_frames_per_segment
;
169 * The actual AVI dumper.
174 avidumper(const std::string
& _prefix
, struct avi_info parameters
);
175 ~avidumper() throw();
178 * Waits for the encode thread. Not needed: Operations that need to synchronize synchronize themselves.
180 void wait_idle() throw();
183 * Dump a frame (new segment starts if needed). Pixel byte order is BGRx.
185 * parameter data: The frame data.
186 * parameter width: Width of frame.
187 * parameter height: Height of frame.
188 * parameter fps_n: Numerator of fps value.
189 * parameter fps_d: Denomerator of fps value.
190 * throws std::bad_alloc: Not enough memory.
191 * throws std::runtime_error: Error dumping frame.
193 void on_frame(const uint16_t* data
, uint16_t width
, uint16_t height
, uint32_t fps_n
, uint32_t fps_d
)
194 throw(std::bad_alloc
, std::runtime_error
);
197 * Dump an audio sample
199 * parameter left: Signed sample for left channel (-32768 - 327678).
200 * parameter right: Signed sample for right channel (-32768 - 327678).
201 * throws std::bad_alloc: Not enough memory.
202 * throws std::runtime_error: Error dumping sample.
204 void on_sample(short left
, short right
) throw(std::bad_alloc
, std::runtime_error
);
207 * Notify end of dump.
209 * throws std::bad_alloc: Not enough memory.
210 * throws std::runtime_error: Error closing dump.
212 void on_end() throw(std::bad_alloc
, std::runtime_error
);
215 * Causes current thread to become encode thread. Do not call this, the code internally uses it.
217 * returns: Return status for the thread.
222 * Set capture errored flag.
224 * parameter err: The error message.
226 void set_capture_error(const char* err
) throw();
228 void print_summary(std::ostream
& str
);
229 void on_frame_threaded(const uint16_t* data
, uint16_t width
, uint16_t height
, uint32_t fps_n
, uint32_t fps_d
)
230 throw(std::bad_alloc
, std::runtime_error
);
231 void flush_audio_to(unsigned commit_ptr
);
232 void open_and_write_avi_header(uint16_t width
, uint16_t height
, uint32_t fps_n
, uint32_t fps_d
);
233 void fixup_avi_header_and_close();
234 std::ofstream avi_stream
;
236 std::string capture_error_str
;
239 unsigned compression_level
;
240 uint32_t audio_sampling_rate
;
241 uint32_t keyframe_interval
;
249 uint32_t segment_movi_ptr
;
250 uint32_t segment_frames
;
251 uint32_t segment_samples
;
252 uint32_t segment_last_keyframe
;
253 uint32_t current_segment
;
254 uint32_t fixup_avi_size
;
255 uint32_t fixup_avi_frames
;
256 uint32_t fixup_avi_length
;
257 uint32_t fixup_avi_a_length
;
258 uint32_t fixup_movi_size
;
259 std::list
<avi_frame
> segment_chunks
;
263 uint64_t total_frames
;
264 uint64_t total_samples
;
266 std::vector
<uint8_t> pframe
;
267 std::vector
<uint8_t> tframe
;
268 std::vector
<uint8_t> cframe
;
269 short audio_buffer
[AVIDUMPER_AUDIO_BUFFER
];
270 unsigned audio_put_ptr
;
271 unsigned audio_get_ptr
;
272 volatile unsigned audio_commit_ptr
; //Protected by frame_mutex.
273 //Multithreading stuff.
274 thread_class
* frame_thread
;
276 mutex_class frame_mutex
;
277 const uint16_t* mt_data
;
278 volatile uint16_t mt_width
;
279 volatile uint16_t mt_height
;
280 volatile uint32_t mt_fps_n
;
281 volatile uint32_t mt_fps_d
;
282 volatile bool sigquit
;