我没有得到的是unix域套接字之间的数据流。
我知道unix域套接字数据流是LocalSocket client .connect()
-->LocalServerSocket server .accept()
客户机将数据直接发送到服务器,这一点我完全理解。
然而,对于来自android的mediarecorder的流式视频/音频,经过大量的研究,我已经看到每个使用localsocket代替mediarecorder文件的例子,其原理如下:
https://static.foxdogstudios.com/static/presentations/keeping-it-real-time.html#/9
LocalServerSocket server = new LocalServerSocket("some name");
receiver = new LocalSocket();
receiver.connect(server.getLocalSocketAddress());
LocalSocket sender = server.accept();//LocalSocket not LocalServerSocket as in Source link
// ...
// Give the MediaRecorder our fake file
recorder.setOutputFile(sender.getFileDescriptor());
这段代码看起来像:
MediaRecorder
-->LocalSocket sender
-->LocalServerSocket server .accept()
-->LocalSocket receiver .connect()
怎么回事??
显然,sockets数据流的方向是相反的,因此,我不明白为什么这段代码按预期工作。
你能给我个建议吗?谢谢。
最佳答案
看来基本上,我的想法是正确的。
我看到的例子,至少,发送者-接收者结构是错误的。
下面是我制作的更简单的模型:
package com.example.app;
import android.app.Activity;
import android.media.MediaRecorder;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import java.io.IOException;
public class MainActivity extends Activity
{
private final String SOCKET_ADDRESS = "socket1";
private MediaRecorder mRecorder = new MediaRecorder();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class IOThread extends Thread
{
@Override
public void run()
{
System.out.println("--------IO Thread------ ");
LocalSocket receiver;
LocalServerSocket server;
try
{
server = new LocalServerSocket(SOCKET_ADDRESS);
localServerCreated();
receiver = server.accept();
System.out.println("---------server.accept();------------- ");
int ret = 0;
while ((ret = receiver.getInputStream().read()) != -1)
{
System.out.println( "ret =" + ret);
}
System.out.println("ret =" + ret);
}
catch (IOException e)
{
}
}
}
IOThread io1 = new IOThread();
io1.start();
}
private void localServerCreated()
{
LocalSocket sender;
try
{
sender = new LocalSocket();
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(sender.getFileDescriptor());
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("------mRecorder configured--------");
try
{
mRecorder.prepare();
mRecorder.start();
System.out.println("------mRecorder.start()--------");
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}