-using boost::optional;
-using boost::scoped_array;
-
-int const Encoder::_history_size = 25;
-
-/** @param f Film that we are encoding */
-Encoder::Encoder (shared_ptr<const Film> f, weak_ptr<Job> j, shared_ptr<Writer> writer)
- : _film (f)
- , _job (j)
- , _video_frames_out (0)
- , _terminate (false)
- , _writer (writer)
-{
-
-}
-
-Encoder::~Encoder ()
-{
- terminate_threads ();
-}
-
-/** Add a worker thread for a each thread on a remote server. Caller must hold
- * a lock on _mutex, or know that one is not currently required to
- * safely modify _threads.
- */
-void
-Encoder::add_worker_threads (ServerDescription d)
-{
- LOG_GENERAL (N_("Adding %1 worker threads for remote %2"), d.host_name ());
- for (int i = 0; i < d.threads(); ++i) {
- _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, d)));
- }
-}
-
-void
-Encoder::begin ()
-{
- 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> ())));
- }
-
- ServerFinder::instance()->connect (boost::bind (&Encoder::server_found, this, _1));
-}
-
-void
-Encoder::end ()
-{
- boost::mutex::scoped_lock lock (_mutex);
-
- LOG_GENERAL (N_("Clearing queue of %1"), _queue.size ());
-
- /* Keep waking workers until the queue is empty */
- while (!_queue.empty ()) {
- _empty_condition.notify_all ();
- _full_condition.wait (lock);
- }
-
- lock.unlock ();
-
- terminate_threads ();
-
- LOG_GENERAL (N_("Mopping up %1"), _queue.size());
-
- /* The following sequence of events can occur in the above code:
- 1. a remote worker takes the last image off the queue
- 2. the loop above terminates
- 3. the remote worker fails to encode the image and puts it back on the queue
- 4. the remote worker is then terminated by terminate_threads
-
- So just mop up anything left in the queue here.
- */
-
- 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 ());
- frame_done ();
- } catch (std::exception& e) {
- LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
- }
- }
-}
-
-/** @return an estimate of the current number of frames we are encoding per second,
- * or 0 if not known.
- */
-float
-Encoder::current_encoding_rate () const
-{
- boost::mutex::scoped_lock lock (_state_mutex);
- if (int (_time_history.size()) < _history_size) {
- return 0;
- }
-
- struct timeval now;
- gettimeofday (&now, 0);
-
- return _history_size / (seconds (now) - seconds (_time_history.back ()));
-}
-
-/** @return Number of video frames that have been sent out */
-int
-Encoder::video_frames_out () const
-{
- boost::mutex::scoped_lock (_state_mutex);
- return _video_frames_out;
-}