Author Topic: VOIP Ingame, work in progress  (Read 3977 times)

Offline Edak

  • byte
  • *
  • Posts: 14
    • View Profile
VOIP Ingame, work in progress
« on: August 08, 2011, 09:04:02 am »
Hey all! This is a working start with server and client side, currently when connected it only records off the mic, sends through network, and plays it back on the speaker.

I've been having THE hardest time with this, not sure if anyone here could help but regardless I wanted to post the concept with hopes someone with a brighter mind that mine could solve this. I've included the server in a rar file because it uses two seperate java classes and I didn't want to post them in code tags

Known issues and points I'm struggling with:
AEC (Acoustic Noise Cancellation) I've read and read about NLMS filter, but I'm mind blown.

My phone (And from what I've read others too) has issues with plugging in head phones. the onboard mic will mute regardless of code calls to unmute it

Usage:
Set listen address and port in the voip class. Also, you must be running the server on the same network with the port open.
Code: [Select]
VOIP voice = null;
voice = new VOIP();
voice.turnOn(); //Begins listening and playing

VOIP Class (Android/Client Side)
Code: [Select]

import com.threed.jpct.Logger;

import java.net.*;
import java.io.*;
import android.media.AudioFormat;
import android.media.MediaRecorder;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.AudioManager;

class VOIP{

    Thread listenThread = null;
    Thread playThread = null;
    Socket socket = null;
    boolean Running = false;
    private int frequency = 8000;
    private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
   
    private BufferedInputStream bis = null;
    private DataInputStream dis = null;
   
    public boolean shouldCancel;
   
    private byte[] playBuffer;

    public VOIP(MainActivity ma) {
    }

    public void connect() { //When called, connects a socket to VoiceServer
        try {
            Logger.log("Attempting connection to VoiceServer...");
            socket = new Socket("192.168.43.156", 5741);
           
            bis = new BufferedInputStream(socket.getInputStream());
            dis = new DataInputStream(bis);
        } catch (Exception ex) {
            Logger.log(ex.getMessage());
        }
    }

    public void turnOn() { //starts the listening and the playing (anything said is sent and anything recieved is played)
        if (!Running) {
            connect();
            try {
                Running = true;
                listenThread = new Thread(new Runnable() {

                    public void run() {
                        listen();
                    }
                });
                listenThread.start();


                playThread = new Thread(new Runnable() {

                    public void run() {
                        play();
                    }
                });
                playThread.start();

            } catch (Exception ex) {
                Running = false;
                Logger.log(ex.getMessage());
            }



        }
    }

    public void turnOff() {
        if (Running) {
            try {
                // Close the streams and the socket.
                socket.close();
                //Set running to false (stops the listen and play loops)
                Running = false;
            } catch (IOException ex) {
                Running = false;
                Logger.log(ex.getMessage());
            }
            //Basically terminates the threads
            //listenThread = null;
            //playThread = null;
        }
    }

    public void listen() {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
        try {
            BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
            DataOutputStream dos = new DataOutputStream(bos);

            int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
            AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    frequency, channelConfiguration,
                    audioEncoding, bufferSize);

            byte[] micBuffer = new byte[bufferSize];
            audioRecord.startRecording();
            while (Running) {
                int bufferReadResult = audioRecord.read(micBuffer, 0, bufferSize);
                dos.write(micBuffer, 0, bufferReadResult);
            }


            audioRecord.stop();
            dos.close();

        } catch (Throwable t) {
            Logger.log("AudioRecord Recording Failed: "+t.getMessage());
        }
    }

    public void play() {
        int musicLength = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        playBuffer = new byte[musicLength];
        try {

            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                    frequency,
                    channelConfiguration,
                    audioEncoding,
                    musicLength,
                    AudioTrack.MODE_STREAM);
           
           
            audioTrack.play();

           
            while(Running){
                for(int i=0;i<musicLength;i++){
                        playBuffer[i] = (byte)dis.read();
                }
                audioTrack.write(playBuffer, 0, musicLength);
            }

// Close the input streams.
            dis.close();
           
        } catch (Throwable t) {
            Logger.log("AudioTrack Playback Failed: "+t.getMessage());
        }
    }
}


[attachment deleted by admin]
« Last Edit: August 08, 2011, 09:10:40 am by Edak »