Pass options only to jobs that need them.
[dcpomatic.git] / test / test.cc
index 638d526e05f760303a6b98945e86f098e27c961a..5aed18c523043512729a14533c85fbb9ccc56350 100644 (file)
@@ -34,6 +34,9 @@
 #include "dcp_video_frame.h"
 #include "config.h"
 #include "server.h"
+#include "cross.h"
+#include "job.h"
+#include "subtitle.h"
 #define BOOST_TEST_DYN_LINK
 #define BOOST_TEST_MODULE dvdomatic_test
 #include <boost/test/unit_test.hpp>
 using namespace std;
 using namespace boost;
 
+void
+setup_test_config ()
+{
+       Config::instance()->set_num_local_encoding_threads (1);
+       Config::instance()->set_colour_lut_index (0);
+       Config::instance()->set_j2k_bandwidth (200000000);
+       Config::instance()->set_servers (vector<ServerDescription*> ());
+       Config::instance()->set_server_port (61920);
+}
+
 BOOST_AUTO_TEST_CASE (film_metadata_test)
 {
        dvdomatic_setup ();
+       setup_test_config ();
        
        string const test_film = "build/test/film";
        
@@ -104,11 +118,11 @@ BOOST_AUTO_TEST_CASE (format_test)
        
        Format const * f = Format::from_nickname ("Flat");
        BOOST_CHECK (f);
-       BOOST_CHECK_EQUAL (f->ratio_as_integer(), 185);
+       BOOST_CHECK_EQUAL (f->ratio_as_integer(0), 185);
        
        f = Format::from_nickname ("Scope");
        BOOST_CHECK (f);
-       BOOST_CHECK_EQUAL (f->ratio_as_integer(), 239);
+       BOOST_CHECK_EQUAL (f->ratio_as_integer(0), 239);
 }
 
 BOOST_AUTO_TEST_CASE (util_test)
@@ -244,18 +258,20 @@ BOOST_AUTO_TEST_CASE (md5_digest_test)
 BOOST_AUTO_TEST_CASE (paths_test)
 {
        FilmState s;
-       s.directory = "build/test/a/b/c/d/e";
-       s.thumbs.push_back (42);
-       BOOST_CHECK_EQUAL (s.thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.tiff");
+       s.set_directory ("build/test/a/b/c/d/e");
+       vector<int> thumbs;
+       thumbs.push_back (42);
+       s.set_thumbs (thumbs);
+       BOOST_CHECK_EQUAL (s.thumb_file (0), "build/test/a/b/c/d/e/thumbs/00000042.png");
 
-       s.content = "/foo/bar/baz";
+       s._content = "/foo/bar/baz";
        BOOST_CHECK_EQUAL (s.content_path(), "/foo/bar/baz");
-       s.content = "foo/bar/baz";
+       s._content = "foo/bar/baz";
        BOOST_CHECK_EQUAL (s.content_path(), "build/test/a/b/c/d/e/foo/bar/baz");
 }
 
 void
-do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded)
+do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* description, shared_ptr<EncodedData> locally_encoded, int N)
 {
        shared_ptr<EncodedData> remotely_encoded;
        BOOST_CHECK_NO_THROW (remotely_encoded = frame->encode_remotely (description));
@@ -267,9 +283,7 @@ do_remote_encode (shared_ptr<DCPVideoFrame> frame, ServerDescription* descriptio
 
 BOOST_AUTO_TEST_CASE (client_server_test)
 {
-       shared_ptr<SimpleImage> image (new SimpleImage (PIX_FMT_RGB24, Size (1998, 1080)));
-       image->set_line_size (0, 1998 * 3);
-
+       shared_ptr<Image> image (new CompactImage (PIX_FMT_RGB24, Size (1998, 1080)));
        uint8_t* p = image->data()[0];
        
        for (int y = 0; y < 1080; ++y) {
@@ -280,13 +294,29 @@ BOOST_AUTO_TEST_CASE (client_server_test)
                }
        }
 
+       shared_ptr<Image> sub_image (new CompactImage (PIX_FMT_RGBA, Size (100, 200)));
+       p = sub_image->data()[0];
+       for (int y = 0; y < 200; ++y) {
+               for (int x = 0; x < 100; ++x) {
+                       *p++ = y % 256;
+                       *p++ = x % 256;
+                       *p++ = (x + y) % 256;
+                       *p++ = 1;
+               }
+       }
+
+       shared_ptr<Subtitle> subtitle (new Subtitle (Position (50, 60), sub_image));
+
        FileLog log ("build/test/client_server_test.log");
 
        shared_ptr<DCPVideoFrame> frame (
                new DCPVideoFrame (
                        image,
+                       subtitle,
                        Size (1998, 1080),
                        0,
+                       0,
+                       1,
                        Scaler::from_id ("bicubic"),
                        0,
                        24,
@@ -298,20 +328,188 @@ BOOST_AUTO_TEST_CASE (client_server_test)
                );
 
        shared_ptr<EncodedData> locally_encoded = frame->encode_locally ();
+       BOOST_ASSERT (locally_encoded);
        
-       Config::instance()->set_server_port (61920);
        Server* server = new Server (&log);
 
        new thread (boost::bind (&Server::run, server, 2));
 
+       /* Let the server get itself ready */
+       dvdomatic_sleep (1);
+
        ServerDescription description ("localhost", 2);
 
        list<thread*> threads;
        for (int i = 0; i < 8; ++i) {
-               threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded)));
+               threads.push_back (new thread (boost::bind (do_remote_encode, frame, &description, locally_encoded, i)));
        }
 
        for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
                (*i)->join ();
        }
