Try to fix up paths for video MXFs, hashes and temporarily-stored frames.
[dcpomatic.git] / src / lib / writer.cc
index fbd371550fd9735ad68a65b46f64cbe684ffc7e1..16ed5c3493cdf717f08e20607bd2f8756441364f 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <libdcp/picture_asset.h>
 #include <libdcp/sound_asset.h>
+#include <libdcp/reel.h>
 #include "writer.h"
 #include "compose.hpp"
 #include "film.h"
@@ -32,7 +33,7 @@ using boost::shared_ptr;
 
 unsigned int const Writer::_maximum_frames_in_memory = 8;
 
-Writer::Writer (shared_ptr<const Film> f)
+Writer::Writer (shared_ptr<Film> f)
        : _film (f)
        , _thread (0)
        , _finish (false)
@@ -41,7 +42,7 @@ Writer::Writer (shared_ptr<const Film> f)
        _picture_asset.reset (
                new libdcp::MonoPictureAsset (
                        _film->dir (_film->dcp_name()),
-                       String::compose ("video_%1.mxf", 0),
+                       _film->video_mxf_path(),
                        DCPFrameRate (_film->frames_per_second()).frames_per_second,
                        _film->format()->dcp_size()
                        )
@@ -53,10 +54,10 @@ Writer::Writer (shared_ptr<const Film> f)
                _sound_asset.reset (
                        new libdcp::SoundAsset (
                                _film->dir (_film->dcp_name()),
-                               String::compose ("audio_%1.mxf", 0),
+                               "audio.mxf",
                                DCPFrameRate (_film->frames_per_second()).frames_per_second,
                                _film->audio_channels(),
-                               _film->audio_stream()->sample_rate()
+                               dcp_audio_sample_rate (_film->audio_stream()->sample_rate())
                                )
                        );
 
@@ -123,9 +124,11 @@ Writer::thread ()
                        _film->log()->log (String::compose ("Writer writes %1 to MXF", encoded.second));
                        if (encoded.first) {
                                _picture_asset_writer->write (encoded.first->data(), encoded.first->size());
+                               encoded.first->write_hash (_film, encoded.second);
                                _last_written = encoded.first;
                        } else {
                                _picture_asset_writer->write (_last_written->data(), _last_written->size());
+                               _last_written->write_hash (_film, encoded.second);
                        }
                        lock.lock ();
 
@@ -161,9 +164,9 @@ Writer::thread ()
                        lock.unlock ();
                        _film->log()->log (String::compose ("Writer pulls %1 back from disk", fetch));
                        shared_ptr<const EncodedData> encoded;
-                       if (boost::filesystem::exists (_film->frame_out_path (fetch, false))) {
+                       if (boost::filesystem::exists (_film->j2c_path (fetch, false))) {
                                /* It's an actual frame (not a repeat-last); load it in */
-                               encoded.reset (new EncodedData (_film->frame_out_path (fetch, false)));
+                               encoded.reset (new EncodedData (_film->j2c_path (fetch, false)));
                        }
                        lock.lock ();
 
@@ -191,7 +194,41 @@ Writer::finish ()
        _thread = 0;
 
        _picture_asset_writer->finalize ();
-       _sound_asset_writer->finalize ();
+
+       if (_sound_asset_writer) {
+               _sound_asset_writer->finalize ();
+       }
+
+       int const frames = _last_written_frame + 1;
+       int const duration = frames - _film->trim_start() - _film->trim_end();
+       
+       _film->set_dcp_intrinsic_duration (frames);
+       
+       _picture_asset->set_entry_point (_film->trim_start ());
+       _picture_asset->set_duration (duration);
+
+       if (_sound_asset) {
+               _sound_asset->set_entry_point (_film->trim_start ());
+               _sound_asset->set_duration (duration);
+       }
+       
+       libdcp::DCP dcp (_film->dir (_film->dcp_name()));
+       DCPFrameRate dfr (_film->frames_per_second ());
+
+       shared_ptr<libdcp::CPL> cpl (
+               new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, dfr.frames_per_second)
+               );
+       
+       dcp.add_cpl (cpl);
+
+       cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (
+                                                        _picture_asset,
+                                                        _sound_asset,
+                                                        shared_ptr<libdcp::SubtitleAsset> ()
+                                                        )
+                              ));
+
+       dcp.write_xml ();
 }
 
 /** Tell the writer that frame `f' should be a repeat of the frame before it */