4 #include "filethread.h"
10 FileThread::FileThread(File *file, int do_audio, int do_video)
22 FileThread::FileThread(File *file,
39 void FileThread::create_objects(File *file,
48 this->do_audio = do_audio;
49 this->do_video = do_video;
50 file_lock = new Mutex("FileThread::file_lock");
51 for(int i = 0; i < RING_BUFFERS; i++)
53 output_lock[i] = new Condition(0, "FileThread::output_lock");
54 input_lock[i] = new Condition(1, "FileThread::input_lock");
56 this->ring_buffers = ring_buffers;
57 this->buffer_size = buffer_size;
58 this->color_model = color_model;
59 this->compressed = compressed;
60 // Buffer is swapped before first get
61 current_buffer = ring_buffers - 1;
66 FileThread::~FileThread()
69 for(int i = 0; i < RING_BUFFERS; i++)
71 delete output_lock[i];
76 void FileThread::run()
81 //printf("FileThread::run 1 %d\n", getpid());
84 output_lock[local_buffer]->lock("FileThread::run 1");
89 if(!last_buffer[local_buffer])
91 if(output_size[local_buffer])
93 file_lock->lock("FileThread::run 2");
96 result = file->write_samples(audio_buffer[local_buffer],
97 output_size[local_buffer]);
105 for(j = 0; j < file->asset->layers && !result; j++)
106 for(i = 0; i < output_size[local_buffer] && !result; i++)
107 result = file->write_compressed_frame(video_buffer[local_buffer][j][i]);
111 result = file->write_frames(video_buffer[local_buffer],
112 output_size[local_buffer]);
117 return_value = result;
122 output_size[local_buffer] = 0;
127 //printf("FileThread::run %lld\n", timer.get_difference());
128 input_lock[local_buffer]->unlock();
130 if(local_buffer >= ring_buffers) local_buffer = 0;
136 int FileThread::stop_writing()
138 int i, buffer, layer, frame;
141 input_lock[current_buffer]->lock("FileThread::stop_writing 1");
143 last_buffer[current_buffer] = 1;
145 for(i = 0; i < ring_buffers; i++)
146 output_lock[i]->unlock();
150 // wait for thread to finish
154 file_lock->lock("FileThread::stop_writing 2");
157 for(buffer = 0; buffer < ring_buffers; buffer++)
159 for(i = 0; i < file->asset->channels; i++)
160 delete [] audio_buffer[buffer][i];
161 delete [] audio_buffer[buffer];
165 // printf("FileThread::stop_writing %d %d %d %d\n",
168 // file->asset->layers,
172 for(buffer = 0; buffer < ring_buffers; buffer++)
174 for(layer = 0; layer < file->asset->layers; layer++)
176 for(frame = 0; frame < buffer_size; frame++)
178 delete video_buffer[buffer][layer][frame];
180 delete [] video_buffer[buffer][layer];
182 delete [] video_buffer[buffer];
189 int FileThread::start_writing()
191 start_writing(buffer_size,
198 int FileThread::start_writing(long buffer_size,
204 int buffer, layer, frame;
205 long bytes_per_frame;
207 this->ring_buffers = ring_buffers;
208 this->buffer_size = buffer_size;
210 file_lock->lock("FileThread::start_writing 1");
213 for(buffer = 0; buffer < ring_buffers; buffer++)
215 audio_buffer[buffer] = new double*[file->asset->channels];
217 for(int channel = 0; channel < file->asset->channels; channel++)
219 audio_buffer[buffer][channel] = new double[buffer_size];
226 this->color_model = color_model;
227 bytes_per_frame = VFrame::calculate_data_size(file->asset->width,
232 for(buffer = 0; buffer < ring_buffers; buffer++)
234 video_buffer[buffer] = new VFrame**[file->asset->layers];
235 for(layer = 0; layer < file->asset->layers; layer++)
237 video_buffer[buffer][layer] = new VFrame*[buffer_size];
238 for(frame = 0; frame < buffer_size; frame++)
241 video_buffer[buffer][layer][frame] = new VFrame;
244 video_buffer[buffer][layer][frame] =
254 //printf("FileThread::start_writing 1\n");
257 for(int i = 0; i < ring_buffers; i++)
261 //printf("FileThread::start_writing 1\n");
268 double** FileThread::get_audio_buffer()
272 input_lock[current_buffer]->lock("FileThread::get_audio_buffer");
273 return audio_buffer[current_buffer];
276 VFrame*** FileThread::get_video_buffer()
280 input_lock[current_buffer]->lock("FileThread::get_video_buffer");
281 return video_buffer[current_buffer];
284 int FileThread::write_buffer(long size)
286 output_size[current_buffer] = size;
288 // unlock the output lock
289 output_lock[current_buffer]->unlock();
294 int FileThread::swap_buffer()
297 if(current_buffer >= ring_buffers) current_buffer = 0;