【发布时间】:2016-05-31 05:33:53
【问题描述】:
我想从我的麦克风捕获音频并使用一台服务器发送到客户端(0 或 4)...
PD:我正在检查这个问题:Sending audio stream over TCP, UnsupportedAudioFileException,但面向的是一台服务器和一台客户端解决方案。 其他问题:Java - Broadcast voice over Java sockets Streaming audio from microphone with Java
我正在考虑使用 TCP(服务器/客户端模型),但服务器和 4 个客户端将接收数据(音频流),我不知道是否可以实施这种替代方案(我担心端口管理)....
这里是我的初步(它仍然不工作,因为我有疑问,我在想如何解决我的顾虑)
变量:
static boolean bThreadCapture = false;
static boolean bThreadServer = false;
static ByteArrayOutputStream myByteArrayOutStream;
ExecutorService myServerPool = Executors.newFixedThreadPool(4);
static Thread myThreadServer = null;
现在我很头疼
final int iServerPort = 2370;
final AudioFormat myAudioFormat = new AudioFormat(8000,8,1,false, true);
final DataLine.Info myDataLineInfo = new DataLine.Info(TargetDataLine.class, myAudioFormat);
if (!AudioSystem.isLineSupported(myDataLineInfo)) {
System.out.println("Line not supported");
System.exit(0);
}
try {
final TargetDataLine myTargetDataLine = (TargetDataLine) AudioSystem.getLine(myDataLineInfo);
myTargetDataLine.open(myAudioFormat);
myByteArrayOutStream = null;
//BEGIN definition Thread for CAPTURING AUDIO FROM MIC
Runnable runnAudioCapture = new Runnable() {
int bufferSize = (int) myAudioFormat.getSampleRate()* myAudioFormat.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
myByteArrayOutStream = new ByteArrayOutputStream();
bThreadCapture = true;
while (bThreadCapture) {
try {
int count = myTargetDataLine.read(buffer, 0, buffer.length);
if (count > 0) {
myByteArrayOutStream.write(buffer, 0, count);
// HERE: I need to send the bytes of Sound to all active Threads Client (from 1 until 4)
// But, How to know what (how much and which) are active threads client?
// How to access to each Executor?
}
} catch (IllegalArgumentException | ArrayIndexOutOfBoundsException e) {
System.err.println("TargetDataLine problems: " + e);
}
}
try {
if (myByteArrayOutStream != null) myByteArrayOutStream.close();
} catch (IOException e) { }
}
};
//END definition Thread for CAPTURING AUDIO FROM MIC
//BEGIN definition Thread for ATTENDANT SERVER CLIENT (Still not defined)
myThreadServer = new Thread() {
@Override
public void run() {
try {
SrvrSocketProducer = new ServerSocket(iServerPort);
System.out.println("Server Listening on port number: "+iServerPort);
} catch (IOException e) {
System.out.println("Could not listen on port: "+iServerPort);
}
new Thread(runnAudioCapture).start(); //BEGIN AUDIO CAPTURE
while(bThreadServer) {
Socket clientSocket = null;
try {
clientSocket = SrvrSocketProducer.accept();
} catch (IOException e) {
if(!bThreadServer) {
System.out.println("Server Stopped.") ;
break;
}
throw new RuntimeException("Error accepting client connection", e);
}
myServerPool.execute(new runnWorkerListener(clientSocket,myAudioFormat));
}
myServerPool.shutdown();
bThreadCapture = false; //STOP AUDIO CAPTURE
}
};
//END definition Thread for ATTENDANT SERVER CLIENT (Still not defined)
myThreadServer.start();
} catch (LineUnavailableException ex) {
System.out.println(ex.toString());
}
问题:
(合并在runnAudioCapture代码中查找帮助)
1. HERE: I need to send the bytes of Sound to all ACTIVE Threads Client (from 1 until 4)
2. But, How to know what (how much and which) are active threads client?
3. How to access to each Executor?
现在是工作线程--
class runnWorkerListener implements Runnable {
Socket innerClientSocket = null;
AudioFormat innerAdfmt = null;
public runnWorkerListener(Socket clientSocket, AudioFormat audioFormat) {
innerClientSocket = clientSocket;
innerAdfmt = audioFormat;
}
public void run() {
try {
InputStream input = innerClientSocket.getInputStream();
OutputStream output = innerClientSocket.getOutputStream();
// Now How Can I to Comunicate with runnAudioCapture?
while (RunningClientAttender /*This variable still is not defined*/) {
// I need to send (thread client here Not defined) EACH bytes from runnAudioCapture
}
output.close();
input.close();
System.out.println("Request processed: ");
} catch (IOException e) {
//report exception somewhere.
e.printStackTrace();
}
}
}
问题:
(合并到 runnWorkerListener 代码中以查找帮助)
1. Now How Can I to Comunicate with runnAudioCapture?
2. I need to send (thread client here Not defined) EACH bytes from runnAudioCapture
我的怀疑与以下内容有关: 一个线程专用于音频捕获 一个线程专用于接收客户端(1、2、3 或 4),使用 Pool 并创建线程工作者(一个线程工作者与“远程”客户端通信)
我不知道将 Capturer 线程 Capturer 与每个 Worker 线程同步的句柄...
谢谢...
【问题讨论】:
标签: java multithreading audio tcp client-server