Tidy up test film naming.
[dcpomatic.git] / src / lib / make_dcp_job.cc
index 81deb835d4df23f73a80f08f782e1c98df5caff0..fcfce4b07bccc6ada73799e79e2f68e8dc6064db 100644 (file)
  */
 
 #include <boost/filesystem.hpp>
+#include <libdcp/dcp.h>
+#include <libdcp/picture_asset.h>
+#include <libdcp/sound_asset.h>
+#include <libdcp/reel.h>
 extern "C" {
 #include <libavutil/pixdesc.h>
 }
@@ -29,6 +33,8 @@ extern "C" {
 #include "film_state.h"
 #include "dcp_content_type.h"
 #include "exceptions.h"
+#include "options.h"
+#include "imagemagick_decoder.h"
 
 using namespace std;
 using namespace boost;
@@ -37,8 +43,9 @@ using namespace boost;
  *  @param o Options.
  *  @param l Log.
  */
-MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
-       : ShellCommandJob (s, o, l)
+MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l, shared_ptr<Job> req)
+       : Job (s, l, req)
+       , _opt (o)
 {
        
 }
@@ -46,49 +53,91 @@ MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options>
 string
 MakeDCPJob::name () const
 {
-       stringstream s;
-       s << "Make DCP for " << _fs->name;
-       return s.str ();
+       return String::compose ("Make DCP for %1", _fs->name());
 }
 
-void
-MakeDCPJob::run ()
+string
+MakeDCPJob::j2c_path (int f) const
 {
-       set_progress_unknown ();
-
-       string const dcp_path = _fs->dir (_fs->name);
-       
-       /* Check that we have our prerequisites */
+       return _opt->frame_out_path (f, false);
+}
 
-       if (!filesystem::exists (filesystem::path (_fs->file ("video.mxf")))) {
-               throw EncodeError ("missing video.mxf");
-       }
+string
+MakeDCPJob::wav_path (libdcp::Channel c) const
+{
+       return _opt->multichannel_audio_out_path (int (c), false);
+}
 
-       bool const have_audio = filesystem::exists (filesystem::path (_fs->file ("audio.mxf")));
+void
+MakeDCPJob::run ()
+{
+       string const dcp_path = _fs->dir (_fs->dcp_name());
 
        /* Remove any old DCP */
        filesystem::remove_all (dcp_path);
 
-       /* Re-make the DCP directory */
-       _fs->dir (_fs->name);
+       int frames = 0;
+       switch (_fs->content_type ()) {
+       case VIDEO:
+               frames = _fs->dcp_length ();
+               break;
+       case STILL:
+               frames = _fs->still_duration() * ImageMagickDecoder::static_frames_per_second ();
+               break;
+       }
+       
+       libdcp::DCP dcp (_fs->dir (_fs->dcp_name()));
+       dcp.Progress.connect (sigc::mem_fun (*this, &MakeDCPJob::dcp_progress));
+
+       shared_ptr<libdcp::CPL> cpl (
+               new libdcp::CPL (_fs->dir (_fs->dcp_name()), _fs->dcp_name(), _fs->dcp_content_type()->libdcp_kind (), frames, rint (_fs->frames_per_second()))
+               );
        
-       stringstream c;
-       c << "cd \"" << dcp_path << "\" && "
-         << " opendcp_xml -d -a " << _fs->name
-         << " -t \"" << _fs->name << "\""
-         << " -k " << _fs->dcp_content_type->opendcp_name()
-         << " --reel \"" << _fs->file ("video.mxf") << "\"";
+       dcp.add_cpl (cpl);
+
+       descend (0.9);
+       shared_ptr<libdcp::MonoPictureAsset> pa (
+               new libdcp::MonoPictureAsset (
+                       sigc::mem_fun (*this, &MakeDCPJob::j2c_path),
+                       _fs->dir (_fs->dcp_name()),
+                       "video.mxf",
+                       &dcp.Progress,
+                       rint (_fs->frames_per_second()),
+                       frames,
+                       _opt->out_size.width,
+                       _opt->out_size.height
+                       )
+               );
        
-       if (have_audio) {
-               c << " \"" << _fs->file ("audio.mxf") << "\"";
+       ascend ();
+
+       shared_ptr<libdcp::SoundAsset> sa;
+
+       if (_fs->audio_channels() > 0) {
+               descend (0.1);
+               sa.reset (
+                       new libdcp::SoundAsset (
+                               sigc::mem_fun (*this, &MakeDCPJob::wav_path),
+                               _fs->dir (_fs->dcp_name()),
+                               "audio.mxf",
+                               &dcp.Progress,
+                               rint (_fs->frames_per_second()),
+                               frames,
+                               _fs->audio_channels()
+                               )
+                       );
+               ascend ();
        }
 
-       command (c.str ());
-
-       filesystem::rename (filesystem::path (_fs->file ("video.mxf")), filesystem::path (dcp_path + "/video.mxf"));
-       if (have_audio) {
-               filesystem::rename (filesystem::path (_fs->file ("audio.mxf")), filesystem::path (dcp_path + "/audio.mxf"));
-       }
+       cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (pa, sa, shared_ptr<libdcp::SubtitleAsset> ())));
+       dcp.write_xml ();
 
        set_progress (1);
+       set_state (FINISHED_OK);
+}
+
+void
+MakeDCPJob::dcp_progress (float p)
+{
+       set_progress (p);
 }