I am using the SinchClient for Voip integration in my audio device. I have Created the VOIPClient.Java is like below.
public class VOIPClient {
private static final String TAG = "VOIPClient";
private SinchClient mSinch = null;
private TTSHelper mTTS;
private Call mCurrentCall;
private BootService mContext;
private NsdController mNsdController;
private final CallListener mCallListener = new CallListener() {
@Override
public void onCallProgressing(Call call) {
Log.d(TAG, "Call established at " + " Thusee");
}
@Override
public void onCallEstablished(Call call) {
Log.d(TAG, "Call established at " + call.getDetails().getEstablishedTime());
mTTS.speak("Call started", TTSHelper.UTTERANCE_VOIP_START);
JsonObject payload = new JsonObject();
payload.addProperty("Status", 0);
payload.addProperty("Call_status", 1);
if (mNsdController != null) {
mNsdController.sendCommand(20, payload);
}
}
@Override
public void onCallEnded(Call call) {
Log.d(TAG, "Call ended at " + call.getDetails().getEndedTime() + "caused by " + call.getDetails().getEndCause().toString());
//TODO : null object reference hangup
if (mCurrentCall != null) {
mTTS.speak("Call ended", TTSHelper.UTTERANCE_VOIP_END);
mCurrentCall.hangup();
return;
}
// mContext.getAlexa().start();
JsonObject payload = new JsonObject();
payload.addProperty("Status", 0);
payload.addProperty("Call_status", 1);
if (mNsdController != null) {
mNsdController.sendCommand(21, payload);
}
}
@Override
public void onShouldSendPushNotification(Call call, List<PushPair> list) {
}
};
public VOIPClient(BootService context) {
mContext = context;
mTTS = TTSHelper.getInstance(context);
if (mNsdController != null)
mNsdController.initialize();
}
public void start() {
SharedPreferences prefs = mContext.getPreferences();
int userId = prefs.getInt(MerryClient.PREF_USER_ID, 0);
String mUserId;
if (mSinch == null) {
mUserId = String.valueOf(userId);
mSinch = Sinch.getSinchClientBuilder().context(mContext.getApplicationContext()).userId(mUserId)
.applicationKey(CloudConfig.SINCH_APP_KEY)
.applicationSecret(CloudConfig.SINCH_APP_SECRET)
.environmentHost(CloudConfig.SINCH_ENVIRONMENT).build();
mSinch.setSupportCalling(true);
mSinch.setSupportManagedPush(false);
mSinch.addSinchClientListener(new MySinchClientListener());
mSinch.getCallClient().setRespectNativeCalls(false);
mSinch.getCallClient().addCallClientListener(new SinchCallClientListener());
mCurrentCall = null;
mSinch.startListeningOnActiveConnection();
mSinch.start();
}
}
public void tearDown() {
if (mSinch != null) {
mSinch.stopListeningOnActiveConnection();
mSinch.terminate();
mSinch = null;
}
}
public void restart() {
tearDown();
start();
}
public void initiateCall(final String targetUserName) {
Handler mainHandler = new Handler(mContext.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
if (targetUserName != null) {
try {
Call call = callUser(targetUserName);
call.addCallListener(mCallListener);
mCurrentCall = call;
} catch (Exception e) {
Log.e(TAG, "Initiate VOIP call failed", e);
// mContext.getAlexa().start();
}
}
}
});
}
public void answerCall() {
if (mCurrentCall != null) {
// mContext.getAlexa().tearDown();
mCurrentCall.answer();
}
}
public void hangUpCall() {
if (mCurrentCall != null) {
mCurrentCall.hangup();
}
}
private class SinchCallClientListener implements CallClientListener {
@Override
public void onIncomingCall(CallClient callClient, Call call) {
// mContext.getAlexa().stop();
Log.d(TAG, "Incoming call");
mTTS.speak("Incoming call from " + call.getRemoteUserId(), TTSHelper.UTTERANCE_VOIP_INCOMING);
call.addCallListener(mCallListener);
mCurrentCall = call;
// For testing only
answerCall();
}
}
public Call callUser(String userId) {
if (mSinch == null) {
start();
}
return mSinch.getCallClient().callUser(userId);
}
private class MySinchClientListener implements SinchClientListener {
@Override
public void onClientStarted(SinchClient sinchClient) {
Log.d(TAG, "Sinch Client starts: " + sinchClient.getLocalUserId());
mTTS.speak("Call ready", TTSHelper.UTTERANCE_VOIP_READY);
}
@Override
public void onClientStopped(SinchClient sinchClient) {
Log.d(TAG, "SinchClient stopped");
}
@Override
public void onClientFailed(SinchClient sinchClient, SinchError sinchError) {
Log.e(TAG, String.format("Sinch Client error %d: %s", sinchError.getCode(), sinchError.getMessage()));
mSinch.stopListeningOnActiveConnection();
mSinch.terminate();
mTTS.speak("Voice Over IP failed", TTSHelper.UTTERANCE_VOIP_FAIL);
}
@Override
public void onRegistrationCredentialsRequired(SinchClient sinchClient, ClientRegistration clientRegistration) {
Log.d(TAG, "Sinch Client requires registration");
}
@Override
public void onLogMessage(int i, String s, String s1) {
Log.d(TAG, s1);
}
}
}
I have initialized this instance in the service, calling
VOIPClient mVoip = new VOIPClient(this);
then I start the SinchClient from the NSD command class like this
Handler mainHandler = new Handler(mBootService.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
LocalRepository.getInstance().saveMerrySetupString("MerrySetup");
mBootService.getVOIP().start();
}
});
then it starts the sinchClient. again If I tried to call to other device it's calling the method like this `
public void initiateCall(final String targetUserName) {
Handler mainHandler = new Handler(mContext.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
if (targetUserName != null) {
try {
Call call = callUser(targetUserName);
call.addCallListener(mCallListener);
mCurrentCall = call;
} catch (Exception e) {
Log.e(TAG, "Initiate VOIP call failed", e);
// mContext.getAlexa().start();
}
}
}
});
}
then first time I can able to get the call then second time I am getting the exception like below
java.lang.IllegalStateException: SinchClient not started
at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.throwUnlessStarted(Unknown Source)
at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.call(Unknown Source)
at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.callUser(Unknown Source)
at com.sinch.android.rtc.internal.client.calling.DefaultCallClient.callUser(Unknown Source)
at tw.com.test.cloud.VOIPClient.callUser(VOIPClient.java:272)
at tw.com.test.cloud.VOIPClient$CallerThread.run(VOIPClient.java:293)
at java.lang.Thread.run(Thread.java:818)
Anyone suggest the solution for this? its getting
Is it threading issue? Please suggest the solution for this I can't make the call hereafter