Merge master.
[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 /** @file  test/ffmpeg_seek_test.cc
21  *  @brief Test seek using Player with an FFmpegDecoder; note that the player
22  *  can hide problems with FFmpegDecoder seeking as it will skip frames / insert
23  *  black as it sees fit.
24  */
25
26 #include <boost/test/unit_test.hpp>
27 #include "lib/player.h"
28 #include "lib/ffmpeg_decoder.h"
29 #include "lib/film.h"
30 #include "lib/ratio.h"
31 #include "test.h"
32
33 using std::cout;
34 using std::string;
35 using std::stringstream;
36 using boost::shared_ptr;
37 using boost::optional;
38
39 #define FFMPEG_SEEK_TEST_DEBUG 1
40
41 optional<DCPTime> first_video;
42 optional<DCPTime> first_audio;
43 shared_ptr<Film> film;
44
45 static void
46 process_video (shared_ptr<PlayerImage>, Eyes, ColourConversion, bool, DCPTime t)
47 {
48         if (!first_video) {
49                 first_video = t;
50         }
51 }
52
53 static void
54 process_audio (shared_ptr<const AudioBuffers>, DCPTime t)
55 {
56         if (!first_audio) {
57                 first_audio = t;
58         }
59 }
60
61 static string
62 print_time (DCPTime t, float fps)
63 {
64         stringstream s;
65         s << t.seconds() << "s " << t.frames (fps) << "f";
66         return s.str ();
67 }
68
69 static void
70 check (shared_ptr<Player> p, DCPTime t)
71 {
72         first_video.reset ();
73         first_audio.reset ();
74
75 #if FFMPEG_SEEK_TEST_DEBUG == 1
76         cout << "\n-- Seek to " << print_time (t, 24) << "\n";
77 #endif  
78         
79         p->seek (t, true);
80         while (!first_video || !first_audio) {
81                 p->pass ();
82         }
83
84 #if FFMPEG_SEEK_TEST_DEBUG == 1
85         cout << "First video " << print_time (first_video.get(), 24) << "\n";
86         cout << "First audio " << print_time (first_audio.get(), 24) << "\n";
87 #endif  
88
89         /* Outputs should be on or after seek time */
90         BOOST_CHECK (first_video.get() >= t);
91         BOOST_CHECK (first_audio.get() >= t);
92         /* And should be rounded to frame boundaries */
93         BOOST_CHECK_EQUAL (first_video.get(), first_video.get().round_up (film->video_frame_rate()));
94         BOOST_CHECK_EQUAL (first_audio.get(), first_audio.get().round_up (film->audio_frame_rate()));
95 }
96
97 /* Test basic seeking */
98 BOOST_AUTO_TEST_CASE (ffmpeg_seek_test)
99 {
100         film = new_test_film ("ffmpeg_seek_test");
101         film->set_name ("ffmpeg_seek_test");
102         film->set_container (Ratio::from_id ("185"));
103         shared_ptr<FFmpegContent> c (new FFmpegContent (film, "test/data/staircase.mov"));
104         c->set_scale (VideoContentScale (Ratio::from_id ("185")));
105         film->examine_and_add_content (c);
106
107         wait_for_jobs ();
108
109         shared_ptr<Player> player = film->make_player ();
110         player->Video.connect (boost::bind (&process_video, _1, _2, _3, _4, _5));
111         player->Audio.connect (boost::bind (&process_audio, _1, _2));
112
113         check (player, DCPTime::from_seconds (0));
114         check (player, DCPTime::from_seconds (0.1));
115         check (player, DCPTime::from_seconds (0.2));
116         check (player, DCPTime::from_seconds (0.3));
117 }