Python - 是否有将 pjsip 与 python 一起使用的示例开源项目?

标签 python centos fedora archlinux pjsip

我正在 CentOS、Fedora、ArchLinux、Ubuntu 中尝试使用 Python 2.7.3 的 pjsip - 注册 - sip 音频编解码器编码/解码 - 媒体访问麦克风,扬声器输出 - 网络眩晕,转向

到目前为止,我已经编写了以下代码,它可以注册 sip,调用电话保持连接。

但问题是我无法捕获麦克风并在发生故障的扬声器中收听音频。所以我正在寻找一些稳定的工作样本(如果有的话)。任何人都可以请教吗?

import sys
import pjsua as pj

LOG_LEVEL=3
current_call = None

def log_cb(level, str, len):
    print str,


class MyAccountCallback(pj.AccountCallback):

    def __init__(self, account=None):
        pj.AccountCallback.__init__(self, account)

    # Notification on incoming call
    def on_incoming_call(self, call):
        global current_call 
        if current_call:
            call.answer(486, "Busy")
            return

        print "Incoming call from ", call.info().remote_uri
        print "Press 'a' to answer"

        current_call = call

        call_cb = MyCallCallback(current_call)
        current_call.set_callback(call_cb)

        current_call.answer(180)


# 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):
        global current_call
        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 + ")"

        if self.call.info().state == pj.CallState.DISCONNECTED:
            current_call = None
            print 'Current call is', current_call

    # 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"

# Function to make call
def make_call(uri):
    try:
        print "Making call to", uri
        return acc.make_call(uri, cb=MyCallCallback())
    except pj.Error, e:
        print "Exception: " + str(e)
        return None


# 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))

    # Create UDP transport which listens to any available port
    transport = lib.create_transport(pj.TransportType.UDP, 
                                     pj.TransportConfig(0))
    print "\nListening on", transport.info().host, 
    print "port", transport.info().port, "\n"

    # Start the library
    lib.start()

    # when no sound card found
    lib.set_null_snd_dev()  # here is the problem i cant capture mic, speaker from local system

    # Create local account
    #acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
    acc = lib.create_account(pj.AccountConfig("192.168.1.12", "abc", "admin2013"))
    cb = MyAccountCallback(acc)
    acc.set_callback(cb)
    #cb.wait()
    print "\n"
    print "Registration complete, status=", acc.info().reg_status, \
          "(" + acc.info().reg_reason + ")"    

    # If argument is specified then make call to the URI
    if len(sys.argv) > 1:
        lck = lib.auto_lock()
        current_call = make_call(sys.argv[1])
        print 'Current call is', current_call
        del lck

    my_sip_uri = "sip:" + transport.info().host + \
                 ":" + str(transport.info().port)

    # Menu loop
    while True:
        print "My SIP URI is", my_sip_uri
        print "Menu:  m=make call, h=hangup call, a=answer call, q=quit"

        input = sys.stdin.readline().rstrip("\r\n")
        if input == "m":
            if current_call:
                print "Already have another call"
                continue
            print "Enter destination URI to call: ", 
            input = sys.stdin.readline().rstrip("\r\n")
            if input == "":
                continue
            lck = lib.auto_lock()
            current_call = make_call(input)
            del lck

        elif input == "h":
            if not current_call:
                print "There is no call"
                continue
            current_call.hangup()

        elif input == "a":
            if not current_call:
                print "There is no call"
                continue
            current_call.answer(200)

        elif input == "q":
            break

    # 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

最佳答案

您的音频问题与我在以下内容中的解释有关:C - how to resolve this error while using pjsip? .这样,您应该能够正确建立通话。

另一方面,关于使用 pjsippython 的项目,我只能记忆起 python-sipsimple , 但我对此没有太多经验。

希望这对您有所帮助。

关于Python - 是否有将 pjsip 与 python 一起使用的示例开源项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20298058/

相关文章:

python - 在每个列表元素上调用 int() 函数?

python - 如何读取字符串的第一行?

python - 通过 os.system 推送

git - 升级 Fedora 33 后 ssh 权限被拒绝(公钥)

php - 比较 PHP 的 __get() 与 Python 中的 __get__() 和 __getattr__()

python - 从 bash 文件调用脚本时,iPython embed() 自动退出

find - 获取目录中最近 30 天内修改的所有文件

c++ - CentOS 上的 Autoconf AC_CHECK_SIZEOF 始终为 0

linux - 如何在不传递日期参数的情况下查看目录中最后创建的 3 个文件

linux - BigTop repo 服务器 403 错误