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