Debian Codegraph @

Xpjproject/tests/pjsua/mod_media_playrec.py

raw symbols SCIP
1
2 # PLAYFILE -> RECFILE:
3 # Input file is played and is recorded to output, then compare them.
4 # Useful to tes clock rates compatibility and resample quality
5 # null-audio
6 # port 1: wav file input xxxxxx.clock_rate.wav, e.g: test1.8.wav
7 # port 2: wav file ouput xxxxxx.clock_rate.wav, e.g: res1.8.wav
8 # wav input must be more than 3 seconds long
9
10 import time
11 import sys
12 import re
13 import subprocess
14 import inc_const as const
15 import inc_util as util
16 from inc_cfg import *
17
18 # Load configuration
19 cfg_file = util.load_module_from_file("cfg_file", ARGS[1])
20
21 # WAV similarity calculator
22 COMPARE_WAV_EXE = ""
23 if sys.platform.find("win32")!=-1:
24 COMPARE_WAV_EXE = "tools/cmp_wav.exe"
25 G_INUNIX = False
26 else:
27 COMPARE_WAV_EXE = "tools/cmp_wav"
28 G_INUNIX = True
29
30
31 # Threshold to declare degradation is too high when result is lower than this value
32 COMPARE_THRESHOLD = 2
33
34 # COMPARE params
35 input_filename = "" # Input filename
36 output_filename = "" # Output filename
37
38 # Test body function
39 def test_func(t):
40 global input_filename
41 global output_filename
42
43 endpt = t.process[0]
44
45 # Get input file name
46 input_filename = re.compile(const.MEDIA_PLAY_FILE).search(endpt.inst_param.arg).group(1)
47 endpt.trace("Input file = " + input_filename)
48
49 # Get output file name
50 output_filename = re.compile(const.MEDIA_REC_FILE).search(endpt.inst_param.arg).group(1)
51 endpt.trace("Output file = " + output_filename)
52
53 # Find appropriate clock rate for the input file
54 clock_rate = re.compile(".+(\.\d+\.wav)$").match(output_filename).group(1)
55 if (clock_rate==None):
56 endpt.trace("Cannot compare input & output, incorrect output filename format")
57 return
58 input_filename = re.sub("\.\d+\.wav$", clock_rate, input_filename)
59 endpt.trace("WAV file to be compared with output = " + input_filename)
60
61 # Connect input-output file
62 endpt.sync_stdout()
63
64 endpt.send("cc 1 2")
65 endpt.expect(const.MEDIA_CONN_PORT_SUCCESS)
66
67 # Wait
68 time.sleep(3)
69
70 endpt.sync_stdout()
71
72 # Disconnect input-output file
73 endpt.send("cd 1 2")
74 endpt.expect(const.MEDIA_DISCONN_PORT_SUCCESS)
75
76
77 # Post body function
78 def post_func(t):
79 global input_filename
80 global output_filename
81
82 endpt = t.process[0]
83
84 # Check WAV similarity
85 fullcmd = COMPARE_WAV_EXE + " " + input_filename + " " + output_filename + " " + "3000"
86 endpt.trace("Popen " + fullcmd)
87 cmp_proc = subprocess.Popen(fullcmd, shell=G_INUNIX, stdout=subprocess.PIPE, universal_newlines=True)
88
89 # Parse similarity ouput
90 line = cmp_proc.stdout.readline()
91 mo_sim_val = re.match(".+=\s+(\d+)", line)
92 if (mo_sim_val == None):
93 raise TestError("Error comparing WAV files")
94 return
95
96 # Evaluate the similarity value
97 sim_val = mo_sim_val.group(1)
98 if (int(sim_val) >= COMPARE_THRESHOLD):
99 endpt.trace("WAV similarity = " + sim_val)
100 else:
101 raise TestError("WAV degraded heavily, similarity = " + sim_val)
102
103
104 # Here where it all comes together
105 test = cfg_file.test_param
106 test.test_func = test_func
107 test.post_func = post_func