comparison mp4_start_key_frame.t @ 1746:be8d5213d6b9

Tests: mp4_start_key_frame tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 17 Nov 2021 12:57:05 +0300
parents
children 2a0a6035a1af
comparison
equal deleted inserted replaced
1745:823f603da727 1746:be8d5213d6b9
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for the mp4_start_key_frame directive.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx qw/ :DEFAULT http_content /;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http mp4/)->has_daemon('ffprobe')
26 ->has_daemon('ffmpeg')
27 ->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 server {
40 listen 127.0.0.1:8080;
41 server_name localhost;
42
43 location / {
44 mp4;
45 }
46
47 location /force/ {
48 mp4;
49 mp4_start_key_frame on;
50 alias %%TESTDIR%%/;
51 }
52 }
53 }
54
55 EOF
56
57 plan(skip_all => 'no lavfi')
58 unless grep /lavfi/, `ffmpeg -loglevel quiet -formats`;
59 system('ffmpeg -nostdin -loglevel quiet -y '
60 . '-f lavfi -i testsrc=duration=10:size=320x200:rate=15 '
61 . '-pix_fmt yuv420p -g 15 -c:v libx264 '
62 . "${\($t->testdir())}/test.mp4") == 0
63 or die "Can't create mp4 file: $!";
64 $t->try_run('no mp4_start_key_frame')->plan(4);
65
66 ###############################################################################
67
68 # baseline durations
69
70 my $test_uri = '/test.mp4';
71 is(durations($t, 2.0, 4.0), '2.00', 'start at key frame');
72 isnt(durations($t, 2.1, 4.0), '1.90', 'start off key frame');
73
74 # with forced start at key frame
75
76 $test_uri = '/force/test.mp4';
77 is(durations($t, 2.0, 4.0), '2.00', 'start at key frame force');
78 is(durations($t, 2.1, 4.0), '1.90', 'start off key frame force');
79
80 ###############################################################################
81
82 sub durations {
83 my ($t, $start, $end) = @_;
84 my $path = $t->{_testdir} . '/frag.mp4';
85
86 my $uri = $test_uri;
87 if (defined $start) {
88 $uri .= "?start=$start";
89 if (defined $end) {
90 $uri .= "&end=$end";
91 }
92
93 } elsif (defined $end) {
94 $uri .= "?end=$end";
95 }
96
97 $t->write_file('frag.mp4', http_content(http_get($uri)));
98
99 my $r = `ffprobe -show_streams $path 2>/dev/null`;
100 Test::Nginx::log_core('||', $r);
101 sprintf "%.2f", $r =~ /duration=(\d+\.\d+)/g;
102 }
103
104 ###############################################################################