Make separate reels for parts of the timeline with no video when
[dcpomatic.git] / src / lib / reel_writer.cc
index c680d3a4e722ed17f0839ea27cbf88c41d471661..8044973eca29adbf056d9af2c250eca08a9cb5e3 100644 (file)
@@ -53,6 +53,7 @@
 using std::list;
 using std::string;
 using std::cout;
+using std::exception;
 using std::map;
 using boost::shared_ptr;
 using boost::optional;
@@ -73,6 +74,7 @@ ReelWriter::ReelWriter (
        , _reel_index (reel_index)
        , _reel_count (reel_count)
        , _content_summary (content_summary)
+       , _job (job)
 {
        /* Create our picture asset in a subdirectory, named according to those
           film's parameters which affect the video output.  We will hard-link
@@ -98,9 +100,6 @@ ReelWriter::ReelWriter (
                _film->internal_video_asset_dir() / _film->internal_video_asset_filename(_period)
                );
 
-       if (job) {
-               job->sub (_("Checking existing image data"));
-       }
        _first_nonexistant_frame = check_existing_picture_asset ();
 
        _picture_asset_writer = _picture_asset->start_write (
@@ -178,16 +177,27 @@ ReelWriter::check_existing_picture_asset ()
        DCPOMATIC_ASSERT (_picture_asset->file());
        boost::filesystem::path asset = _picture_asset->file().get();
 
+       shared_ptr<Job> job = _job.lock ();
+
        /* If there is an existing asset, break any hard links to it as we are about to change its contents
           (if only by changing the IDs); see #1126.
        */
 
        if (boost::filesystem::exists(asset) && boost::filesystem::hard_link_count(asset) > 1) {
-               boost::filesystem::copy_file (asset, asset.string() + ".tmp");
+               if (job) {
+                       job->sub (_("Copying old video file"));
+                       copy_in_bits (asset, asset.string() + ".tmp", bind(&Job::set_progress, job.get(), _1, false));
+               } else {
+                       boost::filesystem::copy_file (asset, asset.string() + ".tmp");
+               }
                boost::filesystem::remove (asset);
                boost::filesystem::rename (asset.string() + ".tmp", asset);
        }
 
+       if (job) {
+               job->sub (_("Checking existing image data"));
+       }
+
        /* Try to open the existing asset */
        FILE* asset_file = fopen_boost (asset, "rb");
        if (!asset_file) {
@@ -289,14 +299,29 @@ ReelWriter::finish ()
                video_to /= _film->dir (_film->dcp_name());
                video_to /= video_asset_filename (_picture_asset, _reel_index, _reel_count, _content_summary);
 
+               LOG_GENERAL ("Must put %1 into DCP at %2", video_from.string(), video_to.string());
+               LOG_GENERAL ("%2: %2", video_from.string(), boost::filesystem::is_regular_file(video_from) ? "yes" : "no");
+               LOG_GENERAL ("%1: %2", video_to.string(), boost::filesystem::is_regular_file(video_to) ? "yes" : "no");
+
                boost::system::error_code ec;
                boost::filesystem::create_hard_link (video_from, video_to, ec);
                if (ec) {
-                       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);
+                       LOG_WARNING ("Hard-link failed (%1); copying instead", ec.message());
+                       shared_ptr<Job> job = _job.lock ();
+                       if (job) {
+                               job->sub (_("Copying video file into DCP"));
+                               try {
+                                       copy_in_bits (video_from, video_to, bind(&Job::set_progress, job.get(), _1, false));
+                               } catch (exception& e) {
+                                       LOG_ERROR ("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), e.what());
+                                       throw FileError (e.what(), video_from);
+                               }
+                       } else {
+                               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);
+                               }
                        }
                }
 
@@ -414,10 +439,14 @@ maybe_add_text (
 shared_ptr<dcp::Reel>
 ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr<Font> > const & fonts)
 {
+       LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
+
        shared_ptr<dcp::Reel> reel (new dcp::Reel ());
 
        shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
 
+       LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
+
        if (_picture_asset) {
                /* We have made a picture asset of our own.  Put it into the reel */
                shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (_picture_asset);
@@ -443,8 +472,6 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                }
        }
 
-       LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
-
        Frame const period_duration = _period.duration().frames_round(_film->video_frame_rate());
 
        DCPOMATIC_ASSERT (reel_picture_asset);
@@ -467,9 +494,13 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                /* We have made a sound asset of our own.  Put it into the reel */
                reel_sound_asset.reset (new dcp::ReelSoundAsset (_sound_asset, 0));
        } else {
+               LOG_GENERAL ("no sound asset of our own; look through %1", refs.size());
                /* We don't have a sound asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
                        shared_ptr<dcp::ReelSoundAsset> k = dynamic_pointer_cast<dcp::ReelSoundAsset> (j.asset);
+                       if (k) {
+                               LOG_GENERAL ("candidate sound asset period is %1-%2", j.period.from.get(), j.period.to.get());
+                       }
                        if (k && j.period == _period) {
                                reel_sound_asset = k;
                                /* If we have a hash for this asset in the CPL, assume that it is correct */