Fix build on RPM systems.
[dcpomatic.git] / test / player_test.cc
1 /*
2     Copyright (C) 2014-2021 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
22 /** @file  test/player_test.cc
23  *  @brief Test Player class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/audio_buffers.h"
29 #include "lib/audio_content.h"
30 #include "lib/butler.h"
31 #include "lib/compose.hpp"
32 #include "lib/config.h"
33 #include "lib/content_factory.h"
34 #include "lib/cross.h"
35 #include "lib/dcp_content.h"
36 #include "lib/dcp_content_type.h"
37 #include "lib/dcpomatic_log.h"
38 #include "lib/ffmpeg_content.h"
39 #include "lib/film.h"
40 #include "lib/image_content.h"
41 #include "lib/image_png.h"
42 #include "lib/player.h"
43 #include "lib/ratio.h"
44 #include "lib/string_text_file_content.h"
45 #include "lib/text_content.h"
46 #include "lib/video_content.h"
47 #include "test.h"
48 #include <boost/test/unit_test.hpp>
49 #include <boost/algorithm/string.hpp>
50 #include <iostream>
51
52
53 using std::cout;
54 using std::list;
55 using std::shared_ptr;
56 using std::make_shared;
57 using std::vector;
58 using boost::bind;
59 using boost::optional;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
62 #endif
63 using namespace dcpomatic;
64
65
66 static shared_ptr<AudioBuffers> accumulated;
67
68
69 static void
70 accumulate (shared_ptr<AudioBuffers> audio, DCPTime)
71 {
72         BOOST_REQUIRE (accumulated);
73         accumulated->append (audio);
74 }
75
76
77 /** Check that the Player correctly generates silence when used with a silent FFmpegContent */
78 BOOST_AUTO_TEST_CASE (player_silence_padding_test)
79 {
80         auto film = new_test_film ("player_silence_padding_test");
81         film->set_name ("player_silence_padding_test");
82         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
83         film->set_container (Ratio::from_id ("185"));
84         film->set_audio_channels (6);
85
86         film->examine_and_add_content (c);
87         BOOST_REQUIRE (!wait_for_jobs());
88
89         accumulated = std::make_shared<AudioBuffers>(film->audio_channels(), 0);
90
91         Player player(film, Image::Alignment::COMPACT);
92         player.Audio.connect(bind(&accumulate, _1, _2));
93         while (!player.pass()) {}
94         BOOST_REQUIRE (accumulated->frames() >= 48000);
95         BOOST_CHECK_EQUAL (accumulated->channels(), film->audio_channels ());
96
97         for (int i = 0; i < 48000; ++i) {
98                 for (int c = 0; c < accumulated->channels(); ++c) {
99                         BOOST_CHECK_EQUAL (accumulated->data()[c][i], 0);
100                 }
101         }
102 }
103
104
105 /* Test insertion of black frames between separate bits of video content */
106 BOOST_AUTO_TEST_CASE (player_black_fill_test)
107 {
108         auto film = new_test_film ("black_fill_test");
109         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
110         film->set_name ("black_fill_test");
111         film->set_container (Ratio::from_id ("185"));
112         film->set_sequence (false);
113         film->set_interop (false);
114         auto contentA = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
115         auto contentB = std::make_shared<ImageContent>("test/data/simple_testcard_640x480.png");
116
117         film->examine_and_add_content (contentA);
118         film->examine_and_add_content (contentB);
119         BOOST_REQUIRE (!wait_for_jobs());
120
121         contentA->video->set_length (3);
122         contentA->set_position (film, DCPTime::from_frames(2, film->video_frame_rate()));
123         contentA->video->set_custom_ratio (1.85);
124         contentB->video->set_length (1);
125         contentB->set_position (film, DCPTime::from_frames(7, film->video_frame_rate()));
126         contentB->video->set_custom_ratio (1.85);
127
128         make_and_verify_dcp (
129                 film,
130                 {
131                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
132                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
133                 });
134
135         boost::filesystem::path ref;
136         ref = "test";
137         ref /= "data";
138         ref /= "black_fill_test";
139
140         boost::filesystem::path check;
141         check = "build";
142         check /= "test";
143         check /= "black_fill_test";
144         check /= film->dcp_name();
145
146         /* This test is concerned with the image, so we'll ignore any
147          * differences in sound between the DCP and the reference to avoid test
148          * failures for unrelated reasons.
149          */
150         check_dcp(ref.string(), check.string(), true);
151 }
152
153
154 /** Check behaviour with an awkward playlist whose data does not end on a video frame start */
155 BOOST_AUTO_TEST_CASE (player_subframe_test)
156 {
157         auto film = new_test_film ("reels_test7");
158         film->set_name ("reels_test7");
159         film->set_container (Ratio::from_id("185"));
160         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
161         auto A = content_factory("test/data/flat_red.png")[0];
162         film->examine_and_add_content (A);
163         BOOST_REQUIRE (!wait_for_jobs());
164         auto B = content_factory("test/data/awkward_length.wav")[0];
165         film->examine_and_add_content (B);
166         BOOST_REQUIRE (!wait_for_jobs());
167         film->set_video_frame_rate (24);
168         A->video->set_length (3 * 24);
169
170         BOOST_CHECK (A->full_length(film) == DCPTime::from_frames(3 * 24, 24));
171         BOOST_CHECK (B->full_length(film) == DCPTime(289920));
172         /* Length should be rounded up from B's length to the next video frame */
173         BOOST_CHECK (film->length() == DCPTime::from_frames(3 * 24 + 1, 24));
174
175         Player player(film, Image::Alignment::COMPACT);
176         player.setup_pieces();
177         BOOST_REQUIRE_EQUAL(player._black._periods.size(), 1U);
178         BOOST_CHECK(player._black._periods.front() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
179         BOOST_REQUIRE_EQUAL(player._silent._periods.size(), 1U);
180         BOOST_CHECK(player._silent._periods.front() == DCPTimePeriod(DCPTime(289920), DCPTime::from_frames(3 * 24 + 1, 24)));
181 }
182
183
184 static Frame video_frames;
185 static Frame audio_frames;
186
187
188 static void
189 video (shared_ptr<PlayerVideo>, DCPTime)
190 {
191         ++video_frames;
192 }
193
194 static void
195 audio (shared_ptr<AudioBuffers> audio, DCPTime)
196 {
197         audio_frames += audio->frames();
198 }
199
200
201 /** Check with a video-only file that the video and audio emissions happen more-or-less together */
202 BOOST_AUTO_TEST_CASE (player_interleave_test)
203 {
204         auto film = new_test_film ("ffmpeg_transcoder_basic_test_subs");
205         film->set_name ("ffmpeg_transcoder_basic_test");
206         film->set_container (Ratio::from_id ("185"));
207         film->set_audio_channels (6);
208
209         auto c = std::make_shared<FFmpegContent>("test/data/test.mp4");
210         film->examine_and_add_content (c);
211         BOOST_REQUIRE (!wait_for_jobs ());
212
213         auto s = std::make_shared<StringTextFileContent>("test/data/subrip.srt");
214         film->examine_and_add_content (s);
215         BOOST_REQUIRE (!wait_for_jobs ());
216
217         Player player(film, Image::Alignment::COMPACT);
218         player.Video.connect(bind(&video, _1, _2));
219         player.Audio.connect(bind(&audio, _1, _2));
220         video_frames = audio_frames = 0;
221         while (!player.pass()) {
222                 BOOST_CHECK (abs(video_frames - (audio_frames / 2000)) <= 8);
223         }
224 }
225
226
227 /** Test some seeks towards the start of a DCP with awkward subtitles; see mantis #1085
228  *  and a number of others.  I thought this was a player seek bug but in fact it was
229  *  caused by the subtitle starting just after the start of the video frame and hence
230  *  being faded out.
231  */
232 BOOST_AUTO_TEST_CASE (player_seek_test)
233 {
234         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
235         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs");
236         film->examine_and_add_content (dcp, true);
237         BOOST_REQUIRE (!wait_for_jobs ());
238         dcp->only_text()->set_use (true);
239
240         Player player(film, Image::Alignment::COMPACT);
241         player.set_fast();
242         player.set_always_burn_open_subtitles();
243         player.set_play_referenced();
244
245         auto butler = std::make_shared<Butler>(
246                 film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
247                 );
248
249         for (int i = 0; i < 10; ++i) {
250                 auto t = DCPTime::from_frames (i, 24);
251                 butler->seek (t, true);
252                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
253                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
254                 write_image(video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test_%1.png", i));
255                 /* This 14.08 is empirically chosen (hopefully) to accept changes in rendering between the reference and a test machine
256                    (17.10 and 16.04 seem to anti-alias a little differently) but to reject gross errors e.g. missing fonts or missing
257                    text altogether.
258                 */
259                 check_image(TestPaths::private_data() / String::compose("player_seek_test_%1.png", i), String::compose("build/test/player_seek_test_%1.png", i), 14.08);
260         }
261 }
262
263
264 /** Test some more seeks towards the start of a DCP with awkward subtitles */
265 BOOST_AUTO_TEST_CASE (player_seek_test2)
266 {
267         auto film = std::make_shared<Film>(optional<boost::filesystem::path>());
268         auto dcp = std::make_shared<DCPContent>(TestPaths::private_data() / "awkward_subs2");
269         film->examine_and_add_content (dcp, true);
270         BOOST_REQUIRE (!wait_for_jobs ());
271         dcp->only_text()->set_use (true);
272
273         Player player(film, Image::Alignment::COMPACT);
274         player.set_fast();
275         player.set_always_burn_open_subtitles();
276         player.set_play_referenced();
277
278         auto butler = std::make_shared<Butler>
279                 (film, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::DISABLED
280                  );
281
282         butler->seek(DCPTime::from_seconds(5), true);
283
284         for (int i = 0; i < 10; ++i) {
285                 auto t = DCPTime::from_seconds(5) + DCPTime::from_frames (i, 24);
286                 butler->seek (t, true);
287                 auto video = butler->get_video(Butler::Behaviour::BLOCKING, 0);
288                 BOOST_CHECK_EQUAL(video.second.get(), t.get());
289                 write_image(
290                         video.first->image(bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true), String::compose("build/test/player_seek_test2_%1.png", i)
291                         );
292                 check_image(TestPaths::private_data() / String::compose("player_seek_test2_%1.png", i), String::compose("build/test/player_seek_test2_%1.png", i), 14.08);
293         }
294 }
295
296
297 /** Test a bug when trimmed content follows other content */
298 BOOST_AUTO_TEST_CASE (player_trim_test)
299 {
300        auto film = new_test_film2 ("player_trim_test");
301        auto A = content_factory("test/data/flat_red.png")[0];
302        film->examine_and_add_content (A);
303        BOOST_REQUIRE (!wait_for_jobs ());
304        A->video->set_length (10 * 24);
305        auto B = content_factory("test/data/flat_red.png")[0];
306        film->examine_and_add_content (B);
307        BOOST_REQUIRE (!wait_for_jobs ());
308        B->video->set_length (10 * 24);
309        B->set_position (film, DCPTime::from_seconds(10));
310        B->set_trim_start(film, ContentTime::from_seconds(2));
311
312        make_and_verify_dcp (film);
313 }
314
315
316 struct Sub {
317         PlayerText text;
318         TextType type;
319         optional<DCPTextTrack> track;
320         DCPTimePeriod period;
321 };
322
323
324 static void
325 store (list<Sub>* out, PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
326 {
327         Sub s;
328         s.text = text;
329         s.type = type;
330         s.track = track;
331         s.period = period;
332         out->push_back (s);
333 }
334
335
336 /** Test ignoring both video and audio */
337 BOOST_AUTO_TEST_CASE (player_ignore_video_and_audio_test)
338 {
339         auto film = new_test_film2 ("player_ignore_video_and_audio_test");
340         auto ff = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
341         film->examine_and_add_content (ff);
342         auto text = content_factory("test/data/subrip.srt")[0];
343         film->examine_and_add_content (text);
344         BOOST_REQUIRE (!wait_for_jobs());
345         text->only_text()->set_type (TextType::CLOSED_CAPTION);
346         text->only_text()->set_use (true);
347
348         Player player(film, Image::Alignment::COMPACT);
349         player.set_ignore_video();
350         player.set_ignore_audio();
351
352         list<Sub> out;
353         player.Text.connect(bind (&store, &out, _1, _2, _3, _4));
354         while (!player.pass()) {}
355
356         BOOST_CHECK_EQUAL (out.size(), 6U);
357 }
358
359
360 /** Trigger a crash due to the assertion failure in Player::emit_audio */
361 BOOST_AUTO_TEST_CASE (player_trim_crash)
362 {
363         auto film = new_test_film2 ("player_trim_crash");
364         auto boon = content_factory(TestPaths::private_data() / "boon_telly.mkv")[0];
365         film->examine_and_add_content (boon);
366         BOOST_REQUIRE (!wait_for_jobs());
367
368         Player player(film, Image::Alignment::COMPACT);
369         player.set_fast();
370         auto butler = std::make_shared<Butler>(
371                 film, player, AudioMapping(), 6, bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::COMPACT, true, false, Butler::Audio::ENABLED
372                 );
373
374         /* Wait for the butler to fill */
375         dcpomatic_sleep_seconds (5);
376
377         boon->set_trim_start(film, ContentTime::from_seconds(5));
378
379         butler->seek (DCPTime(), true);
380
381         /* Wait for the butler to refill */
382         dcpomatic_sleep_seconds (5);
383
384         butler->rethrow ();
385 }
386
387
388 /** Test a crash when the gap between the last audio and the start of a silent period is more than 1 sample */
389 BOOST_AUTO_TEST_CASE (player_silence_crash)
390 {
391         auto film = new_test_film2 ("player_silence_crash");
392         auto sine = content_factory("test/data/impulse_train.wav")[0];
393         film->examine_and_add_content (sine);
394         BOOST_REQUIRE (!wait_for_jobs());
395
396         sine->set_video_frame_rate(film, 23.976);
397         film->write_metadata ();
398         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
399 }
400
401
402 /** Test a crash when processing a 3D DCP */
403 BOOST_AUTO_TEST_CASE (player_3d_test_1)
404 {
405         auto film = new_test_film2 ("player_3d_test_1a");
406         auto left = content_factory("test/data/flat_red.png")[0];
407         film->examine_and_add_content (left);
408         auto right = content_factory("test/data/flat_blue.png")[0];
409         film->examine_and_add_content (right);
410         BOOST_REQUIRE (!wait_for_jobs());
411
412         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
413         left->set_position (film, DCPTime());
414         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
415         right->set_position (film, DCPTime());
416         film->set_three_d (true);
417
418         make_and_verify_dcp (film);
419
420         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
421         auto film2 = new_test_film2 ("player_3d_test_1b", {dcp});
422
423         film2->set_three_d (true);
424         make_and_verify_dcp (film2);
425 }
426
427
428 /** Test a crash when processing a 3D DCP as content in a 2D project */
429 BOOST_AUTO_TEST_CASE (player_3d_test_2)
430 {
431         auto left = content_factory("test/data/flat_red.png")[0];
432         auto right = content_factory("test/data/flat_blue.png")[0];
433         auto film = new_test_film2 ("player_3d_test_2a", {left, right});
434
435         left->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
436         left->set_position (film, DCPTime());
437         right->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
438         right->set_position (film, DCPTime());
439         film->set_three_d (true);
440
441         make_and_verify_dcp (film);
442
443         auto dcp = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
444         auto film2 = new_test_film2 ("player_3d_test_2b", {dcp});
445
446         make_and_verify_dcp (film2);
447 }
448
449
450 /** Test a crash when there is video-only content at the end of the DCP and a frame-rate conversion is happening;
451  *  #1691.
452  */
453 BOOST_AUTO_TEST_CASE (player_silence_at_end_crash)
454 {
455         /* 25fps DCP with some audio */
456         auto content1 = content_factory("test/data/flat_red.png")[0];
457         auto film1 = new_test_film2 ("player_silence_at_end_crash_1", {content1});
458         content1->video->set_length (25);
459         film1->set_video_frame_rate (25);
460         make_and_verify_dcp (film1);
461
462         /* Make another project importing this DCP */
463         auto content2 = std::make_shared<DCPContent>(film1->dir(film1->dcp_name()));
464         auto film2 = new_test_film2 ("player_silence_at_end_crash_2", {content2});
465
466         /* and importing just the video MXF on its own at the end */
467         optional<boost::filesystem::path> video;
468         for (auto i: boost::filesystem::directory_iterator(film1->dir(film1->dcp_name()))) {
469                 if (boost::starts_with(i.path().filename().string(), "j2c_")) {
470                         video = i.path();
471                 }
472         }
473
474         BOOST_REQUIRE (video);
475         auto content3 = content_factory(*video)[0];
476         film2->examine_and_add_content (content3);
477         BOOST_REQUIRE (!wait_for_jobs());
478         content3->set_position (film2, DCPTime::from_seconds(1.5));
479         film2->set_video_frame_rate (24);
480         make_and_verify_dcp (film2);
481 }
482
483
484 /** #2257 */
485 BOOST_AUTO_TEST_CASE (encrypted_dcp_with_no_kdm_gives_no_butler_error)
486 {
487         auto content = content_factory("test/data/flat_red.png")[0];
488         auto film = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error", { content });
489         int constexpr length = 24 * 25;
490         content->video->set_length(length);
491         film->set_encrypted (true);
492         make_and_verify_dcp (
493                 film,
494                 {
495                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
496                 });
497
498         auto content2 = std::make_shared<DCPContent>(film->dir(film->dcp_name()));
499         auto film2 = new_test_film2 ("encrypted_dcp_with_no_kdm_gives_no_butler_error2", { content2 });
500
501         Player player(film, Image::Alignment::COMPACT);
502         Butler butler(film2, player, AudioMapping(), 2, bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED);
503
504         float buffer[2000 * 6];
505         for (int i = 0; i < length; ++i) {
506                 butler.get_video(Butler::Behaviour::BLOCKING, 0);
507                 butler.get_audio(Butler::Behaviour::BLOCKING, buffer, 2000);
508         }
509
510         BOOST_CHECK_NO_THROW(butler.rethrow());
511 }
512
513
514 BOOST_AUTO_TEST_CASE (interleaved_subtitle_are_emitted_correctly)
515 {
516         boost::filesystem::path paths[2] = {
517                 "build/test/interleaved_subtitle_are_emitted_correctly1.srt",
518                 "build/test/interleaved_subtitle_are_emitted_correctly2.srt"
519         };
520
521         dcp::File subs_file[2] = { dcp::File(paths[0], "w"), dcp::File(paths[1], "w") };
522
523         fprintf(subs_file[0].get(), "1\n00:00:01,000 -> 00:00:02,000\nSub 1/1\n\n");
524         fprintf(subs_file[0].get(), "2\n00:00:05,000 -> 00:00:06,000\nSub 1/2\n\n");
525
526         fprintf(subs_file[1].get(), "1\n00:00:00,500 -> 00:00:01,500\nSub 2/1\n\n");
527         fprintf(subs_file[1].get(), "2\n00:00:02,000 -> 00:00:03,000\nSub 2/2\n\n");
528
529         subs_file[0].close();
530         subs_file[1].close();
531
532         auto subs1 = content_factory(paths[0])[0];
533         auto subs2 = content_factory(paths[1])[0];
534         auto film = new_test_film2("interleaved_subtitle_are_emitted_correctly", { subs1, subs2 });
535         film->set_sequence(false);
536         subs1->set_position(film, DCPTime());
537         subs2->set_position(film, DCPTime());
538
539         Player player(film, Image::Alignment::COMPACT);
540         dcp::Time last;
541         player.Text.connect([&last](PlayerText text, TextType, optional<DCPTextTrack>, dcpomatic::DCPTimePeriod) {
542                 for (auto sub: text.string) {
543                         BOOST_CHECK(sub.in() >= last);
544                         last = sub.in();
545                 }
546         });
547         while (!player.pass()) {}
548 }
549
550
551 BOOST_AUTO_TEST_CASE(multiple_sound_files_bug)
552 {
553         Cleanup cl;
554
555         Config::instance()->set_log_types(Config::instance()->log_types() | LogEntry::TYPE_DEBUG_PLAYER);
556
557         auto A = content_factory(TestPaths::private_data() / "kook" / "1.wav").front();
558         auto B = content_factory(TestPaths::private_data() / "kook" / "2.wav").front();
559         auto C = content_factory(TestPaths::private_data() / "kook" / "3.wav").front();
560
561         auto film = new_test_film2("multiple_sound_files_bug", { A, B, C }, &cl);
562         film->set_audio_channels(16);
563         C->set_position(film, DCPTime(3840000));
564
565         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
566
567         check_mxf_audio_file(TestPaths::private_data() / "kook" / "reference.mxf", dcp_file(film, "pcm_"));
568
569         cl.run();
570 }
571
572
573 BOOST_AUTO_TEST_CASE(trimmed_sound_mix_bug_13)
574 {
575         auto A = content_factory("test/data/sine_16_48_440_10.wav").front();
576         auto B = content_factory("test/data/sine_16_44.1_440_10.wav").front();
577         auto film = new_test_film2("trimmed_sound_mix_bug_13", { A, B });
578         film->set_audio_channels(16);
579
580         A->set_position(film, DCPTime());
581         A->audio->set_gain(-12);
582         B->set_position(film, DCPTime());
583         B->audio->set_gain(-12);
584         B->set_trim_start(film, ContentTime(13));
585
586         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
587         check_mxf_audio_file("test/data/trimmed_sound_mix_bug_13.mxf", dcp_file(film, "pcm_"));
588 }
589
590
591 BOOST_AUTO_TEST_CASE(trimmed_sound_mix_bug_13_frame_rate_change)
592 {
593         auto A = content_factory("test/data/sine_16_48_440_10.wav").front();
594         auto B = content_factory("test/data/sine_16_44.1_440_10.wav").front();
595         auto film = new_test_film2("trimmed_sound_mix_bug_13_frame_rate_change", { A, B });
596
597         A->set_position(film, DCPTime());
598         A->audio->set_gain(-12);
599         B->set_position(film, DCPTime());
600         B->audio->set_gain(-12);
601         B->set_trim_start(film, ContentTime(13));
602
603         A->set_video_frame_rate(film, 24);
604         B->set_video_frame_rate(film, 24);
605         film->set_video_frame_rate(25);
606         film->set_audio_channels(16);
607
608         make_and_verify_dcp(film, { dcp::VerificationNote::Code::MISSING_CPL_METADATA });
609         check_mxf_audio_file("test/data/trimmed_sound_mix_bug_13_frame_rate_change.mxf", dcp_file(film, "pcm_"));
610 }
611
612
613 BOOST_AUTO_TEST_CASE(two_d_in_three_d_duplicates)
614 {
615         auto A = content_factory("test/data/flat_red.png").front();
616         auto B = content_factory("test/data/flat_green.png").front();
617         auto film = new_test_film2("two_d_in_three_d_duplicates", { A, B });
618
619         film->set_three_d(true);
620         B->video->set_frame_type(VideoFrameType::THREE_D_LEFT_RIGHT);
621         B->set_position(film, DCPTime::from_seconds(10));
622         B->video->set_custom_size(dcp::Size(1998, 1080));
623
624         auto player = std::make_shared<Player>(film, film->playlist());
625
626         std::vector<uint8_t> red_line(1998 * 3);
627         for (int i = 0; i < 1998; ++i) {
628                 red_line[i * 3] = 255;
629         };
630
631         std::vector<uint8_t> green_line(1998 * 3);
632         for (int i = 0; i < 1998; ++i) {
633                 green_line[i * 3 + 1] = 255;
634         };
635
636         Eyes last_eyes = Eyes::RIGHT;
637         optional<DCPTime> last_time;
638         player->Video.connect([&last_eyes, &last_time, &red_line, &green_line](shared_ptr<PlayerVideo> video, dcpomatic::DCPTime time) {
639                 BOOST_CHECK(last_eyes != video->eyes());
640                 last_eyes = video->eyes();
641                 if (video->eyes() == Eyes::LEFT) {
642                         BOOST_CHECK(!last_time || time == *last_time + DCPTime::from_frames(1, 24));
643                 } else {
644                         BOOST_CHECK(time == *last_time);
645                 }
646                 last_time = time;
647
648                 auto image = video->image([](AVPixelFormat) { return AV_PIX_FMT_RGB24; }, VideoRange::FULL, false);
649                 auto const size = image->size();
650                 for (int y = 0; y < size.height; ++y) {
651                         uint8_t* line = image->data()[0] + y * image->stride()[0];
652                         if (time < DCPTime::from_seconds(10)) {
653                                 BOOST_REQUIRE_EQUAL(memcmp(line, red_line.data(), 1998 * 3), 0);
654                         } else {
655                                 BOOST_REQUIRE_EQUAL(memcmp(line, green_line.data(), 1998 * 3), 0);
656                         }
657                 }
658         });
659
660         BOOST_CHECK(film->length() == DCPTime::from_seconds(20));
661         while (!player->pass()) {}
662 }
663
664
665 BOOST_AUTO_TEST_CASE(three_d_in_two_d_chooses_left)
666 {
667         auto left = content_factory("test/data/flat_red.png").front();
668         auto right = content_factory("test/data/flat_green.png").front();
669         auto mono = content_factory("test/data/flat_blue.png").front();
670
671         auto film = new_test_film2("three_d_in_two_d_chooses_left", { left, right, mono });
672
673         left->video->set_frame_type(VideoFrameType::THREE_D_LEFT);
674         left->set_position(film, dcpomatic::DCPTime());
675         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
676         right->set_position(film, dcpomatic::DCPTime());
677
678         mono->set_position(film, dcpomatic::DCPTime::from_seconds(10));
679
680         auto player = std::make_shared<Player>(film, film->playlist());
681
682         std::vector<uint8_t> red_line(1998 * 3);
683         for (int i = 0; i < 1998; ++i) {
684                 red_line[i * 3] = 255;
685         };
686
687         std::vector<uint8_t> blue_line(1998 * 3);
688         for (int i = 0; i < 1998; ++i) {
689                 blue_line[i * 3 + 2] = 255;
690         };
691
692         optional<DCPTime> last_time;
693         player->Video.connect([&last_time, &red_line, &blue_line](shared_ptr<PlayerVideo> video, dcpomatic::DCPTime time) {
694                 BOOST_CHECK(video->eyes() == Eyes::BOTH);
695                 BOOST_CHECK(!last_time || time == *last_time + DCPTime::from_frames(1, 24));
696                 last_time = time;
697
698                 auto image = video->image([](AVPixelFormat) { return AV_PIX_FMT_RGB24; }, VideoRange::FULL, false);
699                 auto const size = image->size();
700                 for (int y = 0; y < size.height; ++y) {
701                         uint8_t* line = image->data()[0] + y * image->stride()[0];
702                         if (time < DCPTime::from_seconds(10)) {
703                                 BOOST_REQUIRE_EQUAL(memcmp(line, red_line.data(), 1998 * 3), 0);
704                         } else {
705                                 BOOST_REQUIRE_EQUAL(memcmp(line, blue_line.data(), 1998 * 3), 0);
706                         }
707                 }
708         });
709
710         BOOST_CHECK(film->length() == DCPTime::from_seconds(20));
711         while (!player->pass()) {}
712 }
713
714
715 BOOST_AUTO_TEST_CASE(check_seek_with_no_video)
716 {
717         auto content = content_factory(TestPaths::private_data() / "Fight.Club.1999.720p.BRRip.x264-x0r.srt")[0];
718         auto film = new_test_film2("check_seek_with_no_video", { content });
719         auto player = std::make_shared<Player>(film, film->playlist());
720
721         boost::signals2::signal<void (std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime)> Video;
722
723         optional<dcpomatic::DCPTime> earliest;
724
725         player->Video.connect(
726                 [&earliest](shared_ptr<PlayerVideo>, dcpomatic::DCPTime time) {
727                         if (!earliest || time < *earliest) {
728                                 earliest = time;
729                         }
730                 });
731
732         player->seek(dcpomatic::DCPTime::from_seconds(60 * 60), false);
733
734         for (int i = 0; i < 10; ++i) {
735                 player->pass();
736         }
737
738         BOOST_REQUIRE(earliest);
739         BOOST_CHECK(*earliest >= dcpomatic::DCPTime(60 * 60));
740 }
741