2 Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/audio_buffers.h"
23 #include "lib/content.h"
24 #include "lib/content_factory.h"
25 #include "lib/cross.h"
26 #include "lib/dcp_encoder.h"
29 #include "lib/video_content.h"
30 #include "lib/writer.h"
32 #include <dcp/openjpeg_image.h>
33 #include <dcp/j2k_transcode.h>
34 #include <boost/test/unit_test.hpp>
38 using std::make_shared;
39 using std::shared_ptr;
44 BOOST_AUTO_TEST_CASE (test_write_odd_amount_of_silence)
46 auto content = content_factory("test/data/flat_red.png");
47 auto film = new_test_film2 ("test_write_odd_amount_of_silence", content);
48 content[0]->video->set_length(24);
49 auto writer = make_shared<Writer>(film, shared_ptr<Job>());
51 auto audio = make_shared<AudioBuffers>(6, 48000);
52 audio->make_silent ();
53 writer->write (audio, dcpomatic::DCPTime(1));
57 BOOST_AUTO_TEST_CASE (interrupt_writer)
61 auto film = new_test_film2 ("test_interrupt_writer", {}, &cl);
63 auto content = content_factory("test/data/check_image0.png")[0];
64 film->examine_and_add_content (content);
65 BOOST_REQUIRE (!wait_for_jobs());
67 /* Add some dummy content to the film so that it has a reel of the right length */
68 auto constexpr frames = 24 * 60;
69 content->video->set_length (frames);
71 /* Make a random J2K image */
72 auto size = dcp::Size(1998, 1080);
73 auto image = make_shared<dcp::OpenJPEGImage>(size);
74 for (int i = 0; i < 3; ++i) {
75 for (int j = 0; j < (size.width * size.height); ++j) {
76 image->data(i)[j] = rand() % 4095;
81 auto video = dcp::compress_j2k(image, 100000000, 24, false, false);
82 auto video_ptr = make_shared<dcp::ArrayData>(video.data(), video.size());
83 auto audio = make_shared<AudioBuffers>(6, 48000 / 24);
85 auto writer = make_shared<Writer>(film, shared_ptr<Job>());
88 for (int i = 0; i < frames; ++i) {
89 writer->write (video_ptr, i, Eyes::BOTH);
90 writer->write (audio, dcpomatic::DCPTime::from_frames(i, 24));
93 /* Start digest calculations then abort them; there should be no crash or error */
94 boost::thread thread([film, writer]() {
95 writer->finish(film->dir(film->dcp_name()));
98 dcpomatic_sleep_seconds (1);
103 dcpomatic_sleep_seconds (1);
108 BOOST_AUTO_TEST_CASE(writer_progress_test)
110 class TestJob : public Job
113 explicit TestJob(shared_ptr<const Film> film)
122 std::string name() const override {
125 std::string json_name() const override {
128 void run() override {};
131 auto picture1 = content_factory("test/data/flat_red.png")[0];
132 auto picture2 = content_factory("test/data/flat_red.png")[0];
134 auto film = new_test_film2("writer_progress_test", { picture1, picture2 });
135 film->set_reel_type(ReelType::BY_VIDEO_CONTENT);
136 picture1->video->set_length(240);
137 picture2->video->set_length(240);
138 picture2->set_position(film, dcpomatic::DCPTime::from_seconds(10));
140 auto job = std::make_shared<TestJob>(film);
141 job->set_rate_limit_progress(false);
143 float last_progress = 0;
144 string last_sub_name;
145 boost::signals2::scoped_connection connection = job->Progress.connect([job, &last_progress, &last_sub_name]() {
146 auto const progress = job->progress().get_value_or(0);
147 BOOST_REQUIRE(job->sub_name() != last_sub_name || progress >= last_progress);
148 last_progress = progress;
149 last_sub_name = job->sub_name();
152 DCPEncoder encoder(film, job);