Tidy a little and add some coding style.
[libdcp.git] / test / mca_test.cc
1 /*
2     Copyright (C) 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
35 #include "compose.hpp"
36 #include "cpl.h"
37 #include "reel.h"
38 #include "reel_sound_asset.h"
39 #include "sound_asset.h"
40 #include "sound_asset_writer.h"
41 #include "test.h"
42 #include <libcxml/cxml.h>
43 #include <libxml++/libxml++.h>
44 #include <boost/test/unit_test.hpp>
45
46
47 using std::list;
48 using std::string;
49 using std::vector;
50 using std::shared_ptr;
51
52
53 /** Check that when we read a MXF and write its MCA metadata to a CPL we get the same answer
54  *  as the original MXF for that CPL (for a couple of different MXFs).
55  */
56 BOOST_AUTO_TEST_CASE (parse_mca_descriptors_from_mxf_test)
57 {
58         for (int i = 1; i < 3; ++i) {
59                 shared_ptr<dcp::SoundAsset> sound_asset(new dcp::SoundAsset(private_test / "data" / dcp::String::compose("51_sound_with_mca_%1.mxf", i)));
60                 shared_ptr<dcp::ReelSoundAsset> reel_sound_asset(new dcp::ReelSoundAsset(sound_asset, 0));
61                 shared_ptr<dcp::Reel> reel(new dcp::Reel());
62                 reel->add (black_picture_asset(dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1", i), 24));
63                 reel->add (reel_sound_asset);
64
65                 dcp::CPL cpl("", dcp::ContentKind::FEATURE);
66                 cpl.add (reel);
67                 cpl.set_main_sound_configuration("51/L,R,C,LFE,Ls,Rs");
68                 cpl.set_main_sound_sample_rate(48000);
69                 cpl.set_main_picture_stored_area(dcp::Size(1998, 1080));
70                 cpl.set_main_picture_active_area(dcp::Size(1998, 1080));
71                 cpl.write_xml (dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1/cpl.xml", i), dcp::Standard::SMPTE, shared_ptr<dcp::CertificateChain>());
72
73                 cxml::Document ref("CompositionPlaylist", private_test / dcp::String::compose("51_sound_with_mca_%1.cpl", i));
74                 cxml::Document check("CompositionPlaylist", dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1/cpl.xml", i));
75
76                 vector<string> ignore;
77                 check_xml (
78                         dynamic_cast<xmlpp::Element*>(
79                                 ref.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
80                                 ),
81                         dynamic_cast<xmlpp::Element*>(
82                                 check.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
83                                 ),
84                         ignore,
85                         true
86                         );
87         }
88 }
89
90
91 /** Reproduce the MCA tags from one of the example files using libdcp */
92 BOOST_AUTO_TEST_CASE (write_mca_descriptors_to_mxf_test)
93 {
94         shared_ptr<dcp::SoundAsset> sound_asset(new dcp::SoundAsset(dcp::Fraction(24, 1), 48000, 6, dcp::LanguageTag("en-US"), dcp::Standard::SMPTE));
95         vector<dcp::Channel> channels;
96         channels.push_back (dcp::Channel::LEFT);
97         channels.push_back (dcp::Channel::RIGHT);
98         channels.push_back (dcp::Channel::CENTRE);
99         channels.push_back (dcp::Channel::LFE);
100         channels.push_back (dcp::Channel::LS);
101         channels.push_back (dcp::Channel::RS);
102         shared_ptr<dcp::SoundAssetWriter> writer = sound_asset->start_write("build/test/write_mca_descriptors_to_mxf_test.mxf", channels);
103
104         float* samples[6];
105         for (int i = 0; i < 6; ++i) {
106                 samples[i] = new float[2000];
107                 memset (samples[i], 0, 2000 * sizeof(float));
108         }
109         for (int i = 0; i < 24; ++i) {
110                 writer->write(samples, 2000);
111         }
112         for (int i = 0; i < 6; ++i) {
113                 delete[] samples[i];
114         }
115
116         writer->finalize();
117
118         /* Make a CPL as a roundabout way to read the metadata we just wrote to the MXF */
119
120         shared_ptr<dcp::ReelSoundAsset> reel_sound_asset(new dcp::ReelSoundAsset(sound_asset, 0));
121         shared_ptr<dcp::Reel> reel(new dcp::Reel());
122         reel->add (black_picture_asset("build/test/write_mca_descriptors_to_mxf_test", 24));
123         reel->add (reel_sound_asset);
124
125         dcp::CPL cpl("", dcp::ContentKind::FEATURE);
126         cpl.add (reel);
127         cpl.set_main_sound_configuration("51/L,R,C,LFE,Ls,Rs");
128         cpl.set_main_sound_sample_rate(48000);
129         cpl.set_main_picture_stored_area(dcp::Size(1998, 1080));
130         cpl.set_main_picture_active_area(dcp::Size(1998, 1080));
131         cpl.write_xml ("build/test/write_mca_descriptors_to_mxf_test/cpl.xml", dcp::Standard::SMPTE, shared_ptr<dcp::CertificateChain>());
132
133         cxml::Document ref("CompositionPlaylist", private_test / "51_sound_with_mca_1.cpl");
134         cxml::Document check("CompositionPlaylist", "build/test/write_mca_descriptors_to_mxf_test/cpl.xml");
135
136         vector<string> ignore;
137         ignore.push_back ("InstanceID");
138         ignore.push_back ("MCALinkID");
139         ignore.push_back ("SoundfieldGroupLinkID");
140
141         check_xml (
142                 dynamic_cast<xmlpp::Element*>(
143                         ref.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
144                         ),
145                 dynamic_cast<xmlpp::Element*>(
146                         check.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
147                         ),
148                 ignore,
149                 true
150                 );
151 }
152