diff options
Diffstat (limited to 'src/mov_encoder_thread.cc')
-rw-r--r-- | src/mov_encoder_thread.cc | 98 |
1 files changed, 75 insertions, 23 deletions
diff --git a/src/mov_encoder_thread.cc b/src/mov_encoder_thread.cc index dc3581d..4562ec9 100644 --- a/src/mov_encoder_thread.cc +++ b/src/mov_encoder_thread.cc @@ -37,11 +37,13 @@ MovEncoderThread::MovEncoderThread(const char *cpr, Info *i) // Queues inputqueue = new FrameVectorQueue(); video_outputqueue = new FramePriorityQueue(); + audio_inputqueue = new FramePriorityQueue(); audio_outputqueue = new FramePriorityQueue(); // Queue mutexes pthread_mutex_init (&input_mutex, NULL); pthread_mutex_init (&video_output_mutex, NULL); + pthread_mutex_init (&audio_input_mutex, NULL); pthread_mutex_init (&audio_output_mutex, NULL); block = new FrameVector(); @@ -55,39 +57,83 @@ MovEncoderThread::MovEncoderThread(const char *cpr, Info *i) // Thread stuff sem_init(&in_sem, 0, 0); sem_init(&video_out_sem, 0, 0); + sem_init(&audio_in_sem, 0, 0); sem_init(&audio_out_sem, 0, 0); sem_init(&read_sem, 0, 0); + movencodersrunning = true; + for(int cnt = 0; cnt < threads; cnt++) sem_post(&read_sem); + // Create the video encoders + for(int cnt = 0; cnt < threads; cnt++) { + MovEncoder *movenc = + new MovEncoder(&movencodersrunning, &read_sem, + inputqueue, &in_sem, &input_mutex, + video_outputqueue, &video_output_mutex, &video_out_sem, + audio_inputqueue, &audio_input_mutex, &audio_in_sem, + info); + movenc->run(); + encs.push_back(movenc); + } + + // Create the audio encoder + audioenc = new AudioEncoder(audio_inputqueue, &audio_input_mutex, &audio_in_sem, + audio_outputqueue, &audio_output_mutex, &audio_out_sem, + info); + audioenc->run(); + + // Create the multiplexer writer = new MovEncoderWriter(cpr, video_outputqueue, &video_output_mutex, &video_out_sem, audio_outputqueue, &audio_output_mutex, &audio_out_sem, info); writer->run(); - for(int cnt = 0; cnt < threads; cnt++) { - MovEncoder *movenc = new MovEncoder(&read_sem, - inputqueue, &in_sem, &input_mutex, - video_outputqueue, &video_output_mutex, &video_out_sem, - audio_outputqueue, &audio_output_mutex, &audio_out_sem, - info); - movenc->run(); - encs.push_back(movenc); - } - frame_number = 0; } +#include <unistd.h> MovEncoderThread::~MovEncoderThread() { info->info("~MovEncoderThread"); + // Push end of stream frame to the queue. + num_frames_in_block = block->size() + 1; // Make the next frame fill out the block. + encode(endOfFrameStream); + + info->info("Posted endOfFrameStream"); + + // First we destroy the movie encoders + for(int cnt = 0; cnt < threads; cnt++) sem_post(&in_sem); // Kick them + for(int cnt = 0; cnt < threads; cnt++) { + encs[cnt]->wait_stop(); // Wait for it to stop + delete encs[cnt]; // Delete it + } + + info->info("Deleted the movie encoders"); + + // Then we destroy the audio encoder + sem_post(&audio_in_sem); // Kick it + audioenc->wait_stop(); // Wait for it to stop. + delete audioenc; // delete the audio encoder + + info->info("Deleted the audio encoder"); + + // Finally we destroy the writer. + sem_post(&video_out_sem); // Kick it to make it stop. + sem_post(&audio_out_sem); // Kick it to make it stop. + writer->wait_stop(); // Wait for it to stop. + delete writer; // delete the writer (end thereby close the file) + + info->info("Deleted the writer"); + + /* // These should not be deleted here... its done elsewhere. // inputqueue = NULL; - sem_post(&video_out_sem); - sem_post(&audio_out_sem); + // sem_post(&video_out_sem); + // sem_post(&audio_out_sem); // Tell the encoding threads to stop. for(int cnt = 0; cnt < threads; cnt++) { @@ -107,6 +153,18 @@ MovEncoderThread::~MovEncoderThread() delete encs[cnt]; } + // Tell the audio encoder to stop + audioenc->running = false; + + // Kick it to make it stop. + sem_post(&audio_in_sem); + + // Wait for it to stop. + audioenc->wait_stop(); + + // delete the audio encoder + delete audioenc; + // Tell the writer to stop writer->running = false; @@ -119,12 +177,16 @@ MovEncoderThread::~MovEncoderThread() // delete the writer (end thereby close the file) delete writer; - + */ // Destroy the semaphores. sem_destroy(&in_sem); sem_destroy(&video_out_sem); + sem_destroy(&audio_in_sem); sem_destroy(&audio_out_sem); sem_destroy(&read_sem); + + + info->info("MovEncoderThread done deinitializing."); } void MovEncoderThread::encode(Frame* frame) @@ -141,11 +203,6 @@ void MovEncoderThread::encode(Frame* frame) // Switch frame if(block->size() == num_frames_in_block) { // Wait until a free encoder. - /* - int val; - sem_getvalue(&read_sem, &val); - fprintf(stderr, "Sem Value: %d\n", val); fflush(stderr); - */ sem_wait(&read_sem); // Lock input mutex @@ -154,11 +211,6 @@ void MovEncoderThread::encode(Frame* frame) pthread_mutex_unlock(&input_mutex); // Unlock input mutex - // fprintf(stderr, "Frame vector [%d-%d] pushed\n", - // block->at(0)->number, - // block->at(block->size() - 1)->number); - fflush(stderr); - // Kick encoders sem_post(&in_sem); |