Fix bugs caught by tests.
[dcpomatic.git] / src / lib / make_dcp_job.cc
index 998d1f9c350dc8c91c375ebd452797c4f0bb1b6f..bf01119e4995d9bb520adc30348cea9765e5dfdd 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>
 }
 #include "make_dcp_job.h"
-#include "film_state.h"
 #include "dcp_content_type.h"
 #include "exceptions.h"
+#include "options.h"
+#include "imagemagick_decoder.h"
+#include "film.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using boost::shared_ptr;
 
-/** @param s FilmState of the Film we are making the DCP for.
+/** @param f Film we are making the DCP for.
  *  @param o Options.
- *  @param l Log.
  */
-MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
-       : Job (s, o, l)
+MakeDCPJob::MakeDCPJob (shared_ptr<Film> f, shared_ptr<const Options> o, shared_ptr<Job> req)
+       : Job (f, req)
+       , _opt (o)
 {
        
 }
@@ -46,53 +52,95 @@ 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", _film->name());
 }
 
-void
-MakeDCPJob::run ()
+string
+MakeDCPJob::j2c_path (int f) const
 {
-       set_progress_unknown ();
+       return _opt->frame_out_path (f, false);
+}
 
-       string const dcp_path = _fs->dir (_fs->name);
-       
-       /* Check that we have our prerequisites */
+string
+MakeDCPJob::wav_path (libdcp::Channel c) const
+{
+       return _opt->multichannel_audio_out_path (int (c), false);
+}
 
-       if (!filesystem::exists (filesystem::path (_fs->file ("video.mxf")))) {
-               throw EncodeError ("missing video.mxf");
+void
+MakeDCPJob::run ()
+{
+       if (!_film->dcp_length()) {
+               throw EncodeError ("cannot make a DCP when the source length is not known");
        }
-
-       bool const have_audio = filesystem::exists (filesystem::path (_fs->file ("audio.mxf")));
+       
+       string const dcp_path = _film->dir (_film->dcp_name());
 
        /* Remove any old DCP */
-       filesystem::remove_all (dcp_path);
+       boost::filesystem::remove_all (dcp_path);
+
+       int frames = 0;
+       switch (_film->content_type ()) {
+       case VIDEO:
+               frames = _film->dcp_length().get();
+               break;
+       case STILL:
+               frames = _film->still_duration() * ImageMagickDecoder::static_frames_per_second ();
+               break;
+       }
+       
+       libdcp::DCP dcp (_film->dir (_film->dcp_name()));
+       dcp.Progress.connect (boost::bind (&MakeDCPJob::dcp_progress, this, _1));
 
-       DCP dcp (_fs->dir (_fs->name()));
-       dcp.add_asset (
-               shared_ptr<MainPictureAsset> (new MainPictureAsset ("video.mxf", rint (_fs->frames_per_second), _fs->length, _opt->out_size))
+       shared_ptr<libdcp::CPL> cpl (
+               new libdcp::CPL (_film->dir (_film->dcp_name()), _film->dcp_name(), _film->dcp_content_type()->libdcp_kind (), frames, rint (_film->frames_per_second()))
                );
-
-       if (filesystem::exists (filesystem::path (_fs->file ("audio.mxf")))) {
-               dcp.add_asset (
-                       shared_ptr<MainSoundAsset> (new MainSoundAsset ("audio.mxf", rint (_fs->frames_per_second), _fs->length))
+       
+       dcp.add_cpl (cpl);
+
+       descend (0.9);
+       shared_ptr<libdcp::MonoPictureAsset> pa (
+               new libdcp::MonoPictureAsset (
+                       boost::bind (&MakeDCPJob::j2c_path, this, _1),
+                       _film->dir (_film->dcp_name()),
+                       "video.mxf",
+                       &dcp.Progress,
+                       rint (_film->frames_per_second()),
+                       frames,
+                       _opt->out_size.width,
+                       _opt->out_size.height
+                       )
+               );
+       
+       ascend ();
+
+       shared_ptr<libdcp::SoundAsset> sa;
+
+       if (_film->audio_channels() > 0) {
+               descend (0.1);
+               sa.reset (
+                       new libdcp::SoundAsset (
+                               boost::bind (&MakeDCPJob::wav_path, this, _1),
+                               _film->dir (_film->dcp_name()),
+                               "audio.mxf",
+                               &dcp.Progress,
+                               rint (_film->frames_per_second()),
+                               frames,
+                               _film->audio_channels()
+                               )
                        );
+               ascend ();
        }
-                                                                   
-       dcp.write_xml ();
-
-
-       add_pkl ();
-       add_cpl (pkl[0]);
-
-       add_reel (pkl[0].cpl[0]);
-
-       write_cpl ();
-       write_pkl ();
-       write_volindex ();
-       write_assetmap ();
 
+       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);
 }