2014-01-12 65 views
2

我沒有得到的是Unix域套接字之間的數據流。Android(Java)中的MediaRecorder的LocalSocket(Unix域)客戶端 - 服務器數據流問題

我明白Unix域套接字的數據流是

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

客戶端發送數據到服務器,不夠直,我明白了。

然而,對於視頻流/於Android的MediaRecorder音頻,大量的研究後,我已經看到了每一個例子來使用,而不是爲MediaRecorder文件的LocalSocket在原理如下:

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

這是怎麼回事?顯然,套接字數據流向相反的方向,因此,我不明白爲什麼此代碼按預期工作。

您能否提供意見。謝謝。

回答

4

看來,基本上,我的想法是正確的。 我見過的例子,至少發送者 - 接收者結構是錯誤的。

這裏有更直接的模型,我提出:

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

    } 


}