-void
-Encoder::encode (shared_ptr<PlayerVideo> pv)
-{
- _waker.nudge ();
-
- size_t threads = 0;
- {
- boost::mutex::scoped_lock threads_lock (_threads_mutex);
- threads = _threads.size ();
- }
-
- boost::mutex::scoped_lock queue_lock (_queue_mutex);
-
- /* Wait until the queue has gone down a bit */
- while (_queue.size() >= threads * 2) {
- LOG_TIMING ("decoder-sleep queue=%1", _queue.size());
- _full_condition.wait (queue_lock);
- LOG_TIMING ("decoder-wake queue=%1", _queue.size());
- }
-
- _writer->rethrow ();
- /* Re-throw any exception raised by one of our threads. If more
- than one has thrown an exception, only one will be rethrown, I think;
- but then, if that happens something has gone badly wrong.
- */
- rethrow ();
-
- Frame const position = pv->time().frames_floor(_film->video_frame_rate());
-
- if (_writer->can_fake_write (position)) {
- /* We can fake-write this frame */
- _writer->fake_write (position, pv->eyes ());
- frame_done ();
- } 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 && _writer->can_repeat(position) && pv->same (_last_player_video)) {
- _writer->repeat (position, pv->eyes ());
- } else {
- /* Queue this new frame for encoding */
- LOG_TIMING ("add-frame-to-queue queue=%1", _queue.size ());
- _queue.push_back (shared_ptr<DCPVideo> (
- new DCPVideo (
- pv,
- position,
- _film->video_frame_rate(),
- _film->j2k_bandwidth(),
- _film->resolution(),
- _film->log()
- )
- ));
-
- /* The queue might not be empty any more, so notify anything which is
- waiting on that.
- */
- _empty_condition.notify_all ();
- }
-
- _last_player_video = pv;
-}
-
-void
-Encoder::terminate_threads ()
-{
- 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 ();
- DCPOMATIC_ASSERT ((*i)->joinable ());
- try {
- (*i)->join ();
- } catch (boost::thread_interrupted& e) {
- /* This is to be expected */
- }
- delete *i;
- LOG_GENERAL_NC ("Thread terminated");
- ++n;
- }
-
- _threads.clear ();
-}
-
-void
-Encoder::encoder_thread (optional<EncodeServerDescription> server)
-try
-{
- if (server) {
- LOG_TIMING ("start-encoder-thread thread=%1 server=%2", boost::this_thread::get_id (), server->host_name ());
- } else {
- LOG_TIMING ("start-encoder-thread thread=%1 server=localhost", boost::this_thread::get_id ());
- }
-
- /* Number of seconds that we currently wait between attempts
- to connect to the server; not relevant for localhost
- encodings.
- */
- int remote_backoff = 0;
-
- while (true) {
-
- LOG_TIMING ("encoder-sleep thread=%1", boost::this_thread::get_id());
- boost::mutex::scoped_lock lock (_queue_mutex);
- while (_queue.empty ()) {
- _empty_condition.wait (lock);
- }
-
- LOG_TIMING ("encoder-wake thread=%1 queue=%2", boost::this_thread::get_id(), _queue.size());
- shared_ptr<DCPVideo> vf = _queue.front ();
- LOG_TIMING ("encoder-pop thread=%1 frame=%2 eyes=%3", boost::this_thread::get_id(), vf->index(), vf->eyes ());
- _queue.pop_front ();
-
- lock.unlock ();
-
- optional<Data> encoded;
-
- /* We need to encode this input */
- if (server) {
- try {
- encoded = vf->encode_remotely (server.get ());
-
- if (remote_backoff > 0) {
- LOG_GENERAL ("%1 was lost, but now she is found; removing backoff", server->host_name ());
- }
-
- /* This job succeeded, so remove any backoff */
- remote_backoff = 0;
-
- } catch (std::exception& e) {
- if (remote_backoff < 60) {
- /* back off more */
- remote_backoff += 10;
- }
- LOG_ERROR (
- N_("Remote encode of %1 on %2 failed (%3); thread sleeping for %4s"),
- vf->index(), server->host_name(), e.what(), remote_backoff
- );
- }
-
- } else {
- try {
- LOG_TIMING ("start-local-encode thread=%1 frame=%2", boost::this_thread::get_id(), vf->index());
- encoded = vf->encode_locally (boost::bind (&Log::dcp_log, _film->log().get(), _1, _2));
- LOG_TIMING ("finish-local-encode thread=%1 frame=%2", boost::this_thread::get_id(), vf->index());
- } catch (std::exception& e) {
- LOG_ERROR (N_("Local encode failed (%1)"), e.what ());
- throw;
- }
- }
-
- if (encoded) {
- _writer->write (encoded.get(), vf->index (), vf->eyes ());
- frame_done ();
- } else {
- lock.lock ();
- 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 ();
- }
-
- if (remote_backoff > 0) {
- 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 */
- lock.lock ();
- _full_condition.notify_all ();
- }
-}
-catch (boost::thread_interrupted& e) {
- /* Ignore these and just stop the thread */
- _full_condition.notify_all ();
-}
-catch (...)
-{
- store_current ();
- /* Wake anything waiting on _full_condition so it can see the exception */
- _full_condition.notify_all ();
-}
-
-void
-Encoder::servers_list_changed ()