Mercurial > hg > nginx-tests
annotate mp4.t @ 1905:f35824e75b66
Tests: fixed reading QUIC streams on Perl < 5.24.
The parse_stream() routine has had a missing explicit return if there were no
streams received. In Perl < 5.24 this used to return no value, or an empty
array in the list context. In modern Perl this returns an empty value, or an
array of 1 element, which made the check for last index of the returned array
work rather by accident.
The fix is to return explicitly and to check the array size in callers instead.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 06 Jun 2023 18:50:07 +0400 |
parents | 5ac6efbe5552 |
children |
rev | line source |
---|---|
384 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for mp4 module. | |
7 # Ensures that requested stream duration is given with sane accuracy. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 | |
16 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
17 | |
18 use lib 'lib'; | |
1612
8659123d2d37
Tests: made http_content() exportable.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1556
diff
changeset
|
19 use Test::Nginx qw/ :DEFAULT http_content /; |
384 | 20 |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
389
c24d1c0b4c73
Tests: fixed invocation of has_daemon().
Sergey Kandaurov <pluknet@nginx.com>
parents:
388
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/http mp4/)->has_daemon('ffprobe') |
c24d1c0b4c73
Tests: fixed invocation of has_daemon().
Sergey Kandaurov <pluknet@nginx.com>
parents:
388
diff
changeset
|
27 ->has_daemon('ffmpeg') |
390
7a65ebfdb02e
Tests: skip mp4 if ffmpeg does not support lavfi format.
Sergey Kandaurov <pluknet@nginx.com>
parents:
389
diff
changeset
|
28 ->write_file_expand('nginx.conf', <<'EOF'); |
384 | 29 |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 http { | |
38 %%TEST_GLOBALS_HTTP%% | |
39 | |
40 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
41 listen 127.0.0.1:8080; |
384 | 42 server_name localhost; |
43 | |
44 location / { | |
45 mp4; | |
46 } | |
47 } | |
48 } | |
49 | |
50 EOF | |
51 | |
390
7a65ebfdb02e
Tests: skip mp4 if ffmpeg does not support lavfi format.
Sergey Kandaurov <pluknet@nginx.com>
parents:
389
diff
changeset
|
52 plan(skip_all => 'no lavfi') |
7a65ebfdb02e
Tests: skip mp4 if ffmpeg does not support lavfi format.
Sergey Kandaurov <pluknet@nginx.com>
parents:
389
diff
changeset
|
53 unless grep /lavfi/, `ffmpeg -loglevel quiet -formats`; |
1268
1923461981c9
Tests: prevent broken terminal after tc[gs]etattr ffmpeg race.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1251
diff
changeset
|
54 system('ffmpeg -nostdin -loglevel quiet -y ' |
384 | 55 . '-f lavfi -i testsrc=duration=10:size=320x200:rate=15 ' |
56 . '-f lavfi -i testsrc=duration=20:size=320x200:rate=15 ' | |
57 . '-map 0:0 -map 1:0 -pix_fmt yuv420p -g 15 -c:v libx264 ' | |
58 . "${\($t->testdir())}/test.mp4") == 0 | |
59 or die "Can't create mp4 file: $!"; | |
1268
1923461981c9
Tests: prevent broken terminal after tc[gs]etattr ffmpeg race.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1251
diff
changeset
|
60 system('ffmpeg -nostdin -loglevel quiet -y ' |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
61 . '-f lavfi -i testsrc=duration=10:size=320x200:rate=15 ' |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
62 . '-f lavfi -i testsrc=duration=20:size=320x200:rate=15 ' |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
63 . '-map 0:0 -map 1:0 -pix_fmt yuv420p -g 15 -c:v libx264 ' |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
64 . '-movflags +faststart ' |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
65 . "${\($t->testdir())}/no_mdat.mp4") == 0 |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
66 or die "Can't create mp4 file: $!"; |
384 | 67 |
1555
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
68 my $sbad = <<'EOF'; |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
69 00000000: 00 00 00 1c 66 74 79 70 69 73 6f 6d 00 00 02 00 |....ftypisom....| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
70 00000010: 69 73 6f 6d 69 73 6f 32 6d 70 34 31 00 00 00 09 |isomiso2mp41....| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
71 00000020: 6d 64 61 74 00 00 00 00 94 6d 6f 6f 76 00 00 00 |mdat.....moov...| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
72 00000030: 8c 74 72 61 6b 00 00 00 84 6d 64 69 61 00 00 00 |.trak....mdia...| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
73 00000040: 7c 6d 69 6e 66 00 00 00 74 73 74 62 6c 00 00 00 ||minf...tstbl...| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
74 00000050: 18 73 74 74 73 00 00 00 00 00 00 00 01 00 00 03 |.stts...........| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
75 00000060: 3a 00 00 04 00 00 00 00 28 73 74 73 63 00 00 00 |:.......(stsc...| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
76 00000070: 00 00 00 00 02 00 00 00 01 00 00 03 0f 00 00 00 |................| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
77 00000080: 01 00 00 00 02 00 00 00 2b 00 00 00 01 00 00 00 |........+.......| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
78 00000090: 14 73 74 73 7a 00 00 00 00 00 00 05 a9 00 00 03 |.stsz...........| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
79 000000a0: 3b 00 00 00 18 63 6f 36 34 00 00 00 00 00 00 00 |;....co64.......| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
80 000000b0: 01 ff ff ff ff f0 0f fb e7 |.........| |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
81 EOF |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
82 |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
83 $t->write_file('bad.mp4', unhex($sbad)); |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
84 $t->run()->plan(27); |
384 | 85 |
86 ############################################################################### | |
87 | |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
88 my $test_uri = '/test.mp4'; |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
89 |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
90 again: |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
91 |
384 | 92 is(durations($t, 0.0), '10.0 20.0', 'start zero'); |
93 is(durations($t, 2), '8.0 18.0', 'start integer'); | |
94 is(durations($t, 7.1), '2.9 12.9', 'start float'); | |
95 | |
96 is(durations($t, 6, 9), '3.0 3.0', 'start end integer'); | |
97 is(durations($t, 2.7, 5.6), '2.9 2.9', 'start end float'); | |
98 | |
99 is(durations($t, undef, 9), '9.0 9.0', 'end integer'); | |
100 is(durations($t, undef, 5.6), '5.6 5.6', 'end float'); | |
101 | |
102 # invalid range results in ignoring end argument | |
103 | |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
104 like(http_head("$test_uri?start=1&end=1"), qr/200 OK/, 'zero range'); |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
105 like(http_head("$test_uri?start=1&end=0"), qr/200 OK/, 'negative range'); |
384 | 106 |
387
ff2e9afde90d
Tests: adjusted mp4 test for end value beyond track duration.
Sergey Kandaurov <pluknet@nginx.com>
parents:
384
diff
changeset
|
107 # start/end values exceeding track/file duration |
384 | 108 |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
109 unlike(http_head("$test_uri?end=11"), qr!HTTP/1.1 500!, |
387
ff2e9afde90d
Tests: adjusted mp4 test for end value beyond track duration.
Sergey Kandaurov <pluknet@nginx.com>
parents:
384
diff
changeset
|
110 'end beyond short track'); |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
111 unlike(http_head("$test_uri?end=21"), qr!HTTP/1.1 500!, 'end beyond EOF'); |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
112 unlike(http_head("$test_uri?start=11"), qr!HTTP/1.1 500!, |
388
3d4506565f43
Tests: more mp4 tests for exceeding start/end values.
Sergey Kandaurov <pluknet@nginx.com>
parents:
387
diff
changeset
|
113 'start beyond short track'); |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
114 like(http_head("$test_uri?start=21"), qr!HTTP/1.1 500!, 'start beyond EOF'); |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
115 |
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
116 $test_uri = '/no_mdat.mp4', goto again unless $test_uri eq '/no_mdat.mp4'; |
384 | 117 |
1555
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
118 # corrupted formats |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
119 |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
120 like(http_get("/bad.mp4?start=0.5"), qr/500 Internal/, 'co64 chunk beyond EOF'); |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
121 |
384 | 122 ############################################################################### |
123 | |
124 sub durations { | |
125 my ($t, $start, $end) = @_; | |
126 my $path = $t->{_testdir} . '/frag.mp4'; | |
127 | |
1037
92479d9429ff
Tests: added mp4 tests for moov atom before mdat atom.
Sergey Kandaurov <pluknet@nginx.com>
parents:
974
diff
changeset
|
128 my $uri = $test_uri; |
384 | 129 if (defined $start) { |
130 $uri .= "?start=$start"; | |
131 if (defined $end) { | |
132 $uri .= "&end=$end"; | |
133 } | |
134 | |
135 } elsif (defined $end) { | |
136 $uri .= "?end=$end"; | |
137 } | |
138 | |
1612
8659123d2d37
Tests: made http_content() exportable.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1556
diff
changeset
|
139 $t->write_file('frag.mp4', http_content(http_get($uri))); |
384 | 140 |
141 my $r = `ffprobe -show_streams $path 2>/dev/null`; | |
142 Test::Nginx::log_core('||', $r); | |
143 sprintf "%.1f %.1f", $r =~ /duration=(\d+\.\d+)/g; | |
144 } | |
145 | |
1555
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
146 sub unhex { |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
147 my ($input) = @_; |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
148 my $buffer = ''; |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
149 |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
150 for my $l ($input =~ m/: +((?:[0-9a-f]{2,4} +)+) /gms) { |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
151 for my $v ($l =~ m/[0-9a-f]{2}/g) { |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
152 $buffer .= chr(hex($v)); |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
153 } |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
154 } |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
155 |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
156 return $buffer; |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
157 } |
809d67d48d21
Tests: added mp4 test with chunk offset overflow.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1483
diff
changeset
|
158 |
384 | 159 ############################################################################### |