Merge branch '1.0' into 1.0-seek
[dcpomatic.git] / test / 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 using boost::optional;
32
33 #define FFMPEG_SEEK_TEST_DEBUG 1
34
35 optional<DCPTime> first_video;
36 optional<DCPTime> first_audio;
37 shared_ptr<Film> film;
38
39 static void
40 process_video (shared_ptr<PlayerImage>, Eyes, ColourConversion, bool, DCPTime t)
41 {
42         if (!first_video) {
43                 first_video = t;
44         }
45 }
46
47 static void
48 process_audio (shared_ptr<const AudioBuffers>, DCPTime t)
49 {
50         if (!first_audio) {
51                 first_audio = t;
52         }
53 }
54
55 static string
56 print_time (DCPTime t, float fps)
57 {
58         stringstream s;
59         s << t << " " << (float(t) / TIME_HZ) << "s " << (float(t) * fps / TIME_HZ) << "f";
60         return s.str ();
61 }
62
63 static void
64 check (shared_ptr<Player> p, DCPTime t)
65 {
66         first_video.reset ();
67         first_audio.reset ();
68
69 #if FFMPEG_SEEK_TEST_DEBUG == 1
70         cout << "\n-- Seek to " << print_time (t, 24) << "\n";
71 #endif  
72         
73         p->seek (t, true);
74         while (!first_video || !first_audio) {
75                 p->pass ();
76         }
77
78 #if FFMPEG_SEEK_TEST_DEBUG == 1
79         cout << "First video " << print_time (first_video.get(), 24) << "\n";
80         cout << "First audio " << print_time (first_audio.get(), 24) << "\n";
81 #endif  
82
83         /* Outputs should be on or after seek time */
84         BOOST_CHECK (first_video.get() >= t);
85         BOOST_CHECK (first_audio.get() >= t);
86         /* And should be rounded to frame boundaries */
87         BOOST_CHECK ((first_video.get() % (TIME_HZ / film->video_frame_rate())) == 0);
88         BOOST_CHECK ((first_audio.get() % (TIME_HZ / film->audio_frame_rate())) == 0);
89 }
90
91 BOOST_AUTO_TEST_CASE (ffmpeg_seek_test)
92 {
93         film = new_test_film ("ffmpeg_audio_test");
94         film->set_name ("ffmpeg_audio_test");
95         film->set_container (Ratio::from_id ("185"));
96         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/staircase.mov"));
97         c->set_ratio (Ratio::from_id ("185"));
98         film->examine_and_add_content (c);
99
100         wait_for_jobs ();
101
102         shared_ptr<Player> player = film->make_player ();
103         player->Video.connect (boost::bind (&process_video, _1, _2, _3, _4, _5));
104         player->Audio.connect (boost::bind (&process_audio, _1, _2));
105
106         check (player, 0);
107         check (player, 0.1 * TIME_HZ);
108         check (player, 0.2 * TIME_HZ);
109         check (player, 0.3 * TIME_HZ);
110 }
111
112