pyVoIP icon indicating copy to clipboard operation
pyVoIP copied to clipboard

Receive an error TODO: Add 500 Error on Receiving SIP Response

Open jawad097 opened this issue 1 year ago • 7 comments

hey here is my code import logging from pyVoIP.VoIP import VoIPPhone, CallState import speech_recognition as sr import uuid import pywav from pydub import AudioSegment import os import shutil import time

Set up logging

logging.basicConfig( level=logging.DEBUG, # Log all levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) format='%(asctime)s - %(levelname)s - %(message)s', # Log format handlers=[ logging.FileHandler("voip_log.log"), # Save logs to a file logging.StreamHandler() # Also output logs to the console ] )

ffmpeg_path = shutil.which("ffmpeg") if ffmpeg_path: logging.info(f"FFmpeg found at: {ffmpeg_path}") else: logging.error("FFmpeg not found in PATH")

try: # Create a simple test audio file if it doesn't exist if not os.path.exists("test.wav"): from pydub.generators import Sine sound = Sine(440).to_audio_segment(duration=5000) # 1-second tone sound.export("test.wav", format="wav") logging.info("Test audio file 'test.wav' created.")

test_audio = AudioSegment.from_file("test.wav")
logging.info("FFmpeg is correctly configured.")

except Exception as e: logging.error(f"Error with FFmpeg configuration: {e}")

def convert_to_wav(audio, tmpFileName): try: data_bytes = b"".join(audio) wave_write = pywav.WavWrite(tmpFileName, 1, 8000, 8, 7) wave_write.write(data_bytes) wave_write.close() logging.info(f"Audio converted to WAV and saved as {tmpFileName}") return tmpFileName except Exception as e: logging.error(f"Error converting audio to WAV: {e}") return None

def transcribe_to_text(audio_file): recognizer = sr.Recognizer() try: # Load the .wav audio file and recognize speech using Sphinx with sr.AudioFile(audio_file) as source: audio = recognizer.record(source) # Use the offline Sphinx recognizer transcription = recognizer.recognize_sphinx(audio) logging.info(f"Transcription successful: {transcription}") return transcription except sr.UnknownValueError: logging.warning("Sphinx could not understand the audio.") except sr.RequestError as e: logging.error(f"Sphinx error; {e}") return ""

def answer(call): try: call.answer() logging.info("Call answered.") buffer = [] buff_length = 0

    while call.state == CallState.ANSWERED:
        audio = call.read_audio()
        buff_length += len(audio) / 8  # Sample rate is 8000 Hz

        if buff_length <= 1000:  # Buffer until 1000ms
            buffer.append(audio)
        else:
            # Save audio to a temporary .wav file for transcription
            tmpFileName = f"./_audio_buffer_{uuid.uuid4()}.wav"
            if convert_to_wav(buffer, tmpFileName):
                transcription = transcribe_to_text(tmpFileName)
                logging.info(f"Transcription: {transcription}")
            buffer = []
            buff_length = 0

except Exception as e:
    logging.error(f"Error during call processing: {e}")
finally:
    call.hangup()
    logging.info("Call hung up.")

vp = VoIPPhone('ip', 5060, 'phone name', 'passowrd', callCallback=answer) vp.start()

while vp._status == 'REGISTERING': time.sleep(5) logging.info(f"Phone is still registering... Current state: {vp._status}")

Check if the phone is successfully registered

if vp._status == 'REGISTERED': logging.info(f"Phone successfully registered with status: {vp._status}") else: logging.error(f"Phone failed to register. Current status: {vp._status}")

input("Press any key to exit the VOIP phone session.") vp.stop() logging.info("VOIP phone session stopped.") when i run this at show eh this error 2024-09-11 17:45:46,172 - INFO - FFmpeg found at: /usr/bin/ffmpeg 2024-09-11 17:45:46,172 - INFO - FFmpeg is correctly configured. 2024-09-11 17:45:46,313 - ERROR - Phone failed to register. Current status: PhoneStatus.REGISTERING Press any key to exit the VOIP phone session.TODO: Add 500 Error on Receiving SIP Response

jawad097 avatar Sep 11 '24 17:09 jawad097

Has anyone managed to get around this error?

DenysBabi avatar Dec 27 '24 10:12 DenysBabi

not yet use pjsua 2

jawad097 avatar Dec 27 '24 17:12 jawad097

Same issue. pjsua2 is great but a pain in the ass having to build the library and haven't been able to build it on windows.

github59173 avatar Feb 22 '25 00:02 github59173

This might be related: https://github.com/tayler6000/pyVoIP/pull/216

I just stumbled on the same error and captured a traffic with tshark and it looked like that:

> sudo tshark -i any -f 'host mysip.server.net' -Y sip
    1 0.000000000 172.16.32.103 → 123.123.123.123 SIP 588 Request: REGISTER sip:mysip.server.net  (1 binding) | 
    2 0.051632141 123.123.123.123 → 172.16.32.103 SIP 562 Status: 401 Unauthorized | 
    3 0.052910059 172.16.32.103 → 123.123.123.123 SIP 805 Request: REGISTER sip:mysip.server.net  (1 binding) | 
    4 0.106868033 123.123.123.123 → 172.16.32.103 SIP 592 Status: 200 OK (REGISTER)  (2 bindings) | 
    5 0.110899065 172.16.32.103 → 123.123.123.123 SIP/SDP 685 Request: INVITE sip:[email protected] | 
    6 0.157615747 123.123.123.123 → 172.16.32.103 SIP 365 Status: 100 Giving a try | 
    7 0.160666069 123.123.123.123 → 172.16.32.103 SIP 534 Status: 407 Proxy Authentication Required | 

and got the same error: TODO: Add 500 Error on Receiving SIP Response.

q84fh avatar Jul 27 '25 21:07 q84fh

I saw on Ridit I think some one created the python sip service using pjsua2

jawad097 avatar Jul 27 '25 21:07 jawad097

Yup, connection to my SIP server works correctly with pjsua2, but it is no-go for me - because it has dependencies on underlying OS that I cannot satisfy in my situation.

Anyway It would be great to have https://github.com/tayler6000/pyVoIP/pull/216 merged.

q84fh avatar Jul 27 '25 21:07 q84fh

Are you facing the problem when record or received the incoming voice from the customer end?

jawad097 avatar Jul 27 '25 21:07 jawad097