Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / player_test.cc
index c1e2d2cbe971366bb51c5a07d55fe469ae86a837..80c7cf9eb44fc5509c7dd33a9fc1eb3493c08449 100644 (file)
  */
 
 
-#include "lib/film.h"
-#include "lib/ffmpeg_content.h"
-#include "lib/dcp_content_type.h"
-#include "lib/ratio.h"
 #include "lib/audio_buffers.h"
-#include "lib/player.h"
-#include "lib/video_content.h"
-#include "lib/image_content.h"
-#include "lib/string_text_file_content.h"
-#include "lib/content_factory.h"
-#include "lib/dcp_content.h"
-#include "lib/text_content.h"
 #include "lib/butler.h"
 #include "lib/compose.hpp"
+#include "lib/content_factory.h"
 #include "lib/cross.h"
+#include "lib/dcp_content.h"
+#include "lib/dcp_content_type.h"
+#include "lib/ffmpeg_content.h"
+#include "lib/film.h"
+#include "lib/image_content.h"
+#include "lib/player.h"
+#include "lib/ratio.h"
+#include "lib/string_text_file_content.h"
+#include "lib/text_content.h"
+#include "lib/video_content.h"
 #include "test.h"
 #include <boost/test/unit_test.hpp>
 #include <boost/algorithm/string.hpp>
@@ -48,7 +48,6 @@
 
 using std::cout;
 using std::list;
-using std::pair;
 using std::shared_ptr;
 using std::make_shared;
 using boost::bind;
@@ -84,7 +83,7 @@ BOOST_AUTO_TEST_CASE (player_silence_padding_test)
 
        accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->Audio.connect (bind (&accumulate, _1, _2));
        while (!player->pass ()) {}
        BOOST_REQUIRE (accumulated->frames() >= 48000);
@@ -164,7 +163,7 @@ BOOST_AUTO_TEST_CASE (player_subframe_test)
        /* Length should be rounded up from B's length to the next video frame */
        BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->setup_pieces ();
        BOOST_REQUIRE_EQUAL (player->_black._periods.size(), 1U);
        BOOST_CHECK (player->_black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
@@ -206,12 +205,12 @@ BOOST_AUTO_TEST_CASE (player_interleave_test)
        film->examine_and_add_content (s);
        BOOST_REQUIRE (!wait_for_jobs ());
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->Video.connect (bind (&video, _1, _2));
        player->Audio.connect (bind (&audio, _1, _2));
        video_frames = audio_frames = 0;
        while (!player->pass ()) {
-               BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) < 8);
+               BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
        }
 }
 
@@ -229,20 +228,21 @@ BOOST_AUTO_TEST_CASE (player_seek_test)
        BOOST_REQUIRE (!wait_for_jobs ());
        dcp->only_text()->set_use (true);
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->set_fast ();
        player->set_always_burn_open_subtitles ();
        player->set_play_referenced ();
 
-       auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true, false);
-       butler->disable_audio();
+       auto butler = std::make_shared<Butler>(
+               film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
+               );
 
        for (int i = 0; i < 10; ++i) {
                auto t = DCPTime::from_frames (i, 24);
                butler->seek (t, true);
-               auto video = butler->get_video(true, 0);
+               auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
                BOOST_CHECK_EQUAL(video.second.get(), t.get());
-               write_image(video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true), String::compose("build/test/player_seek_test_%1.png", i));
+               write_image(video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
                /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
                   (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
                   text altogether.
@@ -261,23 +261,24 @@ BOOST_AUTO_TEST_CASE (player_seek_test2)
        BOOST_REQUIRE (!wait_for_jobs ());
        dcp->only_text()->set_use (true);
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->set_fast ();
        player->set_always_burn_open_subtitles ();
        player->set_play_referenced ();
 
-       auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 2, bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true, false);
-       butler->disable_audio();
+       auto butler = std::make_shared<Butler>
+               (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
+                );
 
        butler->seek(DCPTime::from_seconds(5), true);
 
        for (int i = 0; i < 10; ++i) {
                auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
                butler->seek (t, true);
-               auto video = butler->get_video(true, 0);
+               auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
                BOOST_CHECK_EQUAL(video.second.get(), t.get());
                write_image(
-                       video.first->image(bind(PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true), String::compose("build/test/player_seek_test2_%1.png", i)
+                       video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
                        );
                check_image(TestPaths::private_data() / String::compose("player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 14.08);
        }
@@ -335,7 +336,7 @@ BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
        text->only_text()->set_type (TextType::CLOSED_CAPTION);
        text->only_text()->set_use (true);
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->set_ignore_video ();
        player->set_ignore_audio ();
 
@@ -355,9 +356,11 @@ BOOST_AUTO_TEST_CASE (player_trim_crash)
        film->examine_and_add_content (boon);
        BOOST_REQUIRE (!wait_for_jobs());
 
-       auto player = std::make_shared<Player>(film, false);
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
        player->set_fast ();
-       auto butler = std::make_shared<Butler>(film, player, AudioMapping(), 6, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true, false);
+       auto butler = std::make_shared<Butler>(
+               film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
+               );
 
        /* Wait for the butler to fill */
        dcpomatic_sleep_seconds (5);
@@ -467,3 +470,71 @@ BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
        film2->set_video_frame_rate (24);
        make_and_verify_dcp (film2);
 }
+
+
+/** #2257 */
+BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
+{
+       auto content = content_factory("test/data/flat_red.png").front();
+       auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
+       int constexpr length = 24 * 25;
+       content->video->set_length(length);
+       film->set_encrypted (true);
+       make_and_verify_dcp (
+               film,
+               {
+                       dcp::VerificationNote::Code::MISSING_CPL_METADATA,
+               });
+
+       auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
+       auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
+
+       auto player = std::make_shared<Player>(film2, Image::Alignment::COMPACT);
+       Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
+
+       float buffer[2000 * 6];
+       for (int i = 0; i < length; ++i) {
+               butler.get_video(Butler::Behaviour::BLOCKING, 0);
+               butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
+       }
+
+       BOOST_CHECK_NO_THROW(butler.rethrow());
+}
+
+
+BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
+{
+       boost::filesystem::path paths[2] = {
+               "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
+               "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
+       };
+
+       dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
+
+       fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
+       fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
+
+       fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
+       fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
+
+       subs_file[0].close();
+       subs_file[1].close();
+
+       auto subs1 = content_factory(paths[0]).front();
+       auto subs2 = content_factory(paths[1]).front();
+       auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
+       film->set_sequence(false);
+       subs1->set_position(film, DCPTime());
+       subs2->set_position(film, DCPTime());
+
+       auto player = std::make_shared<Player>(film, Image::Alignment::COMPACT);
+       dcp::Time last;
+       player->Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
+               for (auto sub: text.string) {
+                       BOOST_CHECK(sub.in() >= last);
+                       last = sub.in();
+               }
+       });
+       while (!player->pass()) {}
+}
+