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