6ca0073d81ed39c037d6404977e578a5593a1d55
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014 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/player_test.cc
22  *  @brief Various tests of Player.
23  */
24
25 #include "lib/film.h"
26 #include "lib/ffmpeg_content.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/audio_buffers.h"
30 #include "lib/player.h"
31 #include "test.h"
32 #include <boost/test/unit_test.hpp>
33 #include <boost/make_shared.hpp>
34 #include <iostream>
35
36 using std::cout;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::make_shared;
40
41 static bool
42 valid (Content const *)
43 {
44         return true;
45 }
46
47 /** Player::overlaps */
48 BOOST_AUTO_TEST_CASE (player_overlaps_test)
49 {
50         shared_ptr<Film> film = new_test_film ("player_overlaps_test");
51         film->set_container (Ratio::from_id ("185"));
52
53         /* This content is 3s long */
54         shared_ptr<FFmpegContent> A = make_shared<FFmpegContent> (film, "test/data/test.mp4");
55         shared_ptr<FFmpegContent> B = make_shared<FFmpegContent> (film, "test/data/test.mp4");
56         shared_ptr<FFmpegContent> C = make_shared<FFmpegContent> (film, "test/data/test.mp4");
57
58         film->examine_and_add_content (A);
59         film->examine_and_add_content (B);
60         film->examine_and_add_content (C);
61         wait_for_jobs ();
62
63         BOOST_CHECK_EQUAL (A->full_length(), DCPTime (288000));
64
65         A->set_position (DCPTime::from_seconds (0));
66         B->set_position (DCPTime::from_seconds (10));
67         C->set_position (DCPTime::from_seconds (20));
68
69         shared_ptr<Player> player = make_shared<Player> (film, film->playlist ());
70
71         list<shared_ptr<Piece> > o = player->overlaps (DCPTime::from_seconds (0), DCPTime::from_seconds (5), &valid);
72         BOOST_CHECK_EQUAL (o.size(), 1U);
73         BOOST_CHECK_EQUAL (o.front()->content, A);
74
75         o = player->overlaps (DCPTime::from_seconds (5), DCPTime::from_seconds (8), &valid);
76         BOOST_CHECK_EQUAL (o.size(), 0U);
77
78         o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (12), &valid);
79         BOOST_CHECK_EQUAL (o.size(), 1U);
80         BOOST_CHECK_EQUAL (o.front()->content, B);
81
82         o = player->overlaps (DCPTime::from_seconds (2), DCPTime::from_seconds (12), &valid);
83         BOOST_CHECK_EQUAL (o.size(), 2U);
84         BOOST_CHECK_EQUAL (o.front()->content, A);
85         BOOST_CHECK_EQUAL (o.back()->content, B);
86
87         o = player->overlaps (DCPTime::from_seconds (8), DCPTime::from_seconds (11), &valid);
88         BOOST_CHECK_EQUAL (o.size(), 1U);
89         BOOST_CHECK_EQUAL (o.front()->content, B);
90 }
91
92 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
93 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
94 {
95         shared_ptr<Film> film = new_test_film ("player_silence_padding_test");
96         film->set_name ("player_silence_padding_test");
97         shared_ptr<FFmpegContent> c = make_shared<FFmpegContent> (film, "test/data/test.mp4");
98         film->set_container (Ratio::from_id ("185"));
99         film->set_audio_channels (6);
100
101         film->examine_and_add_content (c);
102         wait_for_jobs ();
103
104         shared_ptr<Player> player = make_shared<Player> (film, film->playlist ());
105         shared_ptr<AudioBuffers> test = player->get_audio (DCPTime (0), DCPTime::from_seconds (1), true);
106         BOOST_CHECK_EQUAL (test->frames(), 48000);
107         BOOST_CHECK_EQUAL (test->channels(), film->audio_channels ());
108
109         for (int i = 0; i < test->frames(); ++i) {
110                 for (int c = 0; c < test->channels(); ++c) {
111                         BOOST_CHECK_EQUAL (test->data()[c][i], 0);
112                 }
113         }
114 }