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