Fix pull timing; fix units of ReelWriter::total_written_audio_frames.
[dcpomatic.git] / src / lib / reel_writer.cc
index eff5c48eeede1342e2b61f2dee07abb9f3abbd74..e008bf6311ae5acbcbcfa8a1b1d41bbfb75aa225 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -42,7 +42,6 @@
 #include <dcp/interop_subtitle_asset.h>
 #include <dcp/smpte_subtitle_asset.h>
 #include <boost/foreach.hpp>
-#include <boost/make_shared.hpp>
 
 #include "i18n.h"
 
@@ -55,20 +54,24 @@ using std::list;
 using std::string;
 using std::cout;
 using boost::shared_ptr;
-using boost::make_shared;
 using boost::optional;
 using boost::dynamic_pointer_cast;
 using dcp::Data;
 
 int const ReelWriter::_info_size = 48;
 
-ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job)
+ReelWriter::ReelWriter (
+       shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job, int reel_index, int reel_count, optional<string> content_summary
+       )
        : _film (film)
        , _period (period)
        , _first_nonexistant_frame (0)
        , _last_written_video_frame (-1)
        , _last_written_eyes (EYES_RIGHT)
        , _total_written_audio_frames (0)
+       , _reel_index (reel_index)
+       , _reel_count (reel_count)
+       , _content_summary (content_summary)
 {
        /* Create our picture asset in a subdirectory, named according to those
           film's parameters which affect the video output.  We will hard-link
@@ -85,6 +88,7 @@ ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, share
 
        if (_film->encrypted ()) {
                _picture_asset->set_key (_film->key ());
+               _picture_asset->set_context_id (_film->context_id ());
        }
 
        _picture_asset->set_file (
@@ -109,11 +113,13 @@ ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, share
                        _sound_asset->set_key (_film->key ());
                }
 
+               DCPOMATIC_ASSERT (_film->directory());
+
                /* Write the sound asset into the film directory so that we leave the creation
                   of the DCP directory until the last minute.
                */
                _sound_asset_writer = _sound_asset->start_write (
-                       _film->directory() / audio_asset_filename (_sound_asset),
+                       _film->directory().get() / audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary),
                        _film->interop() ? dcp::INTEROP : dcp::SMPTE
                        );
        }
