Rename Server -> EncodeServer, ServerFinder -> EncodeServerFinder, ServerDescription...
[dcpomatic.git] / src / lib / encoder.cc
index 2e116328b4fd4adf72bd43ddb37773d2fa6069d6..8ba794a8e4c43b368e6cb801f4de28aac150e430 100644 (file)
 #include "log.h"
 #include "config.h"
 #include "dcp_video.h"
-#include "server.h"
 #include "cross.h"
 #include "writer.h"
-#include "server_finder.h"
+#include "encode_server_finder.h"
 #include "player.h"
 #include "player_video.h"
-#include "data.h"
-#include "server_description.h"
+#include "encode_server_description.h"
 #include "compose.hpp"
 #include <libcxml/cxml.h>
 #include <boost/foreach.hpp>
@@ -52,13 +50,13 @@ using std::cout;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::optional;
+using dcp::Data;
 
 int const Encoder::_history_size = 25;
 
 /** @param f Film that we are encoding */
-Encoder::Encoder (shared_ptr<const Film> film, weak_ptr<Job> j, shared_ptr<Writer> writer)
+Encoder::Encoder (shared_ptr<const Film> film, shared_ptr<Writer> writer)
        : _film (film)
-       , _job (j)
        , _position (0)
        , _terminate_enqueue (false)
        , _terminate_encoding (false)
@@ -80,8 +78,8 @@ Encoder::~Encoder ()
 void
 Encoder::begin ()
 {
-       if (!ServerFinder::instance()->disabled ()) {
-               _server_found_connection = ServerFinder::instance()->ServersListChanged.connect (boost::bind (&Encoder::servers_list_changed, this));
+       if (!EncodeServerFinder::instance()->disabled ()) {
+               _server_found_connection = EncodeServerFinder::instance()->ServersListChanged.connect (boost::bind (&Encoder::servers_list_changed, this));
        }
 }
 
@@ -226,7 +224,7 @@ Encoder::enqueue (shared_ptr<PlayerVideo> pv)
        } else if (pv->has_j2k ()) {
                /* This frame already has JPEG2000 data, so just write it */
                _writer->write (pv->j2k(), _position, pv->eyes ());
-       } else if (_last_player_video && pv->same (_last_player_video)) {
+       } else if (_last_player_video && _writer->can_repeat(_position) && pv->same (_last_player_video)) {
                _writer->repeat (_position, pv->eyes ());
        } else {
                /* Queue this new frame for encoding */
@@ -257,17 +255,20 @@ Encoder::terminate_threads ()
        {
                boost::mutex::scoped_lock queue_lock (_queue_mutex);
                _terminate_encoding = true;
-               _full_condition.notify_all ();
-               _empty_condition.notify_all ();
        }
 
        boost::mutex::scoped_lock threads_lock (_threads_mutex);
 
+       int n = 0;
        for (list<boost::thread *>::iterator i = _threads.begin(); i != _threads.end(); ++i) {
+               LOG_GENERAL ("Terminating thread %1 of %2", n + 1, _threads.size ());
+               (*i)->interrupt ();
                if ((*i)->joinable ()) {
                        (*i)->join ();
                }
                delete *i;
+               LOG_GENERAL_NC ("Thread terminated");
+               ++n;
        }
 
        _threads.clear ();
@@ -275,7 +276,7 @@ Encoder::terminate_threads ()
 }
 
 void
-Encoder::encoder_thread (optional<ServerDescription> server)
+Encoder::encoder_thread (optional<EncodeServerDescription> server)
 try
 {
        if (server) {
@@ -356,7 +357,7 @@ try
                }
 
                if (remote_backoff > 0) {
-                       dcpomatic_sleep (remote_backoff);
+                       boost::this_thread::sleep (boost::posix_time::seconds (remote_backoff));
                }
 
                /* The queue might not be full any more, so notify anything that is waiting on that */
@@ -382,11 +383,11 @@ Encoder::servers_list_changed ()
 
        if (!Config::instance()->only_servers_encode ()) {
                for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
-                       _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ())));
+                       _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<EncodeServerDescription> ())));
                }
        }
 
-       BOOST_FOREACH (ServerDescription i, ServerFinder::instance()->servers ()) {
+       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->servers ()) {
                LOG_GENERAL (N_("Adding %1 worker threads for remote %2"), i.threads(), i.host_name ());
                for (int j = 0; j < i.threads(); ++j) {
                        _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, i)));