Supporters update.
[dcpomatic.git] / test / writer_test.cc
1 /*
2     Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
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"
27 #include "lib/film.h"
28 #include "lib/job.h"
29 #include "lib/video_content.h"
30 #include "lib/writer.h"
31 #include "test.h"
32 #include <dcp/openjpeg_image.h>
33 #include <dcp/j2k_transcode.h>
34 #include <boost/test/unit_test.hpp>
35 #include <memory>
36
37
38 using std::make_shared;
39 using std::shared_ptr;
40 using std::string;
41 using std::vector;
42
43
44 BOOST_AUTO_TEST_CASE (test_write_odd_amount_of_silence)
45 {
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>());
50
51         auto audio = make_shared<AudioBuffers>(6, 48000);
52         audio->make_silent ();
53         writer->write (audio, dcpomatic::DCPTime(1));
54 }
55
56
57 BOOST_AUTO_TEST_CASE (interrupt_writer)
58 {
59         Cleanup cl;
60
61         auto film = new_test_film2 ("test_interrupt_writer", {}, &cl);
62
63         auto content = content_factory("test/data/check_image0.png")[0];
64         film->examine_and_add_content (content);
65         BOOST_REQUIRE (!wait_for_jobs());
66
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);
70
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;
77                 }
78         }
79
80         /* Write some data */
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);
84
85         auto writer = make_shared<Writer>(film, shared_ptr<Job>());
86         writer->start ();
87
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));
91         }
92
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()));
96         });
97
98         dcpomatic_sleep_seconds (1);
99
100         thread.interrupt ();
101         thread.join ();
102
103         dcpomatic_sleep_seconds (1);
104         cl.run ();
105 }
106
107
108 BOOST_AUTO_TEST_CASE(writer_progress_test)
109 {
110         class TestJob : public Job
111         {
112         public:
113                 explicit TestJob(shared_ptr<const Film> film)
114                         : Job(film)
115                 {}
116
117                 ~TestJob()
118                 {
119                         stop_thread();
120                 }
121
122                 std::string name() const override {
123                         return "test";
124                 }
125                 std::string json_name() const override {
126                         return "test";
127                 }
128                 void run() override {};
129         };
130
131         auto picture1 = content_factory("test/data/flat_red.png")[0];
132         auto picture2 = content_factory("test/data/flat_red.png")[0];
133
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));
139
140         auto job = std::make_shared<TestJob>(film);
141         job->set_rate_limit_progress(false);
142
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();
150         });
151
152         DCPEncoder encoder(film, job);
153         encoder.go();
154 }
155