Last active
December 28, 2016 23:24
-
-
Save Informatic/fa150ee2aa88eaa4a71fc25c85829978 to your computer and use it in GitHub Desktop.
Simple SIP DTMF-to-keypress program I hacked up on 33c3. It is for tetris, you know.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $Id$ | |
# | |
# SIP DTMF-to-keypress | |
# | |
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
# | |
import sys | |
import pjsua as pj | |
import subprocess | |
LOG_LEVEL=3 | |
# Logging callback | |
def log_cb(level, str, len): | |
print str, | |
# Callback to receive events from account | |
class MyAccountCallback(pj.AccountCallback): | |
def __init__(self, account=None): | |
pj.AccountCallback.__init__(self, account) | |
# Notification on incoming call | |
def on_incoming_call(self, call): | |
print "Incoming call from ", call.info().remote_uri | |
current_call = call | |
call_cb = MyCallCallback(current_call) | |
current_call.set_callback(call_cb) | |
current_call.answer(200) | |
# Callback to receive events from Call | |
class MyCallCallback(pj.CallCallback): | |
def __init__(self, call=None): | |
pj.CallCallback.__init__(self, call) | |
# Notification when call state has changed | |
def on_state(self): | |
print "Call with", self.call.info().remote_uri, | |
print "is", self.call.info().state_text, | |
print "last code =", self.call.info().last_code, | |
print "(" + self.call.info().last_reason + ")" | |
# Notification when call's media state has changed. | |
def on_media_state(self): | |
if self.call.info().media_state == pj.MediaState.ACTIVE: | |
# Connect the call to sound device | |
call_slot = self.call.info().conf_slot | |
pj.Lib.instance().conf_connect(call_slot, 0) | |
pj.Lib.instance().conf_connect(0, call_slot) | |
print "Media is now active" | |
else: | |
print "Media is inactive" | |
def on_dtmf_digit(self, digit): | |
print digit | |
try: | |
keymap = { | |
'2': 'Up', | |
'8': 'Down', | |
'4': 'Left', | |
'6': 'Right', | |
} | |
if digit in keymap: | |
subprocess.call(['xdotool', 'key', keymap[digit]]) | |
else: | |
print 'no key for', digit | |
except Exception, e: | |
print 'guwno', e | |
# Create library instance | |
lib = pj.Lib() | |
try: | |
# Init library with default config and some customized | |
# logging config. | |
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) | |
lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5060)) | |
# Start the library | |
lib.start() | |
acc_cfg = pj.AccountConfig("voip.eventphone.de", sys.argv[1], sys.argv[2]) | |
my_cb = MyAccountCallback() | |
acc = lib.create_account(acc_cfg, cb=my_cb) | |
# Menu loop | |
while True: | |
inp = sys.stdin.readline().rstrip("\r\n") | |
# Shutdown the library | |
transport = None | |
acc.delete() | |
acc = None | |
lib.destroy() | |
lib = None | |
except pj.Error, e: | |
print "Exception: " + str(e) | |
lib.destroy() | |
lib = None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment