Allow repeat-frame to work with 3D.
[dcpomatic.git] / test / ffmpeg_audio_only_test.cc
1 /*
2     Copyright (C) 2016 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 /** @file  test/ffmpeg_audio_only_test.cc
22  *  @brief Test FFmpeg content with audio but no video.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/player.h"
30 #include "lib/job_manager.h"
31 #include "lib/audio_buffers.h"
32 #include "test.h"
33 #include <sndfile.h>
34 #include <boost/test/unit_test.hpp>
35
36 using boost::shared_ptr;
37
38 static SNDFILE* ref = 0;
39 static int ref_buffer_size = 0;
40 static float* ref_buffer = 0;
41
42 static void
43 audio (boost::shared_ptr<AudioBuffers> audio, int channels)
44 {
45         /* Check that we have a big enough buffer */
46         BOOST_CHECK (audio->frames() * audio->channels() < ref_buffer_size);
47
48         int const N = sf_readf_float (ref, ref_buffer, audio->frames());
49         for (int i = 0; i < N; ++i) {
50                 switch (channels) {
51                 case 1:
52                         BOOST_CHECK_EQUAL (ref_buffer[i], audio->data(2)[i]);
53                         break;
54                 case 2:
55                         BOOST_CHECK_EQUAL (ref_buffer[i*2 + 0], audio->data(0)[i]);
56                         BOOST_CHECK_EQUAL (ref_buffer[i*2 + 1], audio->data(1)[i]);
57                         break;
58                 default:
59                         BOOST_REQUIRE (false);
60                 }
61         }
62 }
63
64 /** Test the FFmpeg code with audio-only content */
65 static void
66 test (boost::filesystem::path file)
67 {
68         shared_ptr<Film> film = new_test_film ("ffmpeg_audio_only_test");
69         film->set_name ("test_film");
70         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
71         shared_ptr<FFmpegContent> c (new FFmpegContent (film, file));
72         film->examine_and_add_content (c);
73         wait_for_jobs ();
74         film->write_metadata ();
75
76         /* See if can make a DCP without any errors */
77         film->make_dcp ();
78         wait_for_jobs ();
79         BOOST_CHECK (!JobManager::instance()->errors());
80
81         /* Compare the audio data we read with what libsndfile reads */
82
83         SF_INFO info;
84         info.format = 0;
85         ref = sf_open (file.string().c_str(), SFM_READ, &info);
86         /* We don't want to test anything that requires resampling */
87         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
88         ref_buffer_size = info.samplerate * info.channels;
89         ref_buffer = new float[ref_buffer_size];
90
91         shared_ptr<Player> player (new Player (film, film->playlist ()));
92
93         player->Audio.connect (bind (&audio, _1, info.channels));
94         while (!player->pass ()) {}
95
96         sf_close (ref);
97 }
98
99 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
100 {
101         /* S16 */
102         test ("test/data/staircase.wav");
103 }
104
105 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
106 {
107         /* S32 1 channel */
108         test ("test/data/sine_440.wav");
109 }