/*
Copyright (c) 2004 Joseph Gleason
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Current versions of this and other code can be downloaded at:
http://gleason.cc/
*/
package cc.glsn.soundrelay;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.IdentityHashMap;
import java.util.Iterator;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import cc.glsn.PCMonitor;
/** Sound Relay Server
Reads audio data from some sort of audio line-in
And sends that data to clients who connect on the given TCP
port.
It works for me, but is hacked togather. I'm probably not selecting
the audio line in the right way. I'm not supporting changing the sampling rate
or any of the other easily changed items.
All exceptions are caught by user.
Command Line Paremeters: SoundRelayServer port
port - TCP port number to answer on
*/
public class SoundRelayServer
{
IdentityHashMap Listeners;
private static int BuffSize=1048576;
private static int ReadSize=4096;
public static void main(String Args[]) throws Exception
{
new SoundRelayServer(Args);
}
public SoundRelayServer(String Args[]) throws Exception
{
int Port=new Integer(Args[0]).intValue();
Listeners=new IdentityHashMap(16);
new SoundGrabberThread().start();
ServerSocket SS=new ServerSocket(Port);
while(SS.isBound())
{
Socket Sock=SS.accept();
new ClientThread(Sock).start();
}
}
private void addListener(ClientThread ct)
{
synchronized(Listeners)
{
Listeners.put(ct,ct);
}
}
private void rmListener(ClientThread ct)
{
synchronized(Listeners)
{
Listeners.remove(ct);
}
}
private class ClientThread extends Thread
{
PCMonitor Q;
Socket Sock;
OutputStream Out;
public ClientThread(Socket sock)
{
Sock=sock;
Q=new PCMonitor();
addListener(this);
}
public void addData(Object D)
{
Q.Produce(D);
}
public void run()
{
try
{
Out=Sock.getOutputStream();
while(true)
{
DataSegment DS=(DataSegment)Q.Consume();
Out.write(DS.Buff,0,DS.Size);
}
}
catch(Exception e)
{
//e.printStackTrace();
System.out.println("Client disconnected.");
rmListener(this);
}
}
}
private class SoundGrabberThread extends Thread
{
TargetDataLine Line;
public void run()
{
AudioFormat AF=new AudioFormat(44100.0f,16,2,true,false);
System.out.println(AF.toString());
DataLine.Info info=new DataLine.Info(TargetDataLine.class,AF);
if (!AudioSystem.isLineSupported(info))
{
System.out.println("System does not support select formated line.");
System.exit(1);
}
try
{
Line=(TargetDataLine) AudioSystem.getLine(info);
System.out.println(Line.getLineInfo().toString());
Line.open(AF,BuffSize);
Line.start();
while(true)
{
DataSegment DS=new DataSegment(ReadSize);
DS.Size=Line.read(DS.Buff,0,ReadSize);
//System.out.println("Bytes read: " + DS.Size);
if (DS.Size>0)
{
synchronized(Listeners)
{
for(Iterator I=Listeners.keySet().iterator(); I.hasNext(); )
{
ClientThread CT=(ClientThread)I.next();
CT.addData(DS);
}
}
}
else
{
sleep(100);
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
private class DataSegment
{
public DataSegment(int sz)
{
Size=0;
Buff=new byte[sz];
}
int Size;
byte[] Buff;
}
}