Add some player tests. Fix seek with content at non-DCP frame rate. A few other...
[dcpomatic.git] / test / play_test.cc
1 /*
2     Copyright (C) 2013 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 "player.h"
21
22 /* This test needs stuff in Player that is only included in debug mode */
23 #ifdef DCPOMATIC_DEBUG
24
25 using boost::optional;
26
27 struct Video
28 {
29         boost::shared_ptr<Content> content;
30         boost::shared_ptr<const Image> image;
31         Time time;
32 };
33
34 class PlayerWrapper
35 {
36 public:
37         PlayerWrapper (shared_ptr<Player> p)
38                 : _player (p)
39         {
40                 _player->Video.connect (bind (&PlayerWrapper::process_video, this, _1, _2, _3));
41         }
42
43         void process_video (shared_ptr<const Image> i, bool, Time t)
44         {
45                 Video v;
46                 v.content = _player->_last_video;
47                 v.image = i;
48                 v.time = t;
49                 _queue.push_front (v);
50         }
51
52         optional<Video> get_video ()
53         {
54                 while (_queue.empty() && !_player->pass ()) {}
55                 if (_queue.empty ()) {
56                         return optional<Video> ();
57                 }
58                 
59                 Video v = _queue.back ();
60                 _queue.pop_back ();
61                 return v;
62         }
63
64         void seek (Time t, bool ac)
65         {
66                 _player->seek (t, ac);
67                 _queue.clear ();
68         }
69
70 private:
71         shared_ptr<Player> _player;
72         std::list<Video> _queue;
73 };
74
75 BOOST_AUTO_TEST_CASE (play_test)
76 {
77         shared_ptr<Film> film = new_test_film ("play_test");
78         film->set_dcp_content_type (DCPContentType::from_dci_name ("FTR"));
79         film->set_container (Ratio::from_id ("185"));
80         film->set_name ("play_test");
81
82         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/red_24.mp4"));
83         film->examine_and_add_content (A);
84         wait_for_jobs ();
85
86         BOOST_CHECK_EQUAL (A->video_length(), 16);
87
88         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/red_30.mp4"));
89         film->examine_and_add_content (B);
90         wait_for_jobs ();
91
92         BOOST_CHECK_EQUAL (B->video_length(), 16);
93         
94         /* Film should have been set to 25fps */
95         BOOST_CHECK_EQUAL (film->dcp_video_frame_rate(), 25);
96
97         BOOST_CHECK_EQUAL (A->start(), 0);
98         /* A is 16 frames long at 25 fps */
99         BOOST_CHECK_EQUAL (B->start(), 16 * TIME_HZ / 25);
100
101         shared_ptr<Player> player = film->player ();
102         PlayerWrapper wrap (player);
103         /* Seek and audio don't get on at the moment */
104         player->disable_audio ();
105
106         for (int i = 0; i < 32; ++i) {
107                 optional<Video> v = wrap.get_video ();
108                 BOOST_CHECK (v);
109                 if (i < 16) {
110                         BOOST_CHECK (v.get().content == A);
111                 } else {
112                         BOOST_CHECK (v.get().content == B);
113                 }
114         }
115
116         player->seek (10 * TIME_HZ / 25, true);
117         optional<Video> v = wrap.get_video ();
118         BOOST_CHECK (v);
119         cout << (v.get().time * 25 / TIME_HZ) << "\n";
120         BOOST_CHECK_EQUAL (v.get().time, 10 * TIME_HZ / 25);
121 }
122
123 #endif