Merge master.
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "dcp.h"
21 #include "metadata.h"
22 #include "cpl.h"
23 #include "mono_picture_mxf.h"
24 #include "picture_mxf_writer.h"
25 #include "sound_mxf_writer.h"
26 #include "sound_mxf.h"
27 #include "subtitle_content.h"
28 #include "reel.h"
29 #include "test.h"
30 #include "file.h"
31 #include "reel_mono_picture_asset.h"
32 #include "reel_sound_asset.h"
33 #include "KM_util.h"
34 #include <sndfile.h>
35 #include <boost/test/unit_test.hpp>
36
37 using boost::shared_ptr;
38
39 /* Test creation of a DCP from very simple inputs */
40 BOOST_AUTO_TEST_CASE (dcp_test)
41 {
42         Kumu::libdcp_test = true;
43
44         /* Some known metadata */
45         dcp::XMLMetadata xml_meta;
46         xml_meta.issuer = "OpenDCP 0.0.25";
47         xml_meta.creator = "OpenDCP 0.0.25";
48         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
49         dcp::MXFMetadata mxf_meta;
50         mxf_meta.company_name = "OpenDCP";
51         mxf_meta.product_name = "OpenDCP";
52         mxf_meta.product_version = "0.0.25";
53
54         /* We're making build/test/foo */
55         boost::filesystem::remove_all ("build/test/DCP/foo");
56         boost::filesystem::create_directories ("build/test/DCP/foo");
57         dcp::DCP d ("build/test/DCP/foo");
58         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
59         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
60         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
61
62         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
63         mp->set_metadata (mxf_meta);
64         shared_ptr<dcp::PictureMXFWriter> picture_writer = mp->start_write ("build/test/DCP/foo/video.mxf", dcp::SMPTE, false);
65         dcp::File j2c ("test/data/32x32_red_square.j2c");
66         for (int i = 0; i < 24; ++i) {
67                 picture_writer->write (j2c.data (), j2c.size ());
68         }
69         picture_writer->finalize ();
70
71         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
72         ms->set_metadata (mxf_meta);
73         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/foo/audio.mxf", dcp::SMPTE);
74
75         SF_INFO info;
76         info.format = 0;
77         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
78         BOOST_CHECK (sndfile);
79         float buffer[4096*6];
80         float* channels[1];
81         channels[0] = buffer;
82         while (1) {
83                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
84                 sound_writer->write (channels, N);
85                 if (N < 4096) {
86                         break;
87                 }
88         }
89         
90         sound_writer->finalize ();
91         
92         cpl->add (shared_ptr<dcp::Reel> (
93                           new dcp::Reel (
94                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
95                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
96                                   shared_ptr<dcp::ReelSubtitleAsset> ()
97                                   )
98                           ));
99                   
100         d.add (cpl);
101         d.add (mp);
102         d.add (ms);
103
104         d.write_xml (dcp::SMPTE, xml_meta);
105
106         /* build/test/DCP/foo is checked against test/ref/DCP/foo by run-tests.sh */
107 }