std::shared_ptr
[dcpomatic.git] / src / lib / writer.cc
index 7f61c1ece4b6693e7b7d44fed80d011b946ecfc2..ff2adeb0082df70b8e804c397126cfc9bf0d2473 100644 (file)
@@ -32,7 +32,7 @@
 #include "cross.h"
 #include "audio_buffers.h"
 #include "version.h"
-#include "font.h"
+#include "font_data.h"
 #include "util.h"
 #include "reel_writer.h"
 #include "text_content.h"
@@ -59,9 +59,9 @@ using std::map;
 using std::min;
 using std::max;
 using std::vector;
-using boost::shared_ptr;
-using boost::weak_ptr;
-using boost::dynamic_pointer_cast;
+using std::shared_ptr;
+using std::weak_ptr;
+using std::dynamic_pointer_cast;
 using boost::optional;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
@@ -70,7 +70,19 @@ using dcp::Data;
 using dcp::ArrayData;
 using namespace dcpomatic;
 
-Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
+
+static
+void
+ignore_progress (float)
+{
+
+}
+
+
+/** @param j Job to report progress to, or 0.
+ *  @param text_only true to enable only the text (subtitle/ccap) parts of the writer.
+ */
+Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j, bool text_only)
        : WeakConstFilm (weak_film)
        , _job (j)
        , _finish (false)
@@ -82,14 +94,15 @@ Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
        , _fake_written (0)
        , _repeat_written (0)
        , _pushed_to_disk (0)
+       , _text_only (text_only)
+       , _have_subtitles (false)
 {
        shared_ptr<Job> job = _job.lock ();
-       DCPOMATIC_ASSERT (job);
 
        int reel_index = 0;
        list<DCPTimePeriod> const reels = film()->reels();
        BOOST_FOREACH (DCPTimePeriod p, reels) {
-               _reels.push_back (ReelWriter(film(), p, job, reel_index++, reels.size()));
+               _reels.push_back (ReelWriter(weak_film, p, job, reel_index++, reels.size(), text_only));
        }
 
        _last_written.resize (reels.size());
@@ -114,15 +127,19 @@ Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
 void
 Writer::start ()
 {
-       _thread = boost::thread (boost::bind(&Writer::thread, this));
+       if (!_text_only) {
+               _thread = boost::thread (boost::bind(&Writer::thread, this));
 #ifdef DCPOMATIC_LINUX
-       pthread_setname_np (_thread.native_handle(), "writer");
+               pthread_setname_np (_thread.native_handle(), "writer");
 #endif
+       }
 }
 
 Writer::~Writer ()
 {
-       terminate_thread (false);
+       if (!_text_only) {
+               terminate_thread (false);
+       }
 }
 
 /** Pass a video frame to the writer for writing to disk at some point.
@@ -508,26 +525,25 @@ Writer::terminate_thread (bool can_throw)
        }
 }
 
+
+/** @param output_dcp Path to DCP folder to write */
 void
-Writer::finish ()
+Writer::finish (boost::filesystem::path output_dcp)
 {
-       if (!_thread.joinable()) {
-               return;
+       if (_thread.joinable()) {
+               LOG_GENERAL_NC ("Terminating writer thread");
+               terminate_thread (true);
        }
 
-       LOG_GENERAL_NC ("Terminating writer thread");
-
-       terminate_thread (true);
-
        LOG_GENERAL_NC ("Finishing ReelWriters");
 
        BOOST_FOREACH (ReelWriter& i, _reels) {
-               i.finish ();
+               i.finish (output_dcp);
        }
 
        LOG_GENERAL_NC ("Writing XML");
 
-       dcp::DCP dcp (film()->dir(film()->dcp_name()));
+       dcp::DCP dcp (output_dcp);
 
        shared_ptr<dcp::CPL> cpl (
                new dcp::CPL (
@@ -541,7 +557,9 @@ Writer::finish ()
        /* Calculate digests for each reel in parallel */
 
        shared_ptr<Job> job = _job.lock ();
-       job->sub (_("Computing digests"));
+       if (job) {
+               job->sub (_("Computing digests"));
+       }
 
        boost::asio::io_service service;
        boost::thread_group pool;
@@ -554,7 +572,13 @@ Writer::finish ()
                pool.create_thread (boost::bind (&boost::asio::io_service::run, &service));
        }
 
-       boost::function<void (float)> set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1);
+       boost::function<void (float)> set_progress;
+       if (job) {
+               set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1);
+       } else {
+               set_progress = &ignore_progress;
+       }
+
        BOOST_FOREACH (ReelWriter& i, _reels) {
                service.post (boost::bind (&ReelWriter::calculate_digests, &i, set_progress));
        }
@@ -567,7 +591,7 @@ Writer::finish ()
        /* Add reels */
 
        BOOST_FOREACH (ReelWriter& i, _reels) {
-               cpl->add (i.create_reel (_reel_assets, _fonts));
+               cpl->add (i.create_reel(_reel_assets, _fonts, output_dcp, _have_subtitles, _have_closed_captions));
        }
 
        /* Add metadata */
@@ -645,11 +669,11 @@ Writer::finish ()
                N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT, %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk
                );
 
-       write_cover_sheet ();
+       write_cover_sheet (output_dcp);
 }
 
 void
-Writer::write_cover_sheet ()
+Writer::write_cover_sheet (boost::filesystem::path output_dcp)
 {
        boost::filesystem::path const cover = film()->file("COVER_SHEET.txt");
        FILE* f = fopen_boost (cover, "w");
@@ -672,7 +696,7 @@ Writer::write_cover_sheet ()
 
        boost::uintmax_t size = 0;
        for (
-               boost::filesystem::recursive_directory_iterator i = boost::filesystem::recursive_directory_iterator(film()->dir(film()->dcp_name()));
+               boost::filesystem::recursive_directory_iterator i = boost::filesystem::recursive_directory_iterator(output_dcp);
                i != boost::filesystem::recursive_directory_iterator();
                ++i) {
                if (boost::filesystem::is_regular_file (i->path ())) {
@@ -746,11 +770,13 @@ Writer::write (PlayerText text, TextType type, optional<DCPTextTrack> track, DCP
        switch (type) {
        case TEXT_OPEN_SUBTITLE:
                reel = &_subtitle_reel;
+               _have_subtitles = true;
                break;
        case TEXT_CLOSED_CAPTION:
                DCPOMATIC_ASSERT (track);
                DCPOMATIC_ASSERT (_caption_reels.find(*track) != _caption_reels.end());
                reel = &_caption_reels[*track];
+               _have_closed_captions.insert (*track);
                break;
        default:
                DCPOMATIC_ASSERT (false);
@@ -766,14 +792,14 @@ Writer::write (PlayerText text, TextType type, optional<DCPTextTrack> track, DCP
 }
 
 void
-Writer::write (list<shared_ptr<Font> > fonts)
+Writer::write (vector<FontData> fonts)
 {
        /* Just keep a list of unique fonts and we'll deal with them in ::finish */
 
-       BOOST_FOREACH (shared_ptr<Font> i, fonts) {
+       BOOST_FOREACH (FontData const& i, fonts) {
                bool got = false;
-               BOOST_FOREACH (shared_ptr<Font> j, _fonts) {
-                       if (*i == *j) {
+               BOOST_FOREACH (FontData const& j, _fonts) {
+                       if (i == j) {
                                got = true;
                        }
                }