Hand-apply d849d411cff28ef5453085791d0b4d7cd73bd070 from master; replace all assert...
[dcpomatic.git] / src / lib / writer.cc
index a023d5cd2e1cc28992573e180bc2ccd9cc540cbc..512973f46fa4e4137a40f78d2ceb4e1911ee70d0 100644 (file)
 
 */
 
-#include <fstream>
-#include <cerrno>
-#include <dcp/mono_picture_mxf.h>
-#include <dcp/stereo_picture_mxf.h>
-#include <dcp/sound_mxf.h>
-#include <dcp/sound_mxf_writer.h>
-#include <dcp/reel.h>
-#include <dcp/reel_mono_picture_asset.h>
-#include <dcp/reel_stereo_picture_asset.h>
-#include <dcp/reel_sound_asset.h>
-#include <dcp/reel_subtitle_asset.h>
-#include <dcp/dcp.h>
-#include <dcp/cpl.h>
-#include <dcp/signer.h>
 #include "writer.h"
 #include "compose.hpp"
 #include "film.h"
 #include "md5_digester.h"
 #include "encoded_data.h"
 #include "version.h"
+#include "font.h"
+#include <dcp/mono_picture_mxf.h>
+#include <dcp/stereo_picture_mxf.h>
+#include <dcp/sound_mxf.h>
+#include <dcp/sound_mxf_writer.h>
+#include <dcp/reel.h>
+#include <dcp/reel_mono_picture_asset.h>
+#include <dcp/reel_stereo_picture_asset.h>
+#include <dcp/reel_sound_asset.h>
+#include <dcp/reel_subtitle_asset.h>
+#include <dcp/dcp.h>
+#include <dcp/cpl.h>
+#include <dcp/signer.h>
+#include <dcp/interop_subtitle_content.h>
+#include <fstream>
+#include <cerrno>
 
 #include "i18n.h"
 
 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
 #define LOG_TIMING(...) _film->log()->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
 #define LOG_WARNING_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_WARNING);
+#define LOG_ERROR(...) _film->log()->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR);
 
 /* OS X strikes again */
 #undef set_key
@@ -84,7 +87,7 @@ Writer::Writer (shared_ptr<const Film> f, weak_ptr<Job> j)
        boost::filesystem::remove_all (_film->dir (_film->dcp_name ()));
 
        shared_ptr<Job> job = _job.lock ();
-       assert (job);
+       DCPOMATIC_ASSERT (job);
 
        job->sub (_("Checking existing image data"));
        check_existing_picture_mxf ();
@@ -305,7 +308,7 @@ try
                        _last_written_eyes = qi.eyes;
                        
                        shared_ptr<Job> job = _job.lock ();
-                       assert (job);
+                       DCPOMATIC_ASSERT (job);
                        int64_t total = _film->length().frames (_film->video_frame_rate ());
                        if (_film->three_d ()) {
                                /* _full_written and so on are incremented for each eye, so we need to double the total
@@ -330,7 +333,7 @@ try
                                ++i;
                        }
 
-                       assert (i != _queue.rend());
+                       DCPOMATIC_ASSERT (i != _queue.rend());
                        QueueItem qi = *i;
 
                        ++_pushed_to_disk;
@@ -406,9 +409,12 @@ Writer::finish ()
        boost::system::error_code ec;
        boost::filesystem::create_hard_link (video_from, video_to, ec);
        if (ec) {
-               /* hard link failed; copy instead */
-               boost::filesystem::copy_file (video_from, video_to);
-               LOG_WARNING_NC ("Hard-link failed; fell back to copying");
+               LOG_WARNING_NC ("Hard-link failed; copying instead");
+               boost::filesystem::copy_file (video_from, video_to, ec);
+               if (ec) {
+                       LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), ec.message ());
+                       throw FileError (ec.message(), video_from);
+               }
        }
 
        _picture_mxf->set_file (video_to);
@@ -477,7 +483,7 @@ Writer::finish ()
        cpl->add (reel);
 
        shared_ptr<Job> job = _job.lock ();
-       assert (job);
+       DCPOMATIC_ASSERT (job);
 
        job->sub (_("Computing image digest"));
        _picture_mxf->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
@@ -565,7 +571,7 @@ Writer::check_existing_picture_mxf ()
        while (true) {
 
                shared_ptr<Job> job = _job.lock ();
-               assert (job);
+               DCPOMATIC_ASSERT (job);
 
                if (N > 0) {
                        job->set_progress (float (_first_nonexistant_frame) / N);
@@ -611,9 +617,7 @@ Writer::write (PlayerSubtitles subs)
        }
        
        if (!_subtitle_content) {
-               _subtitle_content.reset (
-                       new dcp::SubtitleContent (_film->name(), _film->isdcf_metadata().subtitle_language)
-                       );
+               _subtitle_content.reset (new dcp::InteropSubtitleContent (_film->name(), _film->subtitle_language ()));
        }
        
        for (list<dcp::SubtitleString>::const_iterator i = subs.text.begin(); i != subs.text.end(); ++i) {
@@ -621,6 +625,19 @@ Writer::write (PlayerSubtitles subs)
        }
 }
 
+void
+Writer::write (list<shared_ptr<Font> > fonts)
+{
+       if (!_subtitle_content) {
+               _subtitle_content.reset (new dcp::InteropSubtitleContent (_film->name(), _film->subtitle_language ()));
+       }
+       
+       for (list<shared_ptr<Font> >::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
+               /* XXX: this LiberationSans-Regular needs to be a path to a DCP-o-matic-distributed copy */
+               _subtitle_content->add_font ((*i)->id, (*i)->file.get_value_or ("LiberationSans-Regular.ttf").leaf().string ());
+       }
+}
+
 bool
 operator< (QueueItem const & a, QueueItem const & b)
 {