+
+       for (list<thread*>::iterator i = threads.begin(); i != threads.end(); ++i) {
+               delete *i;
+       }
+}
+
+BOOST_AUTO_TEST_CASE (make_dcp_test)
+{
+       string const test_film = "build/test/film2";
+       
+       if (boost::filesystem::exists (test_film)) {
+               boost::filesystem::remove_all (test_film);
+       }
+       
+       Film film (test_film, false);
+       film.set_name ("test_film");
+       film.set_content ("../../../test/test.mp4");
+       film.examine_content ();
+       film.set_format (Format::from_nickname ("Flat"));
+       film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
+       film.make_dcp (true);
+
+       while (JobManager::instance()->work_to_do ()) {
+               dvdomatic_sleep (1);
+       }
+       
+       BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
+}
+
+BOOST_AUTO_TEST_CASE (make_dcp_with_range_test)
+{
+       string const test_film = "build/test/film3";
+       
+       if (boost::filesystem::exists (test_film)) {
+               boost::filesystem::remove_all (test_film);
+       }
+       
+       Film film (test_film, false);
+       film.set_name ("test_film");
+       film.set_content ("../../../test/test.mp4");
+       film.examine_content ();
+       film.set_format (Format::from_nickname ("Flat"));
+       film.set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
+       film.set_dcp_frames (42);
+       film.make_dcp (true);
+
+       while (JobManager::instance()->work_to_do() && !JobManager::instance()->errors()) {
+               dvdomatic_sleep (1);
+       }
+
+       BOOST_CHECK_EQUAL (JobManager::instance()->errors(), false);
+}
+
+BOOST_AUTO_TEST_CASE (audio_sampling_rate_test)
+{
+       FilmState fs;
+       fs.set_frames_per_second (24);
+
+       fs.set_audio_sample_rate (48000);
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
+
+       fs.set_audio_sample_rate (44100);
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 48000);
+
+       fs.set_audio_sample_rate (80000);
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 96000);
+
+       fs.set_frames_per_second (23.976);
+       fs.set_audio_sample_rate (48000);
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
+
+       fs.set_frames_per_second (29.97);
+       fs.set_audio_sample_rate (48000);
+       BOOST_CHECK_EQUAL (fs.target_sample_rate(), 47952);
+}
+
+class TestJob : public Job
+{
+public:
+       TestJob (shared_ptr<const FilmState> s, Log* l, shared_ptr<Job> req)
+               : Job (s, l, req)
+       {
+
+       }
+
+       void set_finished_ok () {
+               set_state (FINISHED_OK);
+       }
+
+       void set_finished_error () {
+               set_state (FINISHED_ERROR);
+       }
+
+       void run ()
+       {
+               while (1) {
+                       if (finished ()) {
+                               return;
+                       }
+               }
+       }
+
+       string name () const {
+               return "";
+       }
+};
+
+BOOST_AUTO_TEST_CASE (job_manager_test)
+{
+       shared_ptr<const FilmState> s;
+       FileLog log ("build/test/job_manager_test.log");
+
+       /* Single job, no dependency */
+       shared_ptr<TestJob> a (new TestJob (s, &log, shared_ptr<Job> ()));
+
+       JobManager::instance()->add (a);
+       dvdomatic_sleep (1);
+       BOOST_CHECK_EQUAL (a->running (), true);
+       a->set_finished_ok ();
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (a->finished_ok(), true);
+
+       /* Two jobs, dependency */
+       a.reset (new TestJob (s, &log, shared_ptr<Job> ()));
+       shared_ptr<TestJob> b (new TestJob (s, &log, a));
+
+       JobManager::instance()->add (a);
+       JobManager::instance()->add (b);
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (a->running(), true);
+       BOOST_CHECK_EQUAL (b->running(), false);
+       a->set_finished_ok ();
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (a->finished_ok(), true);
+       BOOST_CHECK_EQUAL (b->running(), true);
+       b->set_finished_ok ();
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (b->finished_ok(), true);
+
+       /* Two jobs, dependency, first fails */
+       a.reset (new TestJob (s, &log, shared_ptr<Job> ()));
+       b.reset (new TestJob (s, &log, a));
+
+       JobManager::instance()->add (a);
+       JobManager::instance()->add (b);
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (a->running(), true);
+       BOOST_CHECK_EQUAL (b->running(), false);
+       a->set_finished_error ();
+       dvdomatic_sleep (2);
+       BOOST_CHECK_EQUAL (a->finished_in_error(), true);
+       BOOST_CHECK_EQUAL (b->running(), false);
+}
+
+BOOST_AUTO_TEST_CASE (stream_test)
+{
+       AudioStream a ("4 9 hello there world");
+       BOOST_CHECK_EQUAL (a.id(), 4);
+       BOOST_CHECK_EQUAL (a.channels(), 9);
+       BOOST_CHECK_EQUAL (a.name(), "hello there world");
+       BOOST_CHECK_EQUAL (a.to_string(), "4 9 hello there world");
+
+       SubtitleStream s ("5 a b c");
+       BOOST_CHECK_EQUAL (s.id(), 5);
+       BOOST_CHECK_EQUAL (s.name(), "a b c");
 }