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


def answer(call):
    print("Call answered!")

    try:
        f = wave.open("data/sampleaudio/BabyElephantWalk60.wav", "rb")
        frames = f.getnframes()
        data = f.readframes(frames)
        f.close()

        call.answer()
        call.write_audio(
            data
        )  # This writes the audio data to the transmit buffer, this must be bytes.

        stop = time.time() + (
            frames / 8000
        )  # frames/8000 is the length of the audio in seconds. 8000 is the hertz of PCMU.

        while time.time() <= stop and call.state == CallState.ANSWERED:
            time.sleep(0.1)
        call.hangup()
    except InvalidStateError:
        pass
    except:
        call.hangup()


if __name__ == "__main__":
    phone = VoIPPhone(
        "localhost",
        5060,
        "john.doe",
        "password",
        myIP="127.0.0.1",
        sipPort=25060,
        callCallback=answer,
    )

    phone.start()
    input("Press enter to disable the phone")
    phone.stop()
