点对点视频流

时间:2012-01-13 22:55:01

标签: video-streaming p2p live-video

我正在尝试开发一个点对点应用程序,它将在客户端之间传输实时视频。客户将加入,然后将能够流式传输。

以下代码不会崩溃,但不允许流式传输。请帮助我,因为我想知道这是什么问题。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.Buffer;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;

public class StreamingclienttestActivity extends Activity {
    /** Called when the activity is first created. */
    StreamingMediaPlayer smp;
    boolean paused = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Add Activity as callback for SurfaceView
        final VideoView vv = (VideoView) findViewById(R.id.videoView1);

        try {
            //The IP of the streaming server and the port number
            Socket sock = new Socket("10.68.50.63",3333);

            //Get the input stream from the socket
            final InputStream is = sock.getInputStream();
            if (is == null)
                throw new RuntimeException("stream is null");
            final File temp = File.createTempFile("mediaplayertmp", "dat",new File("/sdcard/"));
            String tempPath = temp.getAbsolutePath();
           final FileOutputStream out = null; 
            new Thread(new Runnable() {
                public void run() { 


            byte buf[] = new byte[1024];
            do {
                int numread = -1;
                try {
                    numread = is.read(buf);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (numread <= 0)
                    break;
                try {
                    out.write(buf, 0, numread);
                    out.equals(temp) ;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                 } while (true);


            //Start the StreamingMediaPlayer with the input stream
            smp = new StreamingMediaPlayer("temp",vv);


            //Trigger playback
            smp.play();
            } 

            }
                    ).start();
        }
        catch (Exception e) {
            Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
            e.printStackTrace();
        }
        new Thread(new Runnable() {
        public void run() { 
            try {
            ServerSocket ssock = new ServerSocket(3333);

            //Log.w(LOGTAG, "Waiting for connection");
            Socket s = ssock.accept();

            BufferedOutputStream outStream = new BufferedOutputStream(s.getOutputStream());

            FileInputStream fileInput = new FileInputStream(new File("sdcard/temp"));

            //Log.w(LOGTAG, "Start sending data");

            byte[] buff = new byte[1024];
            int read = -1;
            while ((read = fileInput.read(buff)) != -1) {
                outStream.write(buff, 0, read);
            }

            outStream.flush();
            outStream.close();

            //Log.w(LOGTAG, "Closed connection");
            } catch (Exception e) {
                Log.w("StreamingcliettestActivity", "Exception while instantiating smp.");
                e.printStackTrace();
            }


        }


    }).start();
    }


//  public class StreamingclientSERVERtestActivity extends StreamingclienttestActivity {
//      private static final String LOGTAG = "StreamingClientServer";
//      public void onCreate(Bundle savedInstanceState) {
//          super.onCreate(savedInstanceState);
//          setContentView(R.layout.main);




    @Override 
    public void onConfigurationChanged(Configuration newCfg) { 
        //ignore orientation change 
        super.onConfigurationChanged(newCfg); 
    } 

    @Override
    public void onDestroy() {
        smp.close();
        super.onDestroy();
    }
}

如上所述,我试图打开一个从输入流中读取的套接字。我正在尝试将此输入流写入文件(正在创建为0字节!!)但其中没有任何内容。我正在尝试将此部分的现有流用于另一个客户端。

请帮忙

0 个答案:

没有答案