9a6541f58aa59e0e00c53da98d204bd52a367ccc
[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/film.h"
27 #include "lib/job.h"
28 #include "lib/video_content.h"
29 #include "lib/writer.h"
30 #include "test.h"
31 #include <dcp/openjpeg_image.h>
32 #include <dcp/j2k_transcode.h>
33 #include <boost/test/unit_test.hpp>
34 #include <memory>
35
36
37 using std::make_shared;
38 using std::shared_ptr;
39
40
41 BOOST_AUTO_TEST_CASE (test_write_odd_amount_of_silence)
42 {
43         auto content = content_factory("test/data/flat_red.png").front();
44         auto film = new_test_film2 ("test_write_odd_amount_of_silence", {content});
45         content->video->set_length(24);
46         auto writer = make_shared<Writer>(film, shared_ptr<Job>());
47
48         auto audio = make_shared<AudioBuffers>(6, 48000);
49         audio->make_silent ();
50         writer->write (audio, dcpomatic::DCPTime(1));
51 }
52
53
54 BOOST_AUTO_TEST_CASE (interrupt_writer)
55 {
56         Cleanup cl;
57
58         auto film = new_test_film2 ("test_interrupt_writer", {}, &cl);
59
60         auto content = content_factory("test/data/check_image0.png").front();
61         film->examine_and_add_content (content);
62         BOOST_REQUIRE (!wait_for_jobs());
63
64         /* Add some dummy content to the film so that it has a reel of the right length */
65         auto constexpr frames = 24 * 60;
66         content->video->set_length (frames);
67
68         /* Make a random J2K image */
69         auto size = dcp::Size(1998, 1080);
70         auto image = make_shared<dcp::OpenJPEGImage>(size);
71         for (int i = 0; i < 3; ++i) {
72                 for (int j = 0; j < (size.width * size.height); ++j) {
73                         image->data(i)[j] = rand();
74                 }
75         }
76
77         /* Write some data */
78         auto video = dcp::compress_j2k(image, 100000000, 24, false, false);
79         auto video_ptr = make_shared<dcp::ArrayData>(video.data(), video.size());
80         auto audio = make_shared<AudioBuffers>(6, 48000 / 24);
81
82         auto writer = make_shared<Writer>(film, shared_ptr<Job>());
83         writer->start ();
84
85         for (int i = 0; i < frames; ++i) {
86                 writer->write (video_ptr, i, Eyes::BOTH);
87                 writer->write (audio, dcpomatic::DCPTime::from_frames(i, 24));
88         }
89
90         /* Start digest calculations then abort them; there should be no crash or error */
91         boost::thread thread([film, writer]() {
92                 writer->finish(film->dir(film->dcp_name()));
93         });
94
95         dcpomatic_sleep_seconds (1);
96
97         thread.interrupt ();
98
99         dcpomatic_sleep_seconds (1);
100         cl.run ();
101 }
102