af4f3f0385100dfeda813084ea49734c2f0a75b8
[dcpomatic.git] / test / shuffler_test.cc
1 /*
2     Copyright (C) 2013-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/decoder_factory.h"
23 #include "lib/image_content.h"
24 #include "lib/piece.h"
25 #include "lib/piece_video.h"
26 #include "lib/shuffler.h"
27 #include "test.h"
28 #include <boost/test/unit_test.hpp>
29
30
31 using std::list;
32 using std::make_shared;
33 using std::shared_ptr;
34 using std::weak_ptr;
35 using boost::optional;
36 #if BOOST_VERSION >= 106100
37 using namespace boost::placeholders;
38 #endif
39
40
41 static void
42 push (Shuffler& s, shared_ptr<Film> film, shared_ptr<Content> content, shared_ptr<Decoder> decoder, int frame, Eyes eyes)
43 {
44         auto piece = make_shared<Piece>(film, content, decoder, FrameRateChange(24, 24), false);
45         PieceVideo cv;
46         cv.frame = frame;
47         cv.eyes = eyes;
48         s.video (piece, cv);
49 }
50
51 list<PieceVideo> pending_cv;
52
53 static void
54 receive (weak_ptr<Piece>, PieceVideo cv)
55 {
56         pending_cv.push_back (cv);
57 }
58
59 static void
60 check (int frame, Eyes eyes, int line)
61 {
62         BOOST_REQUIRE_MESSAGE (!pending_cv.empty(), "Check at " << line << " failed.");
63         BOOST_CHECK_MESSAGE (pending_cv.front().frame == frame, "Check at " << line << " failed.");
64         BOOST_CHECK_MESSAGE (pending_cv.front().eyes == eyes, "Check at " << line << " failed.");
65         pending_cv.pop_front();
66 }
67
68 /** A perfect sequence */
69 BOOST_AUTO_TEST_CASE (shuffler_test1)
70 {
71         Shuffler s;
72         s.Video.connect (boost::bind (&receive, _1, _2));
73
74         auto film = new_test_film2 ("shuffler_test1");
75         auto content = make_shared<ImageContent>("test/data/flat_red.png");
76         auto decoder = decoder_factory (film, content, true, {});
77         BOOST_REQUIRE (decoder);
78
79         for (int i = 0; i < 10; ++i) {
80                 push (s, film, content, decoder, i, Eyes::LEFT);
81                 push (s, film, content, decoder, i, Eyes::RIGHT);
82                 check (i, Eyes::LEFT, __LINE__);
83                 check (i, Eyes::RIGHT, __LINE__);
84         }
85 }
86
87 /** Everything present but some simple shuffling needed */
88 BOOST_AUTO_TEST_CASE (shuffler_test2)
89 {
90         Shuffler s;
91         s.Video.connect (boost::bind (&receive, _1, _2));
92
93         auto film = new_test_film2 ("shuffler_test2");
94         auto content = make_shared<ImageContent>("test/data/flat_red.png");
95         auto decoder = decoder_factory (film, content, true, {});
96
97         for (int i = 0; i < 10; i += 2) {
98                 push (s, film, content, decoder, i, Eyes::LEFT);
99                 push (s, film, content, decoder, i + 1, Eyes::LEFT);
100                 push (s, film, content, decoder, i, Eyes::RIGHT);
101                 push (s, film, content, decoder, i + 1, Eyes::RIGHT);
102                 check (i, Eyes::LEFT, __LINE__);
103                 check (i, Eyes::RIGHT, __LINE__);
104                 check (i + 1, Eyes::LEFT, __LINE__);
105                 check (i + 1, Eyes::RIGHT, __LINE__);
106         }
107 }
108
109 /** One missing left eye image */
110 BOOST_AUTO_TEST_CASE (shuffler_test3)
111 {
112         Shuffler s;
113         s.Video.connect (boost::bind (&receive, _1, _2));
114
115         auto film = new_test_film2 ("shuffler_test3");
116         auto content = make_shared<ImageContent>("test/data/flat_red.png");
117         auto decoder = decoder_factory (film, content, true, {});
118
119         push (s, film, content, decoder, 0, Eyes::LEFT);
120         check (0, Eyes::LEFT, __LINE__);
121         push (s, film, content, decoder, 0, Eyes::RIGHT);
122         check (0, Eyes::RIGHT, __LINE__);
123         push (s, film, content, decoder, 1, Eyes::LEFT);
124         check (1, Eyes::LEFT, __LINE__);
125         push (s, film, content, decoder, 1, Eyes::RIGHT);
126         check (1, Eyes::RIGHT, __LINE__);
127         push (s, film, content, decoder, 2, Eyes::RIGHT);
128         push (s, film, content, decoder, 3, Eyes::LEFT);
129         push (s, film, content, decoder, 3, Eyes::RIGHT);
130         push (s, film, content, decoder, 4, Eyes::LEFT);
131         push (s, film, content, decoder, 4, Eyes::RIGHT);
132         s.flush ();
133         check (2, Eyes::RIGHT, __LINE__);
134         check (3, Eyes::LEFT, __LINE__);
135         check (3, Eyes::RIGHT, __LINE__);
136         check (4, Eyes::LEFT, __LINE__);
137         check (4, Eyes::RIGHT, __LINE__);
138 }
139
140 /** One missing right eye image */
141 BOOST_AUTO_TEST_CASE (shuffler_test4)
142 {
143         Shuffler s;
144         s.Video.connect (boost::bind (&receive, _1, _2));
145
146         auto film = new_test_film2 ("shuffler_test3");
147         auto content = make_shared<ImageContent>("test/data/flat_red.png");
148         auto decoder = decoder_factory (film, content, true, {});
149
150         push (s, film, content, decoder, 0, Eyes::LEFT);
151         check (0, Eyes::LEFT, __LINE__);
152         push (s, film, content, decoder, 0, Eyes::RIGHT);
153         check (0, Eyes::RIGHT, __LINE__);
154         push (s, film, content, decoder, 1, Eyes::LEFT);
155         check (1, Eyes::LEFT, __LINE__);
156         push (s, film, content, decoder, 1, Eyes::RIGHT);
157         check (1, Eyes::RIGHT, __LINE__);
158         push (s, film, content, decoder, 2, Eyes::LEFT);
159         push (s, film, content, decoder, 3, Eyes::LEFT);
160         push (s, film, content, decoder, 3, Eyes::RIGHT);
161         push (s, film, content, decoder, 4, Eyes::LEFT);
162         push (s, film, content, decoder, 4, Eyes::RIGHT);
163         s.flush ();
164         check (2, Eyes::LEFT, __LINE__);
165         check (3, Eyes::LEFT, __LINE__);
166         check (3, Eyes::RIGHT, __LINE__);
167         check (4, Eyes::LEFT, __LINE__);
168         check (4, Eyes::RIGHT, __LINE__);
169 }
170
171 /** Only one eye */
172 BOOST_AUTO_TEST_CASE (shuffler_test5)
173 {
174         Shuffler s;
175         s.Video.connect (boost::bind (&receive, _1, _2));
176
177         auto film = new_test_film2 ("shuffler_test3");
178         auto content = make_shared<ImageContent>("test/data/flat_red.png");
179         auto decoder = decoder_factory (film, content, true, {});
180
181         /* One left should come out straight away */
182         push (s, film, content, decoder, 0, Eyes::LEFT);
183         check (0, Eyes::LEFT, __LINE__);
184
185         /* More lefts should be kept in the shuffler in the hope that some rights arrive */
186         for (int i = 0; i < s._max_size; ++i) {
187                 push (s, film, content, decoder, i + 1, Eyes::LEFT);
188         }
189         BOOST_CHECK (pending_cv.empty ());
190
191         /* If enough lefts come the shuffler should conclude that there's no rights and start
192            giving out the lefts.
193         */
194         push (s, film, content, decoder, s._max_size + 1, Eyes::LEFT);
195         check (1, Eyes::LEFT, __LINE__);
196 }
197
198 /** One complete frame (L+R) missing.
199     Shuffler should carry on, skipping this frame, as the player will cope with it.
200 */
201 BOOST_AUTO_TEST_CASE (shuffler_test6)
202 {
203         Shuffler s;
204         s.Video.connect (boost::bind (&receive, _1, _2));
205
206         auto film = new_test_film2 ("shuffler_test3");
207         auto content = make_shared<ImageContent>("test/data/flat_red.png");
208         auto decoder = decoder_factory (film, content, true, {});
209
210         push (s, film, content, decoder, 0, Eyes::LEFT);
211         check (0, Eyes::LEFT, __LINE__);
212         push (s, film, content, decoder, 0, Eyes::RIGHT);
213         check (0, Eyes::RIGHT, __LINE__);
214
215         push (s, film, content, decoder, 2, Eyes::LEFT);
216         push (s, film, content, decoder, 2, Eyes::RIGHT);
217         check (2, Eyes::LEFT, __LINE__);
218         check (2, Eyes::RIGHT, __LINE__);
219
220         push (s, film, content, decoder, 3, Eyes::LEFT);
221         check (3, Eyes::LEFT, __LINE__);
222         push (s, film, content, decoder, 3, Eyes::RIGHT);
223         check (3, Eyes::RIGHT, __LINE__);
224 }