@@ -125,13 +131,27 @@ ReelWriter::write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const
 {
        FILE* file = 0;
        boost::filesystem::path info_file = _film->info_file (_period);
-       if (boost::filesystem::exists (info_file)) {
+
+       bool const read = boost::filesystem::exists (info_file);
+
+#ifdef DCPOMATIC_WINDOWS
+       if (read) {
+               LOG_GENERAL (
+                       "Checked %1 (which exists) length is %2 perms are %3",
+                       info_file, boost::filesystem::file_size (info_file), int(boost::filesystem::status(info_file).permissions())
+                       );
+       } else {
+               LOG_GENERAL ("Checked %1 (which does not exist)", info_file);
+       }
+#endif
+
+       if (read) {
                file = fopen_boost (info_file, "r+b");
        } else {
                file = fopen_boost (info_file, "wb");
        }
        if (!file) {
-               throw OpenFileError (info_file);
+               throw OpenFileError (info_file, errno, read);
        }
        dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
        fwrite (&info.offset, sizeof (info.offset), 1, file);
@@ -177,12 +197,13 @@ void
 ReelWriter::check_existing_picture_asset ()
 {
        /* Try to open the existing asset */
-       FILE* asset_file = fopen_boost (_picture_asset->file(), "rb");
+       DCPOMATIC_ASSERT (_picture_asset->file());
+       FILE* asset_file = fopen_boost (_picture_asset->file().get(), "rb");
        if (!asset_file) {
-               LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", _picture_asset->file().string(), errno);
+               LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", _picture_asset->file()->string(), errno);
                return;
        } else {
-               LOG_GENERAL ("Opened existing asset at %1", _picture_asset->file().string());
+               LOG_GENERAL ("Opened existing asset at %1", _picture_asset->file()->string());
        }
 
        /* Offset of the last dcp::FrameInfo in the info file */
@@ -255,6 +276,7 @@ ReelWriter::finish ()
 {
        if (!_picture_asset_writer->finalize ()) {
                /* Nothing was written to the picture asset */
+               LOG_GENERAL ("Nothing was written to reel %1 of %2", _reel_index, _reel_count);
                _picture_asset.reset ();
        }
 
@@ -265,10 +287,11 @@ ReelWriter::finish ()
 
        /* Hard-link any video asset file into the DCP */
        if (_picture_asset) {
-               boost::filesystem::path video_from = _picture_asset->file ();
+               DCPOMATIC_ASSERT (_picture_asset->file());
+               boost::filesystem::path video_from = _picture_asset->file().get();
                boost::filesystem::path video_to;
                video_to /= _film->dir (_film->dcp_name());
-               video_to /= video_asset_filename (_picture_asset);
+               video_to /= video_asset_filename (_picture_asset, _reel_index, _reel_count, _content_summary);
 
                boost::system::error_code ec;
                boost::filesystem::create_hard_link (video_from, video_to, ec);
@@ -288,13 +311,14 @@ ReelWriter::finish ()
        if (_sound_asset) {
                boost::filesystem::path audio_to;
                audio_to /= _film->dir (_film->dcp_name ());
-               audio_to /= audio_asset_filename (_sound_asset);
+               string const aaf = audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary);
+               audio_to /= aaf;
 
                boost::system::error_code ec;
-               boost::filesystem::rename (_film->file (audio_asset_filename (_sound_asset)), audio_to, ec);
+               boost::filesystem::rename (_film->file (aaf), audio_to, ec);
                if (ec) {
                        throw FileError (
-                               String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), audio_asset_filename (_sound_asset)
+                               String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), aaf
                                );
                }
 
@@ -305,7 +329,7 @@ ReelWriter::finish ()
 shared_ptr<dcp::Reel>
 ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr<Font> > const & fonts)
 {
-       shared_ptr<dcp::Reel> reel = make_shared<dcp::Reel> ();
+       shared_ptr<dcp::Reel> reel (new dcp::Reel ());
 
        shared_ptr<dcp::ReelPictureAsset> reel_picture_asset;
 
@@ -321,16 +345,24 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                        reel_picture_asset.reset (new dcp::ReelStereoPictureAsset (stereo, 0));
                }
        } else {
+               LOG_GENERAL ("no picture asset of our own; look through %1", refs.size());
                /* We don't have a picture asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
                        shared_ptr<dcp::ReelPictureAsset> k = dynamic_pointer_cast<dcp::ReelPictureAsset> (j.asset);
+                       if (k) {
+                               LOG_GENERAL ("candidate picture asset period is %1-%2", j.period.from.get(), j.period.to.get());
+                       }
                        if (k && j.period == _period) {
                                reel_picture_asset = k;
                        }
                }
        }
 
+       LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
+
+       DCPOMATIC_ASSERT (reel_picture_asset);
        reel->add (reel_picture_asset);
+
        /* If we have a hash for this asset in the CPL, assume that it is correct */
        if (reel_picture_asset->hash()) {
                reel_picture_asset->asset_ref()->set_hash (reel_picture_asset->hash().get());
@@ -338,7 +370,7 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
 
        if (_sound_asset) {
                /* We have made a sound asset of our own.  Put it into the reel */
-               reel->add (make_shared<dcp::ReelSoundAsset> (_sound_asset, 0));
+               reel->add (shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (_sound_asset, 0)));
        } else {
                /* We don't have a sound asset of our own; hopefully we have one to reference */
                BOOST_FOREACH (ReferencedReelAsset j, refs) {
@@ -419,16 +451,14 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
 }
 
 void
-ReelWriter::calculate_digests (shared_ptr<Job> job)
+ReelWriter::calculate_digests (boost::function<void (float)> set_progress)
 {
-       job->sub (_("Computing image digest"));
        if (_picture_asset) {
-               _picture_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
+               _picture_asset->hash (set_progress);
        }
 
        if (_sound_asset) {
-               job->sub (_("Computing audio digest"));
-               _sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
+               _sound_asset->hash (set_progress);
        }
 }
 
@@ -450,7 +480,7 @@ ReelWriter::write (shared_ptr<const AudioBuffers> audio)
                _sound_asset_writer->write (audio->data(), audio->frames());
        }
 
-       ++_total_written_audio_frames;
+       _total_written_audio_frames += audio->frames ();
 }
 
 void
@@ -462,24 +492,27 @@ ReelWriter::write (PlayerSubtitles subs)
                        lang = "Unknown";
                }
                if (_film->interop ()) {
-                       shared_ptr<dcp::InteropSubtitleAsset> s = make_shared<dcp::InteropSubtitleAsset> ();
+                       shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset ());
                        s->set_movie_title (_film->name ());
                        s->set_language (lang);
                        s->set_reel_number ("1");
                        _subtitle_asset = s;
                } else {
-                       shared_ptr<dcp::SMPTESubtitleAsset> s = make_shared<dcp::SMPTESubtitleAsset> ();
+                       shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset ());
                        s->set_content_title_text (_film->name ());
                        s->set_language (lang);
                        s->set_edit_rate (dcp::Fraction (_film->video_frame_rate (), 1));
                        s->set_reel_number (1);
                        s->set_time_code_rate (_film->video_frame_rate ());
                        s->set_start_time (dcp::Time ());
+                       if (_film->encrypted ()) {
+                               s->set_key (_film->key ());
+                       }
                        _subtitle_asset = s;
                }
        }
 
-       BOOST_FOREACH (dcp::SubtitleString i, subs.text) {
+       BOOST_FOREACH (SubtitleString i, subs.text) {
                i.set_in  (i.in()  - dcp::Time (_period.from.seconds(), i.in().tcr));
                i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr));
                _subtitle_asset->add (i);