Remove XMLMetadata use from tests.
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "dcp.h"
35 #include "metadata.h"
36 #include "cpl.h"
37 #include "mono_picture_asset.h"
38 #include "stereo_picture_asset.h"
39 #include "picture_asset_writer.h"
40 #include "sound_asset_writer.h"
41 #include "sound_asset.h"
42 #include "atmos_asset.h"
43 #include "reel.h"
44 #include "test.h"
45 #include "file.h"
46 #include "reel_mono_picture_asset.h"
47 #include "reel_stereo_picture_asset.h"
48 #include "reel_sound_asset.h"
49 #include "reel_atmos_asset.h"
50 #include <asdcp/KM_util.h>
51 #include <sndfile.h>
52 #include <boost/test/unit_test.hpp>
53
54 using std::string;
55 using boost::shared_ptr;
56
57
58 /** Test creation of a 2D SMPTE DCP from very simple inputs */
59 BOOST_AUTO_TEST_CASE (dcp_test1)
60 {
61         RNGFixer fixer;
62
63         make_simple("build/test/DCP/dcp_test1")->write_xml(
64                 dcp::SMPTE, "OpenDCP 0.0.25", "OpenDCP 0.0.25", "2012-07-17T04:45:18+00:00", "Created by libdcp"
65                 );
66
67         /* build/test/DCP/dcp_test1 is checked against test/ref/DCP/dcp_test1 by run/tests */
68 }
69
70 /** Test creation of a 3D DCP from very simple inputs */
71 BOOST_AUTO_TEST_CASE (dcp_test2)
72 {
73         RNGFixer fix;
74
75         /* Some known metadata */
76         dcp::MXFMetadata mxf_meta;
77         mxf_meta.company_name = "OpenDCP";
78         mxf_meta.product_name = "OpenDCP";
79         mxf_meta.product_version = "0.0.25";
80
81         /* We're making build/test/DCP/dcp_test2 */
82         boost::filesystem::remove_all ("build/test/DCP/dcp_test2");
83         boost::filesystem::create_directories ("build/test/DCP/dcp_test2");
84         dcp::DCP d ("build/test/DCP/dcp_test2");
85         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
86         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
87         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
88         cpl->set_issuer ("OpenDCP 0.0.25");
89         cpl->set_creator ("OpenDCP 0.0.25");
90         cpl->set_issue_date ("2012-07-17T04:45:18+00:00");
91         cpl->set_annotation_text ("A Test DCP");
92
93         shared_ptr<dcp::StereoPictureAsset> mp (new dcp::StereoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
94         mp->set_metadata (mxf_meta);
95         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test2/video.mxf", false);
96         dcp::File j2c ("test/data/32x32_red_square.j2c");
97         for (int i = 0; i < 24; ++i) {
98                 /* Left */
99                 picture_writer->write (j2c.data (), j2c.size ());
100                 /* Right */
101                 picture_writer->write (j2c.data (), j2c.size ());
102         }
103         picture_writer->finalize ();
104
105         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
106         ms->set_metadata (mxf_meta);
107         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test2/audio.mxf");
108
109         SF_INFO info;
110         info.format = 0;
111         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
112         BOOST_CHECK (sndfile);
113         float buffer[4096*6];
114         float* channels[1];
115         channels[0] = buffer;
116         while (1) {
117                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
118                 sound_writer->write (channels, N);
119                 if (N < 4096) {
120                         break;
121                 }
122         }
123
124         sound_writer->finalize ();
125
126         cpl->add (shared_ptr<dcp::Reel> (
127                           new dcp::Reel (
128                                   shared_ptr<dcp::ReelStereoPictureAsset> (new dcp::ReelStereoPictureAsset (mp, 0)),
129                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
130                                   )
131                           ));
132
133         d.add (cpl);
134
135         d.write_xml (dcp::SMPTE, "OpenDCP 0.0.25", "OpenDCP 0.0.25", "2012-07-17T04:45:18+00:00", "Created by libdcp");
136
137         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run/tests */
138 }
139
140 static void
141 note (dcp::NoteType, string)
142 {
143
144 }
145
146 /** Test comparison of a DCP with itself */
147 BOOST_AUTO_TEST_CASE (dcp_test3)
148 {
149         dcp::DCP A ("test/ref/DCP/dcp_test1");
150         A.read ();
151         dcp::DCP B ("test/ref/DCP/dcp_test1");
152         B.read ();
153
154         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
155 }
156
157 /** Test comparison of a DCP with a different DCP */
158 BOOST_AUTO_TEST_CASE (dcp_test4)
159 {
160         dcp::DCP A ("test/ref/DCP/dcp_test1");
161         A.read ();
162         dcp::DCP B ("test/ref/DCP/dcp_test2");
163         B.read ();
164
165         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
166 }
167
168 /** Test creation of a 2D DCP with an Atmos track */
169 BOOST_AUTO_TEST_CASE (dcp_test5)
170 {
171         RNGFixer fix;
172
173         /* Some known metadata */
174         dcp::MXFMetadata mxf_meta;
175         mxf_meta.company_name = "OpenDCP";
176         mxf_meta.product_name = "OpenDCP";
177         mxf_meta.product_version = "0.0.25";
178
179         /* We're making build/test/DCP/dcp_test5 */
180         boost::filesystem::remove_all ("build/test/DCP/dcp_test5");
181         boost::filesystem::create_directories ("build/test/DCP/dcp_test5");
182         dcp::DCP d ("build/test/DCP/dcp_test5");
183         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
184         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
185         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
186         cpl->set_issuer ("OpenDCP 0.0.25");
187         cpl->set_creator ("OpenDCP 0.0.25");
188         cpl->set_issue_date ("2012-07-17T04:45:18+00:00");
189         cpl->set_annotation_text ("A Test DCP");
190
191         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
192         mp->set_metadata (mxf_meta);
193         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test5/video.mxf", false);
194         dcp::File j2c ("test/data/32x32_red_square.j2c");
195         for (int i = 0; i < 24; ++i) {
196                 picture_writer->write (j2c.data (), j2c.size ());
197         }
198         picture_writer->finalize ();
199
200         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
201         ms->set_metadata (mxf_meta);
202         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test5/audio.mxf");
203
204         SF_INFO info;
205         info.format = 0;
206         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
207         BOOST_CHECK (sndfile);
208         float buffer[4096*6];
209         float* channels[1];
210         channels[0] = buffer;
211         while (true) {
212                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
213                 sound_writer->write (channels, N);
214                 if (N < 4096) {
215                         break;
216                 }
217         }
218
219         sound_writer->finalize ();
220
221         shared_ptr<dcp::AtmosAsset> am (new dcp::AtmosAsset (private_test / "20160218_NameOfFilm_FTR_OV_EN_A_dcs_r01.mxf"));
222
223         cpl->add (shared_ptr<dcp::Reel> (
224                           new dcp::Reel (
225                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
226                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
227                                   shared_ptr<dcp::ReelSubtitleAsset> (),
228                                   shared_ptr<dcp::ReelMarkersAsset> (),
229                                   shared_ptr<dcp::ReelAtmosAsset> (new dcp::ReelAtmosAsset (am, 0))
230                                   )
231                           ));
232
233         d.add (cpl);
234
235         d.write_xml (dcp::SMPTE, "OpenDCP 0.0.25", "OpenDCP 0.0.25", "2012-07-17T04:45:18+00:00", "Created by libdcp");
236
237         /* build/test/DCP/dcp_test5 is checked against test/ref/DCP/dcp_test5 by run/tests */
238 }
239
240 /** Basic tests of reading a 2D DCP with an Atmos track */
241 BOOST_AUTO_TEST_CASE (dcp_test6)
242 {
243         dcp::DCP dcp ("test/ref/DCP/dcp_test5");
244         dcp.read ();
245
246         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1);
247         BOOST_REQUIRE_EQUAL (dcp.cpls().front()->reels().size(), 1);
248         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_picture());
249         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_sound());
250         BOOST_CHECK (!dcp.cpls().front()->reels().front()->main_subtitle());
251         BOOST_CHECK (dcp.cpls().front()->reels().front()->atmos());
252 }
253
254 /** Test creation of a 2D Interop DCP from very simple inputs */
255 BOOST_AUTO_TEST_CASE (dcp_test7)
256 {
257         RNGFixer fix;
258
259         make_simple("build/test/DCP/dcp_test7")->write_xml(
260                 dcp::INTEROP,  "OpenDCP 0.0.25", "OpenDCP 0.0.25", "2012-07-17T04:45:18+00:00", "Created by libdcp"
261                 );
262
263         /* build/test/DCP/dcp_test7 is checked against test/ref/DCP/dcp_test7 by run/tests */
264 }
265
266 /** Test reading of a DCP with multiple CPLs */
267 BOOST_AUTO_TEST_CASE (dcp_test8)
268 {
269         dcp::DCP dcp (private_test / "data/SMPTE_TST-B1PB2P_S_EN-EN-CCAP_5171-HI-VI_2K_ISDCF_20151123_DPPT_SMPTE_combo/");
270         dcp.read ();
271
272         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 2);
273 }