Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio doesn't work on call, and dtfm isn't capturing values and not working either. #283

Open
coolcunt opened this issue Oct 28, 2024 · 6 comments

Comments

@coolcunt
Copy link

here's my code,
(password's and the server ip and phone number has been changed obviously cuz of reasons but try it out, i'm currently using python 3.10.0 and the latest version of pyvoip. lmk how it goes (Call works, like it can call the person, but it wont play audio or listen to dtfm inputs, and just auto hangup in 7-9 seconds)

from pyVoIP.VoIP import VoIPPhone, InvalidStateError, CallState
import time
import wave
import socket
import logging

SIP_SERVER = "123.45.67.89"
SIP_USERNAME = "username"
SIP_PASSWORD = "password"
PHONE_NUMBER = "+123456789"

Setup logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name)

def get_local_ip():
"""Get the local IP address of the machine"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
local_ip = s.getsockname()[0]
except Exception:
local_ip = '127.0.0.1'
finally:
s.close()
return local_ip

def answer(call):
try:
f = wave.open('raw_audio.wav', 'rb')
frames = f.getnframes()
data = f.readframes(frames)
f.close()

    call.answer()
    call.write_audio(data)

    while call.state == CallState.ANSWERED:
        dtmf = call.get_dtmf()
        if dtmf == "1":
            print("pressed one")
            # Do something
            call.hangup()
        elif dtmf == "2":
            print("pressed 2")
            call.hangup()
        time.sleep(0.1)
except InvalidStateError:
    pass
except:
    call.hangup()

if name == 'main':
local_ip = get_local_ip()
phone = VoIPPhone(
server=SIP_SERVER,
port=5060,
username=SIP_USERNAME,
password=SIP_PASSWORD,
myIP=local_ip,
callCallback=answer
)
phone.start()
logger.info(f"Using local IP: {local_ip}")
logger.info(f"Calling phone number: {PHONE_NUMBER}")
phone.call(PHONE_NUMBER)
input('Press enter to disable the phone')
phone.stop()

@tomlynn
Copy link

tomlynn commented Oct 29, 2024 via email

@coolcunt
Copy link
Author

and that's all u wanted to say? T~T

@FloppiTuna
Copy link

FloppiTuna commented Dec 13, 2024

Don't listen to him. He sounds like an ass anyway.

As for the issue itself; are you using a PJSIP account or a plain SIP account (assuming you're using something like Asterisk/FreePBX here)?

@coolcunt
Copy link
Author

ah, im using a smth like magnus billing, it's my friend's route, btw update, i was able to fix the dtfm handling and auto hangup if i dont play audio, now on python 3.12.8, but as soon as i try to add audio, it doesnt even call T~T.

`from pyVoIP.VoIP import VoIPPhone, InvalidStateError
import time
import wave
import socket
from queue import Queue

def handle_dtmf(call, dtmf_queue):
while True:
try:
digit = call.get_dtmf()
if digit:
print(f"Received DTMF: {digit}")
dtmf_queue.put(digit)
time.sleep(0.1)
except:
# looks like the call dropped
break

def make_call(phone):
try:
# dialing the number now
call = phone.call("my_number") # making the call

    # setting up a queue for button presses
    dtmf_queue = Queue()
    
    # starting up the button handler in background
    import threading
    dtmf_thread = threading.Thread(target=handle_dtmf, args=(call, dtmf_queue))
    dtmf_thread.daemon = True
    dtmf_thread.start()
    
    # keeping the call going
    while True:
        try:
            # making sure call is still active
            _ = call.get_dtmf()
            
            # checking for any button presses
            if not dtmf_queue.empty():
                digit = dtmf_queue.get()
                # handling what button was pressed
                if digit == "1":
                    print("Option 1 selected")
                elif digit == "2":
                    print("Option 2 selected") 
                elif digit == "3":
                    print("Option 3 selected")
                    
            time.sleep(0.1)
        except:
            # call ended, we're done here
            break
        
except InvalidStateError:
    pass
except Exception as e:
    print(f"Error making call: {e}")

if name == 'main':
# getting our local ip address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# quick connection to get ip
s.connect(('10.255.255.255', 1))
local_ip = s.getsockname()[0]
except Exception:
local_ip = '127.0.0.1'
finally:
s.close()

phone = VoIPPhone("my_sip_server", 5060, "my_username", "my_password", myIP=local_ip)
phone.start()

# starting the call now
make_call(phone)

input('press enter to end the call')
phone.stop()`

has no audio here but yea, please help me, any help will be appreciated, thanks. (i removed audio func so it works), (using 1.6.8 stable version)

@coolcunt
Copy link
Author

tried making similar script in 2.0.0a, but it calls faster, but no audio, no dtfm either,

`from pyVoIP.credentials import CredentialsManager
from pyVoIP.VoIP.call import VoIPCall, CallState
from pyVoIP.VoIP.error import InvalidStateError
from pyVoIP.VoIP.phone import VoIPPhone, VoIPPhoneParameter
import time
import wave
import socket
from queue import Queue
import threading

class Call(VoIPCall):
def init(self, *args, **kwargs):
super().init(*args, **kwargs)
self.dtmf_queue = Queue()

def handle_dtmf(self):
    while True:
        try:
            digit = self.get_dtmf()
            if digit:
                print(f"Received DTMF: {digit}")
                self.dtmf_queue.put(digit)
            time.sleep(0.1)
        except:
            break

def ringing(self, invite_request):
    try:
        f = wave.open('prompt.wav', 'rb')
        frames = f.getnframes()
        data = f.readframes(frames)
        f.close()
        self.answer()
        
        dtmf_thread = threading.Thread(target=self.handle_dtmf)
        dtmf_thread.daemon = True
        dtmf_thread.start()
        
        self.write_audio(data)
        
        while self.state == CallState.ANSWERED:
            if not self.dtmf_queue.empty():
                dtmf = self.dtmf_queue.get()
                if dtmf == "1":
                    if self.transfer("sales"):
                        return
                elif dtmf == "2":
                    if self.transfer(uri="<100@different_regisrar.com>"):
                        return
            time.sleep(0.1)
    except InvalidStateError:
        pass
    except Exception as e:
        print(f"Error in call: {e}")
        self.hangup()

if name == 'main':
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1))
local_ip = s.getsockname()[0]
except Exception:
local_ip = '127.0.0.1'
finally:
s.close()

cm = CredentialsManager()
cm.add("username", "password")
params = VoIPPhoneParameter("sip.example.com", 5060, "username", cm, bind_ip=local_ip, call_class=Call)
phone = VoIPPhone(params)
phone.start()
call = phone.call("+15555555555")
while call.state != CallState.ANSWERED:
    time.sleep(0.1)
input('Press enter to disable the phone')
phone.stop()

`
python 3.11,9, win 11, and uh magnus billing based sip, (btw when i tested the old script(with 1.6.8, python 3.12, it worked for dtfm, but no audio),

@coolcunt
Copy link
Author

@FloppiTuna what do u think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@tomlynn @FloppiTuna @coolcunt and others