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