a LocalSocket (Unix domain) client-server data flo

2019-05-28 19:04发布

What I don't get is data flow among the Unix Domain Sockets .

I understand Unix Domain Socket data flow is

LocalSocket client .connect() --> LocalServerSocket server .accept()

The client sends data to the server, straight enough that I understand.

However, for Streaming video/audio from MediaRecorder of Android, after a lot of research, I've seen every example to use a LocalSocket instead of a file for MediaRecorder is in principle as below:

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());

This code looks like:

MediaRecorder --> LocalSocket sender -->

LocalServerSocket server .accept() --> LocalSocket receiver .connect()

What is going on??

Appearently, the sockets data flows in the opposite direction, therefore, I don't get why this code works as expected.

Could you kindly advise. Thanks.

1条回答
Melony?
2楼-- · 2019-05-28 19:13

It appears that basically, my thought is correct. The examples I've seen, at least, sender-receiver structure is something wrong.

Here's more straightforward model I made:

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();
        }

    }


}
查看更多
登录 后发表回答