Build VLC aliases cleanly (don't need to parse argv[0])
[vlc.git] / test / mangle / mangle.py
1 import VLCUtil
2 import shutil
3 import os
4 from random import randint
5 import glob
6
7 # Todo
8 # - Correctly handle errors
9 # - Launch VLC in a separate thread and detect hangs
10 # - Correct path handling
11
12 global conf
13 global l
14
15 def play_mangled( filename, logfile ):
16     os.chdir( "../.." )
17     vlc_pid = os.spawnvp( os.P_NOWAIT, "./vlc",
18         [ "vlc", "-I", "logger", "--quiet",  "--stop-time", "1",
19           filename , "vlc:quit", "--logfile", logfile ])
20     ( exit_pid, exit_status ) = os.waitpid( vlc_pid, 0 )
21     os.chdir( "test/mangle" )
22     l.debug( "VLC exited with status %i" % exit_status )
23     return exit_status
24
25 def mangle_file( filename, header_size, percentage , new_file):
26     shutil.copyfile( filename, new_file )
27     file = open ( new_file, "r+" )
28     for i in range( header_size ):
29         file.seek( i)
30         if( randint(0, 100/percentage) == 0 ):
31             file.write( "%i" % randint (0, 255 ));
32     file.flush()
33     file.close()
34
35 def process_file_once( file, header_size ):
36     suffix = randint( 0, 65535 )
37     new_file = conf["temp_folder"] + conf["prefix"] + "%i" % suffix
38     log_file = conf["temp_folder"] + conf["log_prefix"] + "%i" % suffix
39     
40     mangle_file( file, header_size, conf["mangle_ratio"], new_file )
41     status = play_mangled( new_file, log_file )
42     if( status == 0 ):
43         os.remove( new_file )
44         os.remove( log_file )
45     else:
46         l.info( "Potential crash detected : %i, saving results" % suffix )
47         try:
48             shutil.move( new_file , conf["crashdir"] )
49             shutil.move( log_file , conf["crashdir"] )
50         except:
51             l.error( "Unable to move file" )
52
53 def process_file( file, source, header_size ):
54      l.info( "Starting work on " + file )
55
56      if( len( glob.glob( conf["inputdir"] + "/" + file ) ) == 0 ):
57          l.warn( "%s does not exist in %s" % (file, conf["inputdir"] ) )
58          if( VLCUtil.downloadFile( file, source, conf["inputdir"], l ) != 0 ):
59              l.error( "Unable to download %s" % file )
60              return 
61      
62      for i in range( conf["loops"] ):
63          process_file_once( conf["inputdir"] + "/" + file, header_size )
64
65 l =  VLCUtil.getLogger( "Mangle" )
66
67 conf = {}
68 conf["inputdir"] = "input"
69 conf["crashdir"] = "crashers"
70 conf["temp_folder"] = "/tmp/"
71 conf["prefix"] = "mangle."
72 conf["log_prefix"] = "vlc-log."
73 conf["mangle_ratio"] = 4 # Change X% of bytes within header
74 conf["loops"]  = 20
75
76 l.debug( "Creating folders" )
77
78 try:
79     os.makedirs( conf["crashdir"] )
80     os.makedirs( conf["inputdir"] )
81     os.makedirs( conf["temp_folder"] )
82 except:
83     pass
84
85
86 ##########
87 process_file( "bl.mp4", "ftp://streams.videolan.org/streams-videolan/reference/mp4", 3000 )
88 process_file( "Win98Crash.mov", "ftp://streams.videolan.org/streams-videolan/reference/mov", 3000 )
89 process_file( "x264.avi", "ftp://streams.videolan.org/streams-videolan/reference/avi", 3000 )
90 process_file( "batidadomontoya.wmv", "ftp://streams.videolan.org/streams-videolan/reference/asf", 3000 )
91 process_file( "tarzan.ogm", "ftp://streams.videolan.org/streams-videolan/reference/ogm", 3000 )
92