Problems with AsyncTask class and UDP communication - android

I am trying to send messages through the UDP communication of an android phone a UDP host, according to the buttons on the screen send a specific message, I have a UDP_Service class where I instantiate the datagramsocket and do the corresponding sending:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UDP_Service {
private int server_port;
DatagramSocket skt;
private InetAddress local;
private String msge;
private String st;
private int msge_lenght;
private byte[] byteMsge;
private byte[] resp;
private int j;
public UDP_Service(){
try {
skt = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
// st = null;
}
}
public void setIP(String ip){
try {
local = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
// st = null;
}
}
public void setPort(int np){
server_port = np;
}
public void setMsge(String msj){
msge = msj;
msge_lenght = msge.length();
byteMsge = msge.getBytes();
resp = new byte[1024];
}
public void Enviar(){
try {
skt = new DatagramSocket();
DatagramPacket pqtEnvio = new DatagramPacket(byteMsge, msge_lenght,
local, server_port);
skt.send(pqtEnvio);
DatagramPacket pqtResp = new DatagramPacket(resp, resp.length);
skt.receive(pqtResp);
st = new String(pqtResp.getData(),0,pqtResp.getLength());
//skt.close();
} catch (Exception e) {
// st = null;
}
}
public void close() {
skt.close();
}
public String getRespuesta(){
return st;
}
public int getCont() {
return j;
}
public void setCont(int x) {
j = x;
}
}
The method "Enviar()" I invoke it from an AsyncTask, however it only works once, I open the application from the phone and press any button and effectively the corresponding message is sent, but when I press another button no longer nothing happens. The onPostExecute () method of the AsyncTask is not running,I can not see the corresponding toast.
Here the AsyncTask
public class MainActivity extends AppCompatActivity {
String msje[];
String resp[];
int port;
UDP_Service UDP_Serv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UDP_Serv = new UDP_Service();
//UDP_A = new UDP_Async();
msje = new String[9];
resp = new String[8];
}
public void UDP_Client(String mje, int pt, String ip, int c)
{
//UDP_A = new UDP_Async();
UDP_Serv.setMsge(mje);
UDP_Serv.setPort(pt);
UDP_Serv.setIP(ip);
UDP_Serv.setCont(c);
new UDP_Async().execute();
Toast.makeText(MainActivity.this, mje, Toast.LENGTH_SHORT).show();
}
public class UDP_Async extends AsyncTask < Void, Void, Boolean > {
private String st;
private int j;
#Override
protected Boolean doInBackground(Void...params) {
UDP_Serv.Enviar();
st = UDP_Serv.getRespuesta();
j = UDP_Serv.getCont();
//return st;
return true;
}
#Override
protected void onPostExecute(Boolean result) {
//super.onPostExecute(result);
if (result) {
if (!st.equals(null)) {
resp[j] = st;
actualizarUI(j);
}
Toast.makeText(MainActivity.this, "TAREA FINALIZADA!", Toast.LENGTH_SHORT).show();
}
}
}
}

Related

Sent/Received messages gets repeated and order gets spoiled in peer-to-peer chat app in android using socket

I am developing a peer-to-peer chat app using socket programming in android. The port number of self and IP address and port number of the other peer is given as input and connection is established through socket. I have implemented chat activity using a list-view with custom adapter when the chatting starts, after exchanging some messages, the old message reappears in the list-view and also the order of the sent and the received messages gets spoiled.
I have printed the exchanged message in the logcat of android studio and seems that there is no issue. So what is the reason of the problem ? and What should be the solution?
Here is the Chat Activity
public class ChatActivity extends AppCompatActivity {
String ipAddress,portNo;
//public static String message="";
EditText messageTextView;
TextView responseTextView;
static MessageAdapter mAdapter;
ListView message_List;
ArrayList<Message> messageArray;
EditText portText;
int myport;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
Log.d("STATE",savedInstanceState.toString());
}
setContentView(R.layout.activity_chat);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String info = bundle.getString("ip&port");
String[] infos = info.split(" ");
ipAddress = infos[0];
portNo = infos[1];
myport = Integer.parseInt(infos[2]);
Log.d("info",ipAddress+" "+portNo+" "+myport);
}
message_List = (ListView) findViewById(R.id.message_list);
messageArray = new ArrayList<Message>();
mAdapter = new MessageAdapter(this, messageArray);
message_List.setAdapter(mAdapter);
messageTextView= (EditText) findViewById(R.id.messageEditText);
//message = messageTextView.getText().toString();
startServer();
}
void startServer(){
Server s = new Server(message_List, messageArray, myport);
s.start();
}
public void sendResponse(View view){
Client c =new Client();
c.execute();
}
public void setView(String s){
String str=responseTextView.getText().toString();
str=str+"\nReceived: "+s;
responseTextView.setText(str);
}
public class Client extends AsyncTask<Void,Void,String> {
String msg = messageTextView.getText().toString();;
#Override
protected String doInBackground(Void... voids) {
try {
String ipadd = ipAddress;
int portr = Integer.parseInt(portNo);
Socket clientSocket = new Socket(ipadd, portr);
OutputStream outToServer =clientSocket.getOutputStream();
PrintWriter output = new PrintWriter(outToServer);
output.println(msg);
output.flush();
clientSocket.close();
}
catch (Exception e) {
e.printStackTrace();
}
return msg;
}
protected void onPostExecute(String result) {
messageArray.add(new Message("Sent: " + result, 0));
message_List.setAdapter(mAdapter);
Log.d("problem","Sent: " + result);
for(Message mssg: messageArray){
String sst = mssg.getMessage();
//Log.d("problem"," "+sst);
}
messageTextView.setText("");
}
}
}
Here is the code of the server
public class Server extends Thread {
ListView messageList;
ArrayList<Message> messageArray;
int port;
public Server(ListView messageList, ArrayList<Message> messageArray, int
port) {
this.messageArray = messageArray;
this.messageList = messageList;
this.port = port;
}
ServerSocket welcomeSocket=null;
#Override
public void run(){
try{
String sentence;
welcomeSocket=new ServerSocket(port);
while (true){
Socket connectionSocket=welcomeSocket.accept();
HandleClient c= new HandleClient();
c.execute(connectionSocket);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public class HandleClient extends AsyncTask<Socket,Void,String>{
String sentence;
#Override
protected String doInBackground(Socket... sockets) {
try {
BufferedReader input = new BufferedReader(new
InputStreamReader(sockets[0].getInputStream()));
sentence = input.readLine();
}
catch(Exception e){
e.printStackTrace();
}
return sentence ;
}
protected void onPostExecute(String result) {
messageArray.add(new Message("Received: " + result, 1));
messageList.setAdapter(mAdapter);
Log.d("problem","Received: " + result);
for(Message mssg: messageArray){
String sst = mssg.getMessage();
//Log.d("problem"," "+sst);
}
}
}
}
Here is my message adapter class
public class MessageAdapter extends BaseAdapter{
Context context;
ArrayList<Message> arr = new ArrayList<>();
public MessageAdapter(Context context,ArrayList<Message> arr) {
this.context = context;
this.arr = arr;
for(Message mssg: arr){
String sst = mssg.getMessage();
Log.d("problem"," "+sst);
}
}
#Override
public int getCount() {
return arr.size();
}
#Override
public Object getItem(int i) {
return arr.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
//View listItemView=convertView;
if(convertView==null){
convertView=
LayoutInflater.from(context).inflate(R.layout.message_list,parent,false);
}
Message currentMessage= (Message) getItem(position);
String message=currentMessage.getMessage();
//Log.d("problem"," Current Message: " + message);
if(currentMessage.isSent()){
TextView sent=(TextView) convertView.findViewById(R.id.list_sent);
sent.setText(message);
sent.setVisibility(View.VISIBLE);
//sent.setVisibility(View.VISIBLE);
}
else{
TextView received= (TextView)
convertView.findViewById(R.id.list_received);
received.setText(message);
received.setVisibility(View.VISIBLE);
}
return convertView;
}
}

TCP/IP client receiving and sending between various Android activities

I need help to understand how to make a connection to a tcp client on Android with a server, the connection itself is not the problem, but rather the exchange between the activities.
I will try to explain with the attached image.
I need to start a connection to a server using TCP / IP sockets. After a search for the net I found several examples, but all using a single activity, but I need it to work as follows:
1 - Let's say in the main activity I start the connection by clicking on CONNECT.
2 - But then I need to click the ACTIVITY_A button to open another activity while keeping the connection that has already been opened in the main activity, and continue sending and receiving information in its ACTIVITY_A.
3 - Back to ACTIVITY_A, click on ACTIVITY_B doing the same process above.
I am lost between which solution to use and how to use, asynctask, thread, singleton, intent, context.
You can use Android Service for network connectivity. Also please look at Android Networking official doc. Also there are a lot library for performing network requests (like Robospice)
I edited the previous message to inform how I solved it, it may not be ideal but it is working.
Act_Main
public class Act_Main extends AppCompatActivity implements Singleton.OnReceiveListener{
private Singleton sing;
String ip = "192.168.4.1";
int porta = 23;
Button btConectar, btActivityA, btActivityB;
TextView txtStatus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
btConectar = (Button) findViewById(R.id.btConectarID);
btActivityA = (Button) findViewById(R.id.btActivityAID);
btActivityB = (Button) findViewById(R.id.btActivityBID);
txtStatus = (TextView) findViewById(R.id.txtStatusID);
}
public void conectar (View view){
sing = Singleton.getInstance(ip, porta, this);
}
public void openActivityA(View view) {
Intent it = new Intent(Act_Main.this, Activity_A.class);
startActivity(it);
}
public void openActivityB(View view) {
Intent it = new Intent(Act_Main.this, Activity_B.class);
startActivity(it);
}
#Override
public void onReceive(String dataRx) {
// Trata a informação recebida aqui.
txtStatus.setText(dataRx);
}
}
Singleton
public class Singleton {
private static Singleton instancia = null;
private static OnReceiveListener orl = null;
private boolean running;
private static Client client;
private Singleton() {
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public static interface OnReceiveListener {
public void onReceive(String dataRx);
}
public static Singleton getInstance(String _ip, int _port, OnReceiveListener listener) {
if (instancia == null) {
client = new Client(_ip, _port);
client.execute();
instancia = new Singleton();
}
orl = listener;
return instancia;
}
public void sendMsg(String str) {
client.sendMessage(str);
}
private static class Client extends AsyncTask<Void, String, Void> {
String dstAddress;
int dstPort;
String response = "";
BufferedReader in;
PrintWriter out;
String incomingMessage;
private boolean running;
Client(String addr, int port) {
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
running = true;
// Cria um objeto PrintWriter para enviar mensagens ao servidor.
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
// Cria um objeto BufferedReader para receber mensagens do servidor.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d(TAG, "In/Out created");
while (running) {
incomingMessage = in.readLine();
if (incomingMessage != null) {
publishProgress(incomingMessage);
}else{
running = false;
}
incomingMessage = null;
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onProgressUpdate(String... params) {
orl.onReceive(params[0]);
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
private void sendMessage(String message) {
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
Log.d(TAG, "Sent Message: " + message);
}
}
}
}
Activity_A
public class Activity_A extends AppCompatActivity implements Singleton.OnReceiveListener {
private Singleton sing;
String ip = "192.168.4.1";
int porta = 23;
Button btVoltar, btEnviar;
TextView txtRx, txtTx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
btEnviar = (Button) findViewById(R.id.btEnviarID);
btVoltar = (Button) findViewById(R.id.btVoltarID);
txtRx = (TextView) findViewById(R.id.txtRxID);
txtTx = (TextView) findViewById(R.id.txtTxID);
sing = Singleton.getInstance(ip, porta, this);
}
#Override
public void onReceive(String dataRx) {
txtRx.setText(dataRx);
}
public void Enviar (View view){
sing.sendMsg(txtTx.getText().toString());
}
public void Voltar(View view) {
this.finish();
}
}
Activity_B
public class Activity_B extends AppCompatActivity implements Singleton.OnReceiveListener {
private Singleton sing;
String ip = "192.168.4.1";
int porta = 23;
Button btVoltar, btEnviar;
TextView txtRx, txtTx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
btEnviar = (Button) findViewById(R.id.btEnviarID);
btVoltar = (Button) findViewById(R.id.btVoltarID);
txtRx = (TextView) findViewById(R.id.txtRxID);
txtTx = (TextView) findViewById(R.id.txtTxID);
sing = Singleton.getInstance(ip, porta, this);
}
#Override
public void onReceive(String dataRx) {
txtRx.setText(dataRx);
}
public void Enviar (View view){
sing.sendMsg(txtTx.getText().toString());
}
public void Voltar(View view) {
this.finish();
}
}
Evidently it is not finished, but it is a beginning.
Thank you to those who have responded.

Loading many asset effectively into android project

I have 231 sound files ( duration ~ 0.2 Sec each) of size 5.7 MB total to load into my android project. I am trying load them when the application starts using for loop like
for (int i = 0; i < 231; i++){
...
loadSoundAsset(i); //method to load the sound files
i++;
...
}
Yet the above method is taking too long to load the sound files. What should be done to load effectively many asset files into android project?
I create sample code for you. How to get faster? (I test it for assets files about 180 sounds files.)
MainActivity
public class MainActivity extends Activity implements TaskListener {
MultiLoader loader = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText("Loader");
setContentView(view);
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
loader = new MultiLoader(this, this);
loader.load("sound");
}
#Override
public void onTaskEnd() {
Vector<byte[]> soundDatas = loader.getData();
Log.e("MainActivity", "TaskEnd");
}
#Override
protected void onDestroy() {
loader.clear();
super.onDestroy();
}
}
MultiLoader
package com.fastload;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.regex.Pattern;
import android.content.Context;
import android.util.Log;
public class MultiLoader {
private int threadCount = 0;
private String[] mFiles;
private Vector<byte[]> fileContents = new Vector<byte[]>();
private Thread[] mQueue = null;
private Context mContext;
private TaskListener listener;
public MultiLoader(Context mContext, TaskListener listener) {
super();
this.mContext = mContext;
this.listener = listener;
}
public Vector<byte[]> getData(){
return fileContents;
}
public void reQueue(int index){
boolean status = true;
mQueue[index] = null;
for(Thread item : mQueue){
status &= (item == null);
}
if(status){
listener.onTaskEnd();
}
}
public void load(final String path){
initialize(path);
if(mFiles == null || (mFiles != null && mFiles.length < 1))
return;
mQueue = new Thread[threadCount];
for(int i = 0; i < threadCount; ++i){
int len = mFiles.length;
int piece = len / threadCount;
final int startIndex = i * piece;
final int endIndex = (i == threadCount - 1) ? len - startIndex - 1 : startIndex + piece;
MyTask task = new MyTask("MyTask##"+i, i, new EndListener(){
#Override
public void onEnd(int index, String name) {
Log.e("ThreadEND", "name = "+name);
reQueue(index);
}
}) {
#Override
public void execute() {
for(int index = startIndex; index < endIndex; ++index){
File file = new File(mFiles[index]);
InputStream is = null;
ByteArrayOutputStream os = null;
byte[] data = null;
try {
is = mContext.getAssets().open(path + File.separator + file.getName());
os = new ByteArrayOutputStream();
int count = 0;
byte[] buffer = new byte[1024];
while((count = is.read(buffer)) > 0){
os.write(buffer, 0, count);
}
os.flush();
data = os.toByteArray();
debug(getName(), index, path + File.separator + file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(data != null){
add(data);
}
}
}
};
mQueue[i] = task;
task.start();
}
}
private void debug(String who, int index, String name){
Log.e("MULTI LOADER DEBUG", "who = "+who+" , name = "+name+", index = "+index);
}
private void initialize(String path){
threadCount = getNumCores() * 2;
try {
mFiles = mContext.getAssets().list(path);
} catch (IOException e) {
e.printStackTrace();
}
}
private void add(byte[] data){
synchronized (fileContents) {
fileContents.add(data);
}
}
private void remove(byte[] data){
synchronized (fileContents) {
fileContents.remove(data);
}
}
public void clear(){
synchronized (fileContents) {
fileContents.clear();
}
}
private int getNumCores() {
class CpuFilter implements FileFilter {
#Override
public boolean accept(File pathname) {
if(Pattern.matches("cpu[0-9]+", pathname.getName())) {
return true;
}
return false;
}
}
try {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new CpuFilter());
return files.length;
} catch(Exception e) {
return 1;
}
}
private abstract class MyTask extends Thread{
private EndListener listener;
private int index;
private MyTask() { }
public MyTask(String threadName, int index, EndListener listener) {
super(threadName);
this.index = index;
this.listener = listener;
}
public abstract void execute();
#Override
public void run() {
execute();
end();
}
public void end(){
listener.onEnd(index, getName());
}
public int getIndex(){
return index;
}
}
public interface TaskListener{
public void onTaskEnd();
}
public interface EndListener{
public void onEnd(int index, String name);
}
}

WebRTC Android Library function not getting called

I am using the following open-source webrtc android application:
https://github.com/pchab/AndroidRTC
I have just modified this application to use my socket.io server instead of using the following one which is given by same author:
https://github.com/pchab/ProjectRTC
To do this, I needed to do some changes in the two classes of the above AndroidRTC Application. After this, when I started the application it did not call the 'createOffer()' or 'createAnswer()' function which is part of libjingle_peerconnection library. I am confused whether these two functions are not getting called or they are not able to use 'sendMessage()' function.
From debugging, I came to know that line which calls 'createAnswer()' function is successfully reached. After this, I expect the 'createAnswer()' function to use my 'sendMessage()' function to send the answer back to other party by using my socket.io server. I am not able to peek inside this 'createAnswer()' function as it is part of the library.
Before changing the above application to use my own server, I had tested it with the server given by auhtor. It ran successfully. I don't know what is wrong when I use my own server to make calls and do handshaking. I just modified few lines to support the way I do signalling on the server.
My server code is already used for webrtc web application. Web Applications are successful in making calls using this server. It should work for this android application too with little modification on the application.
I modified the following two classes in android application:
RTCActivity.java
package fr.pchab.AndroidRTC;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import org.json.JSONException;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.VideoRenderer;
import java.util.List;
public class RTCActivity extends Activity implements WebRtcClient.RTCListener{
private final static int VIDEO_CALL_SENT = 666;
private VideoStreamsView vsv;
private WebRtcClient client;
private String mSocketAddress;
private String callerId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mSocketAddress = "https://" + getResources().getString(R.string.host);
mSocketAddress += (":"+getResources().getString(R.string.port)+"/");
PeerConnectionFactory.initializeAndroidGlobals(this);
// Camera display view
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize);
vsv = new VideoStreamsView(this, displaySize);
client = new WebRtcClient(this, mSocketAddress);
final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
final List<String> segments = intent.getData().getPathSegments();
callerId = segments.get(0);
}
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
#Override
public void onPause() {
super.onPause();
vsv.onPause();
}
#Override
public void onResume() {
super.onResume();
vsv.onResume();
}
#Override
public void onCallReady(String callId) {
startCam();
}
public void answer(String callerId) throws JSONException {
client.sendMessage(callerId, "init", null);
startCam();
}
public void call(String callId) {
Intent msg = new Intent(Intent.ACTION_SEND);
msg.putExtra(Intent.EXTRA_TEXT, mSocketAddress + callId);
msg.setType("text/plain");
startActivityForResult(Intent.createChooser(msg, "Call someone :"), VIDEO_CALL_SENT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == VIDEO_CALL_SENT) {
startCam();
}
}
public void startCam() {
setContentView(vsv);
// Camera settings
client.setCamera("front", "640", "480");
client.start("android_test", true);
}
#Override
public void onStatusChanged(final String newStatus) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), newStatus, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onLocalStream(MediaStream localStream) {
localStream.videoTracks.get(0).addRenderer(new VideoRenderer(new VideoCallbacks(vsv, 0)));
}
#Override
public void onAddRemoteStream(MediaStream remoteStream, int endPoint) {
remoteStream.videoTracks.get(0).addRenderer(new VideoRenderer(new VideoCallbacks(vsv, endPoint)));
vsv.shouldDraw[endPoint] = true;
}
#Override
public void onRemoveRemoteStream(MediaStream remoteStream, int endPoint) {
remoteStream.videoTracks.get(0).dispose();
vsv.shouldDraw[endPoint] = false;
}
// Implementation detail: bridge the VideoRenderer.Callbacks interface to the
// VideoStreamsView implementation.
private class VideoCallbacks implements VideoRenderer.Callbacks {
private final VideoStreamsView view;
private final int stream;
public VideoCallbacks(VideoStreamsView view, int stream) {
this.view = view;
this.stream = stream;
}
#Override
public void setSize(final int width, final int height) {
view.queueEvent(new Runnable() {
public void run() {
view.setSize(stream, width, height);
}
});
}
#Override
public void renderFrame(VideoRenderer.I420Frame frame) {
view.queueFrame(stream, frame);
}
}
}
WebRTCClient.java
package fr.pchab.AndroidRTC;
import java.util.HashMap;
import java.util.LinkedList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.DataChannel;
import org.webrtc.IceCandidate;
import org.webrtc.MediaConstraints;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SdpObserver;
import org.webrtc.SessionDescription;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoSource;
import android.os.Handler;
import android.util.Log;
import com.koushikdutta.async.http.socketio.Acknowledge;
import com.koushikdutta.async.http.socketio.ConnectCallback;
import com.koushikdutta.async.http.socketio.EventCallback;
import com.koushikdutta.async.http.socketio.SocketIOClient;
class WebRtcClient {
private final static int MAX_PEER = 2;
private boolean[] endPoints = new boolean[MAX_PEER];
private PeerConnectionFactory factory;
private HashMap<String, Peer> peers = new HashMap<String, Peer>();
private LinkedList<PeerConnection.IceServer> iceServers = new LinkedList<PeerConnection.IceServer>();
private MediaConstraints pcConstraints = new MediaConstraints();
private MediaStream lMS;
private RTCListener mListener;
private SocketIOClient client;
private final MessageHandler messageHandler = new MessageHandler();
private final static String TAG = WebRtcClient.class.getCanonicalName();
public interface RTCListener{
void onCallReady(String callId);
void onStatusChanged(String newStatus);
void onLocalStream(MediaStream localStream);
void onAddRemoteStream(MediaStream remoteStream, int endPoint);
void onRemoveRemoteStream(MediaStream remoteStream, int endPoint);
}
private interface Command{
void execute(String peerId, JSONObject payload) throws JSONException;
}
private class CreateOfferCommand implements Command{
public void execute(String peerId, JSONObject payload) throws JSONException {
Log.d(TAG,"CreateOfferCommand");
Peer peer = peers.get(peerId);
peer.pc.createOffer(peer, pcConstraints);
}
}
private class CreateAnswerCommand implements Command{
public void execute(String peerId, JSONObject payload) throws JSONException {
Log.d(TAG,"CreateAnswerCommand");
Peer peer = peers.get(peerId);
SessionDescription sdp = new SessionDescription(
SessionDescription.Type.fromCanonicalForm(payload.getString("type")),
payload.getString("sdp")
);
peer.pc.setRemoteDescription(peer, sdp);
peer.pc.createAnswer(peer, pcConstraints);
}
}
private class SetRemoteSDPCommand implements Command{
public void execute(String peerId, JSONObject payload) throws JSONException {
Log.d(TAG,"SetRemoteSDPCommand");
Peer peer = peers.get(peerId);
SessionDescription sdp = new SessionDescription(
SessionDescription.Type.fromCanonicalForm(payload.getString("type")),
payload.getString("sdp")
);
peer.pc.setRemoteDescription(peer, sdp);
}
}
private class AddIceCandidateCommand implements Command{
public void execute(String peerId, JSONObject payload) throws JSONException {
Log.d(TAG,"AddIceCandidateCommand");
PeerConnection pc = peers.get(peerId).pc;
if (pc.getRemoteDescription() != null) {
IceCandidate candidate = new IceCandidate(
payload.getString("id"),
payload.getInt("label"),
payload.getString("candidate")
);
pc.addIceCandidate(candidate);
}
}
}
public void sendMessage(String to, String type, JSONObject payload) throws JSONException {
JSONObject message = new JSONObject();
//message.put("room", to);
message.put("type", type);
message.put("msg", payload);
message.put("room", "sojharo");
client.emit("message", new JSONArray().put(message));
}
private class MessageHandler implements EventCallback {
private HashMap<String, Command> commandMap;
public MessageHandler() {
this.commandMap = new HashMap<String, Command>();
commandMap.put("init", new CreateOfferCommand());
commandMap.put("offer", new CreateAnswerCommand());
commandMap.put("answer", new SetRemoteSDPCommand());
commandMap.put("candidate", new AddIceCandidateCommand());
}
#Override
public void onEvent(String s, JSONArray jsonArray, Acknowledge acknowledge) {
try {
Log.d(TAG,"MessageHandler.onEvent() "+ (s == null ? "nil" : s));
if(s.equals("id")) {
JSONObject message = new JSONObject();
message.put("room", "sojharo");
message.put("username", "android");
client.emit("create or join livehelp",
new JSONArray().put(message));
} else if (s.equals("joined")) {
mListener.onCallReady("Not Initiator");
} else {
JSONObject json = jsonArray.getJSONObject(0);
try{
if(json.getString("msg").equals("got user media"))
return ;
}catch(JSONException e){}
String from = json.getString("from");
String type = null;
try{
type = json.getString("type");
}catch(JSONException e){}
// if peer is unknown, try to add him
if(!peers.containsKey(from)) {
// if MAX_PEER is reach, ignore the call
int endPoint = findEndPoint();
if(endPoint != MAX_PEER) {
addPeer(from, endPoint);
commandMap.get(type).execute(from, json);
}
} else {
commandMap.get(type).execute(from, json);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class Peer implements SdpObserver, PeerConnection.Observer{
private PeerConnection pc;
private String id;
private int endPoint;
#Override
public void onCreateSuccess(final SessionDescription sdp) {
try {
JSONObject payload = new JSONObject();
payload.put("type", sdp.type.canonicalForm());
payload.put("sdp", sdp.description);
sendMessage(id, sdp.type.canonicalForm(), payload);
pc.setLocalDescription(Peer.this, sdp);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onSetSuccess() {}
#Override
public void onCreateFailure(String s) {}
#Override
public void onSetFailure(String s) {}
#Override
public void onSignalingChange(PeerConnection.SignalingState signalingState) {}
#Override
public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
if(iceConnectionState == PeerConnection.IceConnectionState.DISCONNECTED) {
removePeer(id);
mListener.onStatusChanged("DISCONNECTED");
}
}
#Override
public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {}
#Override
public void onIceCandidate(final IceCandidate candidate) {
try {
JSONObject payload = new JSONObject();
payload.put("label", candidate.sdpMLineIndex);
payload.put("id", candidate.sdpMid);
payload.put("candidate", candidate.sdp);
sendMessage(id, "candidate", payload);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onError() {}
#Override
public void onAddStream(MediaStream mediaStream) {
Log.d(TAG,"onAddStream "+mediaStream.label());
// remote streams are displayed from 1 to MAX_PEER (0 is localStream)
mListener.onAddRemoteStream(mediaStream, endPoint+1);
}
#Override
public void onRemoveStream(MediaStream mediaStream) {
mListener.onRemoveRemoteStream(mediaStream, endPoint);
removePeer(id);
}
#Override
public void onDataChannel(DataChannel dataChannel) {}
public Peer(String id, int endPoint) {
Log.d(TAG,"new Peer: "+id + " " + endPoint);
this.pc = factory.createPeerConnection(iceServers, pcConstraints, this);
this.id = id;
this.endPoint = endPoint;
pc.addStream(lMS, new MediaConstraints());
mListener.onStatusChanged("CONNECTING");
}
}
public WebRtcClient(RTCListener listener, String host) {
mListener = listener;
factory = new PeerConnectionFactory();
SocketIOClient.connect(host, new ConnectCallback() {
#Override
public void onConnectCompleted(Exception ex, SocketIOClient socket) {
if (ex != null) {
Log.e(TAG,"WebRtcClient connect failed: "+ex.getMessage());
return;
}
Log.d(TAG,"WebRtcClient connected.");
client = socket;
// specify which events you are interested in receiving
client.addListener("id", messageHandler);
client.addListener("message", messageHandler);
client.addListener("joined", messageHandler);
}
}, new Handler());
iceServers.add(new PeerConnection.IceServer("stun:23.21.150.121"));
iceServers.add(new PeerConnection.IceServer("stun:stun.l.google.com:19302"));
pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
}
public void setCamera(String cameraFacing, String height, String width){
MediaConstraints videoConstraints = new MediaConstraints();
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", height));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", width));
VideoSource videoSource = factory.createVideoSource(getVideoCapturer(cameraFacing), videoConstraints);
lMS = factory.createLocalMediaStream("ARDAMS");
lMS.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));
lMS.addTrack(factory.createAudioTrack("ARDAMSa0"));
mListener.onLocalStream(lMS);
}
private int findEndPoint() {
for(int i = 0; i < MAX_PEER; i++) {
if(!endPoints[i]) return i;
}
return MAX_PEER;
}
public void start(String name, boolean privacy){
try {
JSONObject message = new JSONObject();
message.put("msg", new JSONObject().put("msg", "got user media"));
message.put("room", "sojharo");
client.emit("message", new JSONArray().put(message));
} catch (JSONException e) {
e.printStackTrace();
}
}
/*
Cycle through likely device names for the camera and return the first
capturer that works, or crash if none do.
*/
private VideoCapturer getVideoCapturer(String cameraFacing) {
int[] cameraIndex = { 0, 1 };
int[] cameraOrientation = { 0, 90, 180, 270 };
for (int index : cameraIndex) {
for (int orientation : cameraOrientation) {
String name = "Camera " + index + ", Facing " + cameraFacing +
", Orientation " + orientation;
VideoCapturer capturer = VideoCapturer.create(name);
if (capturer != null) {
return capturer;
}
}
}
throw new RuntimeException("Failed to open capturer");
}
private void addPeer(String id, int endPoint) {
Peer peer = new Peer(id, endPoint);
peers.put(id, peer);
endPoints[endPoint] = true;
}
private void removePeer(String id) {
Peer peer = peers.get(id);
peer.pc.close();
peer.pc.dispose();
peers.remove(peer.id);
endPoints[peer.endPoint] = false;
}
}
The code is able to receive the offer and candidates from other party. It is not able to send the answer or candidates to that party in return.
I have not modified other two classes which can be found on the above link for android application.
Here is snippet of my socket.io server code written in nodejs:
socket.on('create or join livehelp', function (room) {
var numClients = socketio.sockets.clients(room.room).length;
if (numClients === 0){
socket.join(room.room);
socket.set('nickname', room.username);
socket.emit('created', room);
} else if (numClients < 2) {
socket.join(room.room);
socket.set('nickname', room.username);
socket.emit('joined', room);
socket.broadcast.to(room.room).emit('join', room);
} else { // max three clients
socket.emit('full', room.room);
}
console.log(socketio.sockets.manager.rooms)
console.log(room)
});
socket.on('message', function (message) {
//console.log('Got message:', message);
//socket.broadcast.emit('message', message);
message.msg.from = socket.id;
//socketio.sockets.in(message.room).emit('message', message.msg);
socket.broadcast.to(message.room).emit('message', message.msg);
//console.log('Got message:', message.msg);
//console.log(socketio.sockets.manager.rooms)
});
I am confused if there is any error why I am not able to find it in debugging. Log for this is very difficult to read as it runs very fast and I am not able to catch each and every line. But apparently, it looked fine at a glance.
Please help. Thanks.
I think you are not able to generate answer but you are able to generate offer?. If this is the case try adding
pcConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
to your pc constraints.
Hope this will help..
client.on('message', function (details) {
console.log('message',details.to);
console.log(details.type);
if(details.type !== 'init'){
var otherClient = io.sockets.connected[details.to];
if (!otherClient) {
return;
}
delete details.to;
details.from = client.id;
otherClient.emit('message', details);
}
else
{
if (io.sockets.adapter.rooms[client.room] !== undefined ) {
for(var member in io.sockets.adapter.rooms[client.room]){
console.log(member);
if(member !== client.id){
var otherClient = io.sockets.connected[member];
if (!otherClient) {
return;
}
delete details.to;
details.from = client.id;
otherClient.emit('message', details);
}
else{
console.log("no need to send self again!");
}
}
} else {
client.emit("update", "Please connect to a room.");
}
}
});
Please download latest libjingle from here
http://repo.spring.io/libs-release-remote/io/pristine/libjingle/

How to pass asynctask to another activity in android?

I'm try to writing an online game with a socket connection.
So I use asynctask to make a socket connection.
SocketServer.java
public class SocketServer{
private MyCustomListener listener;
private String ip = "127.0.0.1";
private int port = 4444;
#SuppressWarnings("unused")
private Context context;
private SocketAsync socketAsync;
private String dataInput, username;
public SocketServer(Context context) {
this.context = context;
}
public void setOnRecieveMsgListener(MyCustomListener listener) {
this.listener = listener;
}
public void connect() {
socketAsync = new SocketAsync();
socketAsync.execute();
}
public void sentData(String x, String y, String z) {
dataInput = null;
JSONObject object = new JSONObject();
// JSON Encode
socketAsync.sentJSON(object);
}
private class SocketAsync extends AsyncTask<Void, Void, String> {
private Socket socket;
private PrintWriter printWriter;
#Override
protected String doInBackground(Void... params) {
try {
socket = new Socket(InetAddress.getByName(ip),port);
OutputStreamWriter streamOut = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
printWriter = new PrintWriter(streamOut);
streamOut.flush();
BufferedReader streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
Looper.prepare();
while(socket.isConnected()) {
try {
dataInput = streamIn.readLine();
listener.onRecieveMessage(new MyListener(dataInput));
}
catch(Exception e) {}
}
Looper.loop();
}
catch(Exception e) {}
return null;
}
public void sentJSON(JSONObject object) {
if(socket.isConnected()) {
try {
printWriter.println(object.toString());
printWriter.flush();
}
catch(Exception e) {}
}
}
}
}
Login.class
public class Login extends Activity implements MyCustomListener {
JSONObject object;
SocketServer socketserver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
socketserver = new SocketServer(this);
socketserver.setOnRecieveMsgListener(this);
socketserver.connect();
button();
}
private void button() {
Button loginBt = (Button)findViewById(R.id.login_bt);
final EditText un = (EditText)findViewById(R.id.username);
final EditText ps = (EditText)findViewById(R.id.password);
final String[] logindata = new String[2];
loginBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
logindata[0] = un.getText().toString();
logindata[1] = ps.getText().toString();
socketserver.setUsername(logindata[0]);
socketserver.sentData("SERVER", "TEST", "login");
}
});
}
private void toMainScreen() {
Intent x = new Intent(this,Main.class);
startActivity(x);
}
#Override
public void onRecieveMessage(MyListener ml) {
try {
JSONObject json = new JSONObject(ml.getMsgStr());
System.out.println(json.getString("content"));
if(json.getString("content").equals("TRUE")) {
toMainScreen();
}
else
Toast.makeText(getApplicationContext(), "Login Fail", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e("## JSON DECODE", e.toString());
e.printStackTrace();
}
}
}
Main.class
public class Main extends Activity implements MyCustomListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//do some thing
}
#Override
public void onRecieveMessage(MyListener ml) {
System.out.println("MAIN : " + ml.getMsgStr());
}
}
so how can I pass object "socketserver" from login class to main class?
or is there an other way to do something like this?
sorry for my poor english.
You should not try to pass an instance of SocketServer around. One of it's properties is context which means you should not used it outside the original context it was created in (i.e. activity it was created in) or you'll have memory leaks.
Your SocketServer class needs IP and port. This is the kind of information that you should pass between activities and then use that to create another instance of your SocketServer class.

Categories

Resources