Я пытаюсь разработать приложение для сервера Android, которое получает живое видео с камеры клиента Android.Я нашел этот проект на GitHub: https://github.com/Lucklyric/android-camera-socket-stream. Мне нужно инвертировать клиент и сервер (клиент передает свою камеру на сервер).Это для сервера:
public class MyServerThread implements Runnable {
private int mServerPort;
private String mServerIP;
private Handler mHandler;
private Context mContext;
private VideoActivity mActivityInstance;
String message;
private BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
public MyServerThread (Context context,String serverip,int serverport,Handler handler) {
super();
mContext=context;
mServerIP = serverip;
mServerPort = serverport;
this.mHandler = handler;
bitmap_options.inPreferredConfig = Bitmap.Config.RGB_565;
mActivityInstance = (VideoActivity) mContext;
}
public void run(){
try {
ServerSocket ss = new ServerSocket(mServerPort);
mHandler.post(new Runnable() {
@Override
public void run() {
mActivityInstance.serverStatus.setText("Listening on IP: " + mServerIP);
}
});
while (true){
final Socket s = ss.accept();
mHandler.post(new Runnable() {
@Override
public void run() {
mActivityInstance.serverStatus.setText("Video from: " + s.getInetAddress());
}
});
new Thread(new ServerSocketThread(s)).start();
}
}catch(Exception e){
Log.d("ServerThread", "run: erro");
}
}
public class ServerSocketThread implements Runnable{
Socket s = null;
InputStream is = null;
public ServerSocketThread(Socket s) throws IOException {
this.s = s;
}
@Override
public void run() {
try {
InputStream inStream = null;
try {
inStream = s.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
DataInputStream is = new DataInputStream(inStream);
while (true) {
try {
int token = is.readInt();
if (token == 4) {
if (is.readUTF().equals("#@@#")) {
//System.out.println("before-token" + token);
int imgLength = is.readInt();
System.out.println("getLength:" + imgLength);
System.out.println("back-token" + is.readUTF());
byte[] buffer = new byte[imgLength];
int len = 0;
while (len < imgLength) {
len += is.read(buffer, len, imgLength - len);
}
Message m = mHandler.obtainMessage();
m.obj = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,bitmap_options);
if (m.obj != null) {
mHandler.sendMessage(m);
} else {
System.out.println("Decode Failed");
}
}
}else{
Log.d("MyServerThread","Skip Dirty bytes!!!!"+Integer.toString(token));
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Это для клиента:
public class MyClientThread implements Runnable{
private Handler mHandler;
Socket s;
private Context mContext;
private VideoActivity mActivityInstance;
public MyClientThread(Context context , Socket s, Handler handler){
mContext=context;
this.s = s;
this.mHandler = handler;
mActivityInstance = (VideoActivity) mContext;
}
@Override
public void run() {
OutputStream os = null;
try {
s.setKeepAlive(true);
os = s.getOutputStream();
while(true){
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(4);
dos.writeUTF("#@@#");
dos.writeInt(mActivityInstance.mPreview.mFrameBuffer.size());
dos.writeUTF("-@@-");
dos.flush();
System.out.println(mActivityInstance.mPreview.mFrameBuffer.size());
dos.write(mActivityInstance.mPreview.mFrameBuffer.toByteArray());
dos.flush();
Thread.sleep(1000/15);
}
} catch (Exception e) {
e.printStackTrace();
try {
if (os!= null)
os.close();
} catch (Exception e2) {
e.printStackTrace();
}
}
}
}
Сервер не загружает видео иЯ не знаю почему ..