Fix typo if -> of (thanks to Uwe Dittes)
[dcpomatic.git] / test / reel_writer_test.cc
1 /*
2     Copyright (C) 2019-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 /** @file  test/reel_writer_test.cc
23  *  @brief Test ReelWriter class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/audio_content.h"
29 #include "lib/content.h"
30 #include "lib/content_factory.h"
31 #include "lib/cross.h"
32 #include "lib/film.h"
33 #include "lib/reel_writer.h"
34 #include "lib/video_content.h"
35 #include "test.h"
36 #include <dcp/dcp.h>
37 #include <dcp/cpl.h>
38 #include <dcp/reel.h>
39 #include <dcp/reel_picture_asset.h>
40 #include <dcp/reel_sound_asset.h>
41 #include <boost/test/unit_test.hpp>
42
43
44 using std::shared_ptr;
45 using std::string;
46 using boost::optional;
47
48
49 static bool equal (dcp::FrameInfo a, ReelWriter const & writer, shared_ptr<InfoFileHandle> file, Frame frame, Eyes eyes)
50 {
51         auto b = writer.read_frame_info(file, frame, eyes);
52         return a.offset == b.offset && a.size == b.size && a.hash == b.hash;
53 }
54
55
56 BOOST_AUTO_TEST_CASE (write_frame_info_test)
57 {
58         auto film = new_test_film2 ("write_frame_info_test");
59         dcpomatic::DCPTimePeriod const period (dcpomatic::DCPTime(0), dcpomatic::DCPTime(96000));
60         ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1, false);
61
62         /* Write the first one */
63
64         dcp::FrameInfo info1(0, 123, "12345678901234567890123456789012");
65         writer.write_frame_info (0, Eyes::LEFT, info1);
66         {
67                 auto file = film->info_file_handle(period, true);
68                 BOOST_CHECK (equal(info1, writer, file, 0, Eyes::LEFT));
69         }
70
71         /* Write some more */
72
73         dcp::FrameInfo info2(596, 14921, "123acb789f1234ae782012n456339522");
74         writer.write_frame_info (5, Eyes::RIGHT, info2);
75
76         {
77                 auto file = film->info_file_handle(period, true);
78                 BOOST_CHECK (equal(info1, writer, file, 0, Eyes::LEFT));
79                 BOOST_CHECK (equal(info2, writer, file, 5, Eyes::RIGHT));
80         }
81
82         dcp::FrameInfo info3(12494, 99157123, "xxxxyyyyabc12356ffsfdsf456339522");
83         writer.write_frame_info (10, Eyes::LEFT, info3);
84
85         {
86                 auto file = film->info_file_handle(period, true);
87                 BOOST_CHECK (equal(info1, writer, file, 0, Eyes::LEFT));
88                 BOOST_CHECK (equal(info2, writer, file, 5, Eyes::RIGHT));
89                 BOOST_CHECK (equal(info3, writer, file, 10, Eyes::LEFT));
90         }
91
92         /* Overwrite one */
93
94         dcp::FrameInfo info4(55512494, 123599157123, "ABCDEFGyabc12356ffsfdsf4563395ZZ");
95         writer.write_frame_info (5, Eyes::RIGHT, info4);
96
97         {
98                 auto file = film->info_file_handle(period, true);
99                 BOOST_CHECK (equal(info1, writer, file, 0, Eyes::LEFT));
100                 BOOST_CHECK (equal(info4, writer, file, 5, Eyes::RIGHT));
101                 BOOST_CHECK (equal(info3, writer, file, 10, Eyes::LEFT));
102         }
103 }
104
105
106 /** Check that the reel writer correctly re-uses a video asset changed if we remake
107  *  a DCP with no video changes.
108  */
109 BOOST_AUTO_TEST_CASE (reel_reuse_video_test)
110 {
111         /* Make a DCP */
112         auto video = content_factory("test/data/flat_red.png").front();
113         auto audio = content_factory("test/data/white.wav").front();
114         auto film = new_test_film2 ("reel_reuse_video_test", { video, audio });
115         make_and_verify_dcp (film);
116
117         /* Find main picture and sound asset IDs */
118         dcp::DCP dcp1 (film->dir(film->dcp_name()));
119         dcp1.read ();
120         BOOST_REQUIRE_EQUAL (dcp1.cpls().size(), 1U);
121         BOOST_REQUIRE_EQUAL (dcp1.cpls()[0]->reels().size(), 1U);
122         BOOST_REQUIRE (dcp1.cpls()[0]->reels()[0]->main_picture());
123         BOOST_REQUIRE (dcp1.cpls()[0]->reels()[0]->main_sound());
124         auto const picture_id = dcp1.cpls()[0]->reels()[0]->main_picture()->asset()->id();
125         auto const sound_id = dcp1.cpls()[0]->reels()[0]->main_sound()->asset()->id();
126
127         /* Change the audio and re-make */
128         audio->audio->set_gain (-3);
129         make_and_verify_dcp (film);
130
131         /* Video ID should be the same, sound different */
132         dcp::DCP dcp2 (film->dir(film->dcp_name()));
133         dcp2.read ();
134         BOOST_REQUIRE_EQUAL (dcp2.cpls().size(), 1U);
135         BOOST_REQUIRE_EQUAL (dcp2.cpls()[0]->reels().size(), 1U);
136         BOOST_REQUIRE (dcp2.cpls()[0]->reels()[0]->main_picture());
137         BOOST_REQUIRE (dcp2.cpls()[0]->reels()[0]->main_sound());
138         BOOST_CHECK_EQUAL (picture_id, dcp2.cpls()[0]->reels()[0]->main_picture()->asset()->id());
139         BOOST_CHECK (sound_id != dcp2.cpls()[0]->reels().front()->main_sound()->asset()->id());
140
141         /* Crop video and re-make */
142         video->video->set_left_crop (5);
143         make_and_verify_dcp (film);
144
145         /* Video and sound IDs should be different */
146         dcp::DCP dcp3 (film->dir(film->dcp_name()));
147         dcp3.read ();
148         BOOST_REQUIRE_EQUAL (dcp3.cpls().size(), 1U);
149         BOOST_REQUIRE_EQUAL (dcp3.cpls()[0]->reels().size(), 1U);
150         BOOST_REQUIRE (dcp3.cpls()[0]->reels()[0]->main_picture());
151         BOOST_REQUIRE (dcp3.cpls()[0]->reels()[0]->main_sound());
152         BOOST_CHECK (picture_id != dcp3.cpls()[0]->reels()[0]->main_picture()->asset()->id());
153         BOOST_CHECK (sound_id != dcp3.cpls()[0]->reels().front()->main_sound()->asset()->id());
154 }