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