comparison mp4.t @ 384:1d67bc8fa680

Tests: mp4 tests added.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 26 Mar 2014 19:58:00 +0400
parents
children ff2e9afde90d
comparison
equal deleted inserted replaced
383:4e0e795e0019 384:1d67bc8fa680
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';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http mp4/)->has_daemon('ffprobe ffmpeg')
27 ->plan(12)->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 }
48
49 EOF
50
51 system('ffmpeg -loglevel quiet -y '
52 . '-f lavfi -i testsrc=duration=10:size=320x200:rate=15 '
53 . '-f lavfi -i testsrc=duration=20:size=320x200:rate=15 '
54 . '-map 0:0 -map 1:0 -pix_fmt yuv420p -g 15 -c:v libx264 '
55 . "${\($t->testdir())}/test.mp4") == 0
56 or die "Can't create mp4 file: $!";
57
58 $t->run();
59
60 ###############################################################################
61
62 is(durations($t, 0.0), '10.0 20.0', 'start zero');
63 is(durations($t, 2), '8.0 18.0', 'start integer');
64 is(durations($t, 7.1), '2.9 12.9', 'start float');
65
66 SKIP: {
67 skip 'no end argument support', 7 unless $t->has_version('1.5.13');
68
69 is(durations($t, 6, 9), '3.0 3.0', 'start end integer');
70 is(durations($t, 2.7, 5.6), '2.9 2.9', 'start end float');
71
72 is(durations($t, undef, 9), '9.0 9.0', 'end integer');
73 is(durations($t, undef, 5.6), '5.6 5.6', 'end float');
74
75 # invalid range results in ignoring end argument
76
77 like(http_head('/test.mp4?start=1&end=1'), qr/200 OK/, 'zero range');
78 like(http_head('/test.mp4?start=1&end=0'), qr/200 OK/, 'negative range');
79
80 # start/end values exceeding file duration
81
82 like(http_head("/test.mp4?end=11"), qr!HTTP/1.1 500!, 'end beyond EOF');
83
84 }
85
86 like(http_head("/test.mp4?start=21"), qr!HTTP/1.1 500!, 'start beyond EOF');
87
88 # check for alerts, e.g., "zero buf in output", shouldn't be any
89
90 like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts');
91
92 ###############################################################################
93
94 sub durations {
95 my ($t, $start, $end) = @_;
96 my $path = $t->{_testdir} . '/frag.mp4';
97
98 my $uri = '/test.mp4';
99 if (defined $start) {
100 $uri .= "?start=$start";
101 if (defined $end) {
102 $uri .= "&end=$end";
103 }
104
105 } elsif (defined $end) {
106 $uri .= "?end=$end";
107 }
108
109 $t->write_file('frag.mp4', Test::Nginx::http_content(http_get($uri)));
110
111 my $r = `ffprobe -show_streams $path 2>/dev/null`;
112 Test::Nginx::log_core('||', $r);
113 sprintf "%.1f %.1f", $r =~ /duration=(\d+\.\d+)/g;
114 }
115
116 ###############################################################################