+
+void
+Encoder::write_audio (shared_ptr<const AudioBuffers> audio)
+{
+ for (int i = 0; i < audio->channels(); ++i) {
+ sf_write_float (_sound_files[i], audio->data(i), audio->frames());
+ }
+
+ _audio_frames_written += audio->frames ();
+}
+
+void
+Encoder::close_sound_files ()
+{
+ for (vector<SNDFILE*>::iterator i = _sound_files.begin(); i != _sound_files.end(); ++i) {
+ sf_close (*i);
+ }
+
+ _sound_files.clear ();
+}
+
+void
+Encoder::terminate_worker_threads ()
+{
+ boost::mutex::scoped_lock lock (_worker_mutex);
+ _process_end = true;
+ _worker_condition.notify_all ();
+ lock.unlock ();
+
+ for (list<boost::thread *>::iterator i = _worker_threads.begin(); i != _worker_threads.end(); ++i) {
+ (*i)->join ();
+ delete *i;
+ }
+}
+
+void
+Encoder::encoder_thread (ServerDescription* server)
+{
+ /* Number of seconds that we currently wait between attempts
+ to connect to the server; not relevant for localhost
+ encodings.
+ */
+ int remote_backoff = 0;
+
+ while (1) {
+
+ TIMING ("encoder thread %1 sleeps", boost::this_thread::get_id());
+ boost::mutex::scoped_lock lock (_worker_mutex);
+ while (_queue.empty () && !_process_end) {
+ _worker_condition.wait (lock);
+ }
+
+ if (_process_end) {
+ return;
+ }
+
+ TIMING ("encoder thread %1 wakes with queue of %2", boost::this_thread::get_id(), _queue.size());
+ boost::shared_ptr<DCPVideoFrame> vf = _queue.front ();
+ _film->log()->log (String::compose ("Encoder thread %1 pops frame %2 from queue", boost::this_thread::get_id(), vf->frame()), Log::VERBOSE);
+ _queue.pop_front ();
+
+ lock.unlock ();
+
+ shared_ptr<EncodedData> encoded;
+
+ if (server) {
+ try {
+ encoded = vf->encode_remotely (server);
+
+ if (remote_backoff > 0) {
+ _film->log()->log (String::compose ("%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;
+ }
+ _film->log()->log (
+ String::compose (
+ "Remote encode of %1 on %2 failed (%3); thread sleeping for %4s",
+ vf->frame(), server->host_name(), e.what(), remote_backoff)
+ );
+ }
+
+ } else {
+ try {
+ TIMING ("encoder thread %1 begins local encode of %2", boost::this_thread::get_id(), vf->frame());
+ encoded = vf->encode_locally ();
+ TIMING ("encoder thread %1 finishes local encode of %2", boost::this_thread::get_id(), vf->frame());
+ } catch (std::exception& e) {
+ _film->log()->log (String::compose ("Local encode failed (%1)", e.what ()));
+ }
+ }
+
+ if (encoded) {
+ encoded->write (_opt, vf->frame ());
+ frame_done ();
+ } else {
+ lock.lock ();
+ _film->log()->log (
+ String::compose ("Encoder thread %1 pushes frame %2 back onto queue after failure", boost::this_thread::get_id(), vf->frame())
+ );
+ _queue.push_front (vf);
+ lock.unlock ();
+ }
+
+ if (remote_backoff > 0) {
+ dvdomatic_sleep (remote_backoff);
+ }
+
+ lock.lock ();
+ _worker_condition.notify_all ();
+ }
+}
+
+void
+Encoder::link (string a, string b) const
+{
+#ifdef DVDOMATIC_POSIX
+ int const r = symlink (a.c_str(), b.c_str());
+ if (r) {
+ throw EncodeError (String::compose ("could not create symlink from %1 to %2", a, b));
+ }
+#endif
+
+#ifdef DVDOMATIC_WINDOWS
+ boost::filesystem::copy_file (a, b);
+#endif
+}