I am trying to develop a peer to peer application which will stream live video between the clients. The clients will be joining in and will then be able to stream.
The below code does not crash but it does not allow the streaming. Please help me as i would like to know what is the problem.
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();
}
}
As written, i am trying to open up a socket which reads from the input stream. I am trying to write this input stream to a file (which is being created as 0 bytes!!) but nothing in it. I am trying t use this existing stream this part to another client.
Please help