5

I'm developing a project which requires me to stream audio from microphone from a client to a server. The code shown below is what I have written. When I run both the client and server code the audio is not streamed live. In fact the audio from the client is stored in the buffer and when I terminate the execution of the client side code the audio from the buffer on the server gets output to the speaker. What am I doing wrong? (I'm developing on eclipse)

server:

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;

//import org.apache.commons.io.output.ByteArrayOutputStream;


public class ServerStream {
    private OutgoingSoudnListener osl = new OutgoingSoudnListener();
    boolean outVoice = true;
    AudioFormat format = getAudioFormat();
    private ServerSocket serverSocket;
    Socket server;


    private AudioFormat getAudioFormat() {
        float sampleRate = 16000.0F;
        int sampleSizeBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;

        return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
    }
    public ServerStream() throws IOException{
        try{
            System.out.println("Creating Socket...");
            serverSocket = new ServerSocket(3000);
            osl.runSender();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    class OutgoingSoudnListener{
        public void runSender(){
            try{
                server = serverSocket.accept();
                System.out.println("Listening from mic.");
                DataOutputStream out = new DataOutputStream(server.getOutputStream());
                DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class,format);
                TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(micInfo);
                mic.open(format);
                System.out.println("Mic open.");
                byte tmpBuff[] = new byte[mic.getBufferSize()/5];
                mic.start();
                while(outVoice) {
                    System.out.println("Reading from mic.");
                    int count = mic.read(tmpBuff,0,tmpBuff.length);
                    if (count > 0){
                        System.out.println("Writing buffer to server.");
                        out.write(tmpBuff, 0, count);
                    }               
                    }
                mic.drain();
                mic.close();
                System.out.println("Stopped listening from mic.");
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }
    public static void main (String args[]) throws IOException{
        new ServerStream();

    }


}

client:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

import org.apache.commons.io.IOUtils;

public class ClientStream{

    public ClientStream() throws IOException{
        isl.runListener();
    }

    private IncomingSoundListener isl = new IncomingSoundListener();    
    AudioFormat format = getAudioFormat();
    InputStream is;
    Socket client;
    String serverName = "192.168.2.8";
    int port=3000;
    boolean inVoice = true;


    private AudioFormat getAudioFormat(){
        float sampleRate = 16000.0F;
        int sampleSizeBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;

        return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
    }
    class IncomingSoundListener {
        public void runListener(){
            try{
                System.out.println("Connecting to server:"+serverName+" Port:"+port);
                client = new Socket(serverName,port); 
                System.out.println("Connected to: "+client.getRemoteSocketAddress());
                System.out.println("Listening for incoming audio.");
                DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class,format);
                SourceDataLine speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
                speaker.open(format);
                speaker.start();
                while(inVoice){
                    is = client.getInputStream();
                    byte[] data = IOUtils.toByteArray(is);  
                    ByteArrayInputStream bais = new ByteArrayInputStream(data);
                    AudioInputStream ais = new AudioInputStream(bais,format,data.length);
                    int bytesRead = 0;
                    if((bytesRead = ais.read(data)) != -1){
                        System.out.println("Writing to audio output.");
                        speaker.write(data,0,bytesRead);

       //                 bais.reset();
                    }
                    ais.close();
                    bais.close();

                }
               speaker.drain();
               speaker.close();
               System.out.println("Stopped listening to incoming audio.");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String [] args) throws IOException{
            new ClientStream();
        }
    }
Akas Antony
  • 728
  • 1
  • 8
  • 19
  • Minor point - you have mislabelled the server and client - swap over the headers:) – Martin James Oct 24 '14 at 15:01
  • I'm just trying to understand how the program works. So the server reads data in from a microphone and sends it to the client. The client gets that data and is supposed to output it to the speaker? And right now, only when you terminate the client program does the speaker play whatever sounds it's supposed to play? – user2570465 Oct 24 '14 at 15:34
  • The speaker plays the sound when i terminate the server side program. It plays the audio that got recorded until when the server side program got terminated. – Akas Antony Oct 24 '14 at 15:43
  • The data is being sent to the client side. Only that the client side plays the audio when the server side program gets terminated. – Akas Antony Oct 24 '14 at 15:44

2 Answers2

1

The problem is in client side, in the line

byte[] data = IOUtils.toByteArray(is);

It deals with the object itself, not with the content. So, you must change it to this:

byte[] data = new byte[1024];

Ghost4Man
  • 945
  • 1
  • 12
  • 18
0

I am not familiar with this, so don't get mad if I am way off here, but reading the api for DataLine it seems to function like a buffer, that you have to flush or drain in this case to get the output. Have you attempted to put the mic.drain()/speaker.drain() command in the while loop?

spanglerb
  • 101
  • 1
  • 8
  • Yup. Tired that. It dint work. It looks like the problem is on the client side because the client receives all the data sent by the server right before the termination of the server. – Akas Antony Oct 24 '14 at 16:13