Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming voice over tcp

Tags:

java

android

tcp

I've coded an app that streams audio over TCP from client to server but it does not work, i.e. no audible output. Could you check my code tell me whats wrong with it?

Client:

public void startStreaming() {


   Thread streamThread = new Thread(new Runnable() {

       @Override
       public void run() {
            try {

                int minBufSize =AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

                Log.d("VD ", "Bufer intioalised "+minBufSize);

                short[] buffer=new short[minBufSize];


                Log.d("VS","Buffer created of size .c" + minBufSize);
              //  DatagramPacket packet;

                final InetAddress destination = InetAddress.getByName(target.getText().toString());
                port=Integer.parseInt(target_port.getText().toString());

                Socket socket=new Socket(destination,port);

                DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                Log.d("VS", "Address retrieved.c");


                if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {
                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                Log.d("VS", "Recorder initialized.c");}


                if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
                    Log.d("VS", "Recorder working....c");
                    recorder.startRecording();}

                BufferedWriter input;
                while(status == true) {


                    //reading data from MIC into buffer
                  int  bufferReadResult = recorder.read(buffer, 0, buffer.length);


                      dos.write(buffer,0,bufferReadResult);

                  dos.flush();



                }

            } catch(UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("IOException message:",e.getMessage().toString());


            }



        }

Server:

public void startStreaming() { Thread streamThread = new Thread(new Runnable() {

    @Override
    public void run() {



        try {

            int  minBufSize =1024;//recorder.getMinBufferSize(sampleRate,channelConfig,audioFormat);


            ServerSocket serversocket = new ServerSocket(50005);



             // DatagramSocket socket = new DatagramSocket(50005);


              byte[] buffer = new byte[1024];


                  if (minBufSize != AudioRecord.ERROR_BAD_VALUE) {



                      speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM);


                      speaker.play();
                      Log.d("VR", "spekaer playing...");
                  }
            //  }




                  Log.d("VR", ""+status);
                  BufferedReader input;
                  InputStream is;
                  ObjectInputStream ois;
                  ByteArrayInputStream baiss;
                  socket = serversocket.accept();
                    DataInputStream dis=new DataInputStream(new BufferedInputStream(socket.getInputStream()));


                        while(status == true) {

                              //DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

                            InputStream in = socket.getInputStream();

                            Log.d("content :", socket.getOutputStream().toString());


                            int i=0;
                            while (dis.available() > 0 && i < buffer.length) {
                                buffer[i]=(byte) dis.readShort();
                              i++;
                            }

       speaker.write(buffer,0,buffer.length);

Please give me your best help.

like image 899
Ryan110 Avatar asked Nov 09 '22 08:11

Ryan110


1 Answers

The answers are in the comments. Both for server and client read()/write() had to be implemented better.

like image 61
greenapps Avatar answered Nov 14 '22 22:11

greenapps