diff options
Diffstat (limited to 'src/mov_encoder_thread.cc')
-rw-r--r-- | src/mov_encoder_thread.cc | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/src/mov_encoder_thread.cc b/src/mov_encoder_thread.cc index dab308d..7c7f5d9 100644 --- a/src/mov_encoder_thread.cc +++ b/src/mov_encoder_thread.cc @@ -34,17 +34,19 @@ MovEncoderThread::MovEncoderThread(const char *cpr, Info *i) info = i; info->info("MovEncoderThread"); - // 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); + // Queue + inputqueue = new ThreadSafeQueueFIFO<FrameVector*>(); + + // Initialize read semaphore + sem_init(&read_sem, 0, 0); + + video_output_queue = new ThreadSafeQueuePriority(info); + audio_input_queue = new ThreadSafeQueuePriority(info); + audio_output_queue = new ThreadSafeQueuePriority(info); + + info->info("video_output_queue: 0x%x", video_output_queue); + info->info("audio_input_queue: 0x%x", audio_input_queue); + info->info("audio_output_queue: 0x%x", audio_output_queue); block = new FrameVector(); @@ -53,13 +55,6 @@ MovEncoderThread::MovEncoderThread(const char *cpr, Info *i) info->info("Frame sequence length %d", num_frames_in_block); threads = config->readInt("encoding_threads"); - - // 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; @@ -67,26 +62,25 @@ MovEncoderThread::MovEncoderThread(const char *cpr, Info *i) // 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); + MovEncoder *movenc = new MovEncoder(&movencodersrunning, &read_sem, + inputqueue, + video_output_queue, + audio_input_queue, + 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, + audioenc = new AudioEncoder(audio_input_queue, + audio_output_queue, 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, + video_output_queue, + audio_output_queue, info); writer->run(); @@ -99,7 +93,7 @@ MovEncoderThread::~MovEncoderThread() info->info("~MovEncoderThread"); // 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++) 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 @@ -108,7 +102,6 @@ MovEncoderThread::~MovEncoderThread() 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 @@ -116,18 +109,15 @@ MovEncoderThread::~MovEncoderThread() // Finally we destroy the writer. writer->running = false; - sem_post(&video_out_sem); // Kick it to make it stop. - sem_post(&audio_out_sem); // Kick it to make it stop. + + // FIXME: Post writer writer->wait_stop(); // Wait for it to stop. delete writer; // delete the writer (end thereby close the file) info->info("Deleted the 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(&in_sem); sem_destroy(&read_sem); info->info("~MovEncoderThread::done"); @@ -139,12 +129,11 @@ void MovEncoderThread::encode(Frame* frame) if(output % 250 == 0) // 25 * 24 info->info("inputqueue: %d\tvideo_outputqueue: %d\taudio_inputqueue: %d\taudio_outputqueue: %d.", inputqueue->size(), - video_outputqueue->size(), - audio_inputqueue->size(), - audio_outputqueue->size()); + video_output_queue->size(), + audio_input_queue->size(), + audio_output_queue->size()); output++; - if(frame == NULL) { info->info("MovEncoderThread::encode - NULL frame detected."); // Terminate @@ -159,14 +148,7 @@ void MovEncoderThread::encode(Frame* frame) // Wait until a free encoder. sem_wait(&read_sem); - // Lock input mutex - pthread_mutex_lock(&input_mutex); inputqueue->push(block); - pthread_mutex_unlock(&input_mutex); - // Unlock input mutex - - // Kick encoders - sem_post(&in_sem); // Start new block block = new FrameVector; |