DCPVideoFrame -> DCPVideo and PlayerVideoFrame -> PlayerVideo.
[dcpomatic.git] / src / lib / encoder.cc
index ee43476c95153c3d6ed3295f6eb7c04a8fe96a12..2b1f1d9b3bab37236f1bb69462d658e598216540 100644 (file)
 #include "film.h"
 #include "log.h"
 #include "config.h"
-#include "dcp_video_frame.h"
+#include "dcp_video.h"
 #include "server.h"
 #include "cross.h"
 #include "writer.h"
 #include "server_finder.h"
 #include "player.h"
-#include "player_video_frame.h"
+#include "player_video.h"
 
 #include "i18n.h"
 
 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
 #define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
-#define LOG_TIMING(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
+#define LOG_TIMING(...) _film->log()->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
 
 using std::pair;
 using std::string;
@@ -65,9 +65,7 @@ Encoder::Encoder (shared_ptr<const Film> f, weak_ptr<Job> j)
        , _video_frames_out (0)
        , _terminate (false)
 {
-       _have_a_real_frame[EYES_BOTH] = false;
-       _have_a_real_frame[EYES_LEFT] = false;
-       _have_a_real_frame[EYES_RIGHT] = false;
+
 }
 
 Encoder::~Encoder ()
@@ -108,8 +106,8 @@ Encoder::process_end ()
 
        /* Keep waking workers until the queue is empty */
        while (!_queue.empty ()) {
-               _condition.notify_all ();
-               _condition.wait (lock);
+               _empty_condition.notify_all ();
+               _full_condition.wait (lock);
        }
 
        lock.unlock ();
@@ -127,7 +125,7 @@ Encoder::process_end ()
             So just mop up anything left in the queue here.
        */
 
-       for (list<shared_ptr<DCPVideoFrame> >::iterator i = _queue.begin(); i != _queue.end(); ++i) {
+       for (list<shared_ptr<DCPVideo> >::iterator i = _queue.begin(); i != _queue.end(); ++i) {
                LOG_GENERAL (N_("Encode left-over frame %1"), (*i)->index ());
                try {
                        _writer->write ((*i)->encode_locally(), (*i)->index (), (*i)->eyes ());
@@ -183,7 +181,7 @@ Encoder::frame_done ()
 }
 
 void
-Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf, bool same)
+Encoder::process_video (shared_ptr<PlayerVideo> pvf)
 {
        _waker.nudge ();
        
@@ -194,7 +192,7 @@ Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf, bool same)
        /* Wait until the queue has gone down a bit */
        while (_queue.size() >= _threads.size() * 2 && !_terminate) {
                LOG_TIMING ("decoder sleeps with queue of %1", _queue.size());
-               _condition.wait (lock);
+               _full_condition.wait (lock);
                LOG_TIMING ("decoder wakes with queue of %1", _queue.size());
        }
 
@@ -211,24 +209,25 @@ Encoder::process_video (shared_ptr<PlayerVideoFrame> pvf, bool same)
 
        if (_writer->can_fake_write (_video_frames_out)) {
                _writer->fake_write (_video_frames_out, pvf->eyes ());
-               _have_a_real_frame[pvf->eyes()] = false;
-               frame_done ();
-       } else if (same && _have_a_real_frame[pvf->eyes()]) {
-               /* Use the last frame that we encoded. */
-               _writer->repeat (_video_frames_out, pvf->eyes());
                frame_done ();
        } else {
                /* Queue this new frame for encoding */
                LOG_TIMING ("adding to queue of %1", _queue.size ());
-               _queue.push_back (shared_ptr<DCPVideoFrame> (
-                                         new DCPVideoFrame (
-                                                 pvf, _video_frames_out, _film->video_frame_rate(),
-                                                 _film->j2k_bandwidth(), _film->resolution(), _film->log()
+               _queue.push_back (shared_ptr<DCPVideo> (
+                                         new DCPVideo (
+                                                 pvf,
+                                                 _video_frames_out,
+                                                 _film->video_frame_rate(),
+                                                 _film->j2k_bandwidth(),
+                                                 _film->resolution(),
+                                                 _film->log()
                                                  )
                                          ));
-               
-               _condition.notify_all ();
-               _have_a_real_frame[pvf->eyes()] = true;
+
+               /* The queue might not be empty any more, so notify anything which is
+                  waiting on that.
+               */
+               _empty_condition.notify_all ();
        }
 
        if (pvf->eyes() != EYES_LEFT) {
@@ -248,7 +247,8 @@ Encoder::terminate_threads ()
        {
                boost::mutex::scoped_lock lock (_mutex);
                _terminate = true;
-               _condition.notify_all ();
+               _full_condition.notify_all ();
+               _empty_condition.notify_all ();
        }
 
        for (list<boost::thread *>::iterator i = _threads.begin(); i != _threads.end(); ++i) {
@@ -271,21 +271,21 @@ try
        */
        int remote_backoff = 0;
        
-       while (1) {
+       while (true) {
 
-               LOG_TIMING ("encoder thread %1 sleeps", boost::this_thread::get_id());
+               LOG_TIMING ("[%1] encoder thread sleeps", boost::this_thread::get_id());
                boost::mutex::scoped_lock lock (_mutex);
                while (_queue.empty () && !_terminate) {
-                       _condition.wait (lock);
+                       _empty_condition.wait (lock);
                }
 
                if (_terminate) {
                        return;
                }
 
-               LOG_TIMING ("encoder thread %1 wakes with queue of %2", boost::this_thread::get_id(), _queue.size());
-               shared_ptr<DCPVideoFrame> vf = _queue.front ();
-               LOG_TIMING ("encoder thread %1 pops frame %2 (%3) from queue", boost::this_thread::get_id(), vf->index(), vf->eyes ());
+               LOG_TIMING ("[%1] encoder thread wakes with queue of %2", boost::this_thread::get_id(), _queue.size());
+               shared_ptr<DCPVideo> vf = _queue.front ();
+               LOG_TIMING ("[%1] encoder thread pops frame %2 (%3) from queue", boost::this_thread::get_id(), vf->index(), vf->eyes ());
                _queue.pop_front ();
                
                lock.unlock ();
@@ -316,9 +316,9 @@ try
                                
                } else {
                        try {
-                               LOG_TIMING ("encoder thread %1 begins local encode of %2", boost::this_thread::get_id(), vf->index());
+                               LOG_TIMING ("[%1] encoder thread begins local encode of %2", boost::this_thread::get_id(), vf->index());
                                encoded = vf->encode_locally ();
-                               LOG_TIMING ("encoder thread %1 finishes local encode of %2", boost::this_thread::get_id(), vf->index());
+                               LOG_TIMING ("[%1] encoder thread finishes local encode of %2", boost::this_thread::get_id(), vf->index());
                        } catch (std::exception& e) {
                                LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
                        }
@@ -329,7 +329,7 @@ try
                        frame_done ();
                } else {
                        lock.lock ();
-                       LOG_GENERAL (N_("Encoder thread %1 pushes frame %2 back onto queue after failure"), boost::this_thread::get_id(), vf->index());
+                       LOG_GENERAL (N_("[%1] Encoder thread pushes frame %2 back onto queue after failure"), boost::this_thread::get_id(), vf->index());
                        _queue.push_front (vf);
                        lock.unlock ();
                }
@@ -338,8 +338,9 @@ try
                        dcpomatic_sleep (remote_backoff);
                }
 
+               /* The queue might not be full any more, so notify anything that is waiting on that */
                lock.lock ();
-               _condition.notify_all ();
+               _full_condition.notify_all ();
        }
 }
 catch (...)