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