summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/test-pjsua/mod_media_playrec.py
blob: 8d8a60fe31c8cfc728f19acdd7c03e361639bb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# $Id$

# PLAYFILE -> RECFILE:
# input file is played and is recorded to output, then compare them.
#	null-audio
#	port 1: wav file input xxxxxx.clock_rate.wav, e.g: test1.8.wav
#	port 2: wav file ouput xxxxxx.clock_rate.wav, e.g: res1.8.wav
#	wav input more than 3 seconds

import time
import imp
import sys
import re
import subprocess
import inc_const as const

# Load configuration
cfg_file = imp.load_source("cfg_file", sys.argv[2])

# WAV similarity calculator
COMPARE_WAV_EXE = "tools/cmp_wav.exe"

# UserData
class mod_media_playrec_user_data:
	input_filename = ""
	output_filename = ""

# Test body function
def test_func(t, ud):
	endpt = t.process[0]
	
	# Get input file name
	endpt.sync_stdout()
	endpt.send("dc")
	line = endpt.expect(const.MEDIA_PLAY_FILE)
	ud.input_filename = re.compile(const.MEDIA_PLAY_FILE).match(line).group(1)
	endpt.trace("Input file = " + ud.input_filename)

	# Get output file name
	endpt.sync_stdout()
	endpt.send("dc")
	line = endpt.expect(const.MEDIA_REC_FILE)
	ud.output_filename = re.compile(const.MEDIA_REC_FILE).match(line).group(1)
	endpt.trace("Output file = " + ud.output_filename)

	# Find appropriate clock rate for the input file
	clock_rate = re.compile(".+(\.\d+\.wav)$").match(ud.output_filename).group(1)
	if (clock_rate==None):
		endpt.trace("Cannot compare input & output, incorrect output filename format")
		return
	ud.input_filename = re.sub("\.\d+\.wav$", clock_rate, ud.input_filename)
	endpt.trace("WAV file to be compared with output = " + ud.input_filename)

	# Connect input-output file
	endpt.sync_stdout()

	endpt.send("cc 1 2")
	endpt.expect(const.MEDIA_CONN_PORT_SUCCESS)

	# Wait
	time.sleep(3)

	endpt.sync_stdout()

	# Disconnect input-output file
	endpt.send("cd 1 2")
	endpt.expect(const.MEDIA_DISCONN_PORT_SUCCESS)


# Post body function
def post_func(t, ud):
	endpt = t.process[0]

	# Check WAV similarity
	fullcmd = COMPARE_WAV_EXE + " " + ud.input_filename + " " + ud.output_filename + " " + "3000"
	endpt.trace("Popen " + fullcmd)
	cmp_proc = subprocess.Popen(fullcmd, stdout=subprocess.PIPE, universal_newlines=True)
	line = cmp_proc.stdout.readline()
	endpt.trace("WAV similarity = " + line)


# Here where it all comes together
test = cfg_file.test_param
test.test_func = test_func
test.post_func = post_func
test.user_data = mod_media_playrec_user_data()