Work around deadlock when destroying J2KEncoder with a full writer queue (#2784).
[dcpomatic.git] / test / j2k_encoder_test.cc
1 /*
2     Copyright (C) 2024 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/config.h"
23 #include "lib/cross.h"
24 #include "lib/image.h"
25 #include "lib/j2k_encoder.h"
26 #include "lib/player_video.h"
27 #include "lib/raw_image_proxy.h"
28 #include "lib/writer.h"
29 #include "test.h"
30 extern "C" {
31 #include <libavutil/pixfmt.h>
32 }
33 #include <boost/test/unit_test.hpp>
34
35
36 using std::make_shared;
37 using std::weak_ptr;
38 using boost::optional;
39
40
41 BOOST_AUTO_TEST_CASE(j2k_encoder_deadlock_test)
42 {
43         ConfigRestorer cr;
44
45         auto film = new_test_film2("j2k_encoder_deadlock_test");
46
47         /* Don't call ::start() on this Writer, so it can never write anything */
48         Writer writer(film, {});
49         writer.set_encoder_threads(4);
50
51         /* We want to test the case where the writer queue fills, and this can't happen unless there
52          * are enough encoding threads (each of which will end up waiting for the writer to empty,
53          * which will never happen).
54          */
55         Config::instance()->set_master_encoding_threads(4);
56         J2KEncoder encoder(film, writer);
57         encoder.begin();
58
59         for (int i = 0; i < 26; ++i) {
60                 auto image = make_shared<Image>(AV_PIX_FMT_RGB24, dcp::Size(1998, 1080), Image::Alignment::PADDED);
61                 auto image_proxy = make_shared<RawImageProxy>(image);
62                 encoder.encode(
63                         std::make_shared<PlayerVideo>(
64                                 image_proxy,
65                                 Crop(),
66                                 optional<double>(),
67                                 dcp::Size(1998, 1080),
68                                 dcp::Size(1998, 1080),
69                                 Eyes::BOTH,
70                                 Part::WHOLE,
71                                 optional<ColourConversion>(),
72                                 VideoRange::VIDEO,
73                                 weak_ptr<Content>(),
74                                 optional<Frame>(),
75                                 false
76                                 ),
77                         {}
78                         );
79         }
80
81         dcpomatic_sleep_seconds(10);
82 }
83