Mostly-merge master.
[dcpomatic.git] / test / long_ffmpeg_seek_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/ffmpeg_decoder.h"
23 #include "lib/film.h"
24 #include "lib/ratio.h"
25 #include "test.h"
26
27 using std::cout;
28 using std::string;
29 using std::stringstream;
30 using boost::shared_ptr;
31
32 #define LONG_FFMPEG_SEEK_TEST_DEBUG 1
33
34 boost::optional<DCPTime> first_video;
35 boost::optional<DCPTime> first_audio;
36
37 static void
38 process_video (shared_ptr<PlayerImage>, Eyes, ColourConversion, bool, DCPTime t)
39 {
40         if (!first_video) {
41                 first_video = t;
42         }
43 }
44
45 static void
46 process_audio (shared_ptr<const AudioBuffers>, DCPTime t)
47 {
48         if (!first_audio) {
49                 first_audio = t;
50         }
51 }
52
53 static string
54 print_time (DCPTime t, float fps)
55 {
56         stringstream s;
57         s << t << " " << t.seconds() << "s " << t.frames(fps) << "f";
58         return s.str ();
59 }
60
61 static void
62 check (shared_ptr<Player> p, DCPTime t)
63 {
64         first_video.reset ();
65         first_audio.reset ();
66
67 #if LONG_FFMPEG_SEEK_TEST_DEBUG == 1
68         cout << "\n-- Seek to " << print_time (t, 24) << "\n";
69 #endif  
70         
71         p->seek (t, true);
72         while (!first_video || !first_audio) {
73                 p->pass ();
74         }
75
76 #if LONG_FFMPEG_SEEK_TEST_DEBUG == 1
77         cout << "First video " << print_time (first_video.get(), 24) << "\n";
78         cout << "First audio " << print_time (first_audio.get(), 24) << "\n";
79 #endif  
80         
81         BOOST_CHECK (first_video.get() >= t);
82         BOOST_CHECK (first_audio.get() >= t);
83 }
84
85 BOOST_AUTO_TEST_CASE (long_ffmpeg_seek_test)
86 {
87         shared_ptr<Film> film = new_test_film ("long_ffmpeg_audio_test");
88         film->set_name ("long_ffmpeg_audio_test");
89         film->set_container (Ratio::from_id ("185"));
90         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/long_data/dolby_aurora.vob"));
91         c->set_scale (VideoContentScale (Ratio::from_id ("185")));
92         film->examine_and_add_content (c);
93
94         wait_for_jobs ();
95
96         shared_ptr<Player> player = film->make_player ();
97         player->Video.connect (boost::bind (&process_video, _1, _2, _3, _4, _5));
98         player->Audio.connect (boost::bind (&process_audio, _1, _2));
99
100         for (float i = 0; i < 10; i += 0.1) {
101                 check (player, DCPTime::from_seconds (i));
102         }
103 }
104
105