5 #include "filethread.h"
16 FileThread::FileThread(File *file, int do_audio, int do_video)
25 FileThread::~FileThread()
33 void FileThread::reset()
44 void FileThread::create_objects(File *file,
49 this->do_audio = do_audio;
50 this->do_video = do_video;
51 file_lock = new Mutex("FileThread::file_lock");
55 void FileThread::delete_objects()
59 for(int i = 0; i < ring_buffers; i++)
61 delete output_lock[i];
63 delete [] output_lock;
68 for(int i = 0; i < ring_buffers; i++)
77 delete [] last_buffer;
80 delete [] output_size;
85 void FileThread::run()
92 output_lock[local_buffer]->lock("FileThread::run 1");
98 if(!last_buffer[local_buffer])
100 if(output_size[local_buffer])
102 file_lock->lock("FileThread::run 2");
105 result = file->write_samples(audio_buffer[local_buffer],
106 output_size[local_buffer]);
114 for(j = 0; j < file->asset->layers && !result; j++)
115 for(i = 0; i < output_size[local_buffer] && !result; i++)
116 result = file->write_compressed_frame(video_buffer[local_buffer][j][i]);
120 result = file->write_frames(video_buffer[local_buffer],
121 output_size[local_buffer]);
126 return_value = result;
131 output_size[local_buffer] = 0;
136 input_lock[local_buffer]->unlock();
138 if(local_buffer >= ring_buffers) local_buffer = 0;
144 int FileThread::stop_writing()
147 int i, buffer, layer, frame;
150 input_lock[current_buffer]->lock("FileThread::stop_writing 1");
152 last_buffer[current_buffer] = 1;
154 for(i = 0; i < ring_buffers; i++)
155 output_lock[i]->unlock();
159 // wait for thread to finish
163 file_lock->lock("FileThread::stop_writing 2");
166 for(buffer = 0; buffer < ring_buffers; buffer++)
168 for(i = 0; i < file->asset->channels; i++)
169 delete [] audio_buffer[buffer][i];
170 delete [] audio_buffer[buffer];
172 delete [] audio_buffer;
176 // printf("FileThread::stop_writing %d %d %d %d\n",
179 // file->asset->layers,
183 for(buffer = 0; buffer < ring_buffers; buffer++)
185 for(layer = 0; layer < file->asset->layers; layer++)
187 for(frame = 0; frame < buffer_size; frame++)
189 delete video_buffer[buffer][layer][frame];
191 delete [] video_buffer[buffer][layer];
193 delete [] video_buffer[buffer];
195 delete [] video_buffer;
203 int FileThread::start_writing(long buffer_size,
209 int buffer, layer, frame;
210 long bytes_per_frame;
212 this->ring_buffers = ring_buffers;
213 this->buffer_size = buffer_size;
214 this->color_model = color_model;
215 this->compressed = compressed;
216 this->current_buffer = ring_buffers - 1;
220 file_lock->lock("FileThread::start_writing 1");
225 // Buffer is swapped before first get
226 last_buffer = new int[ring_buffers];
227 output_size = new long[ring_buffers];
230 output_lock = new Condition*[ring_buffers];
231 input_lock = new Condition*[ring_buffers];
232 for(int i = 0; i < ring_buffers; i++)
234 output_lock[i] = new Condition(0, "FileThread::output_lock");
235 input_lock[i] = new Condition(1, "FileThread::input_lock");
244 audio_buffer = new double**[ring_buffers];
245 for(buffer = 0; buffer < ring_buffers; buffer++)
247 audio_buffer[buffer] = new double*[file->asset->channels];
249 for(int channel = 0; channel < file->asset->channels; channel++)
251 audio_buffer[buffer][channel] = new double[buffer_size];
258 this->color_model = color_model;
259 bytes_per_frame = VFrame::calculate_data_size(file->asset->width,
264 video_buffer = new VFrame***[ring_buffers];
265 // printf("FileThread::start_writing 1 %d %d %d %p\n",
267 // file->asset->layers,
270 for(buffer = 0; buffer < ring_buffers; buffer++)
272 video_buffer[buffer] = new VFrame**[file->asset->layers];
273 for(layer = 0; layer < file->asset->layers; layer++)
275 video_buffer[buffer][layer] = new VFrame*[buffer_size];
276 for(frame = 0; frame < buffer_size; frame++)
279 video_buffer[buffer][layer][frame] = new VFrame;
282 video_buffer[buffer][layer][frame] =
287 // printf("FileThread::start_writing 4 %d %d %d %p\n",
291 // video_buffer[buffer][layer]);
299 for(int i = 0; i < ring_buffers; i++)
310 int64_t FileThread::get_memory_usage()
312 frame_lock->lock("FileThread::get_memory_usage");
314 // 2.0 to 2.1 Dummy place-holder patch for until variables better implemented
315 frame_lock->unlock();
320 double** FileThread::get_audio_buffer()
324 input_lock[current_buffer]->lock("FileThread::get_audio_buffer");
325 return audio_buffer[current_buffer];
328 VFrame*** FileThread::get_video_buffer()
332 input_lock[current_buffer]->lock("FileThread::get_video_buffer");
333 return video_buffer[current_buffer];
336 int FileThread::write_buffer(long size)
338 output_size[current_buffer] = size;
340 // unlock the output lock
341 output_lock[current_buffer]->unlock();
346 int FileThread::swap_buffer()
349 if(current_buffer >= ring_buffers) current_buffer = 0;