package com.android.webrtc;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.webrtc.AudioSource;
import org.webrtc.AudioTrack;
import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
import org.webrtc.CameraEnumerator;
import org.webrtc.DataChannel;
import org.webrtc.DefaultVideoDecoderFactory;
import org.webrtc.DefaultVideoEncoderFactory;
import org.webrtc.EglBase;
import org.webrtc.IceCandidate;
import org.webrtc.Logging;
import org.webrtc.MediaConstraints;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.PeerConnectionFactory.InitializationOptions;
import org.webrtc.RendererCommon;
import org.webrtc.SessionDescription;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* 界面展示
* Created by taxiao on 2019/8/17
* CSDN:http://blog.csdn.net/yin13753884368/article
* Github:https://github.com/taxiao213
* 微信公众号:他晓
*/
public class WebRtcActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "WebRtcActivity";
private SurfaceViewRenderer localSurfaceView;
private SurfaceViewRenderer remoteSurfaceView;
private EglBase eglBase;
private PeerConnectionFactory peerConnectionFactory;
private VideoTrack videoTrack;
private AudioTrack audioTrack;
private PeerConnection peerConnection;
private List<String> streamList;
private WebSocketClient webSocketClient;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg != null) {
String obj = (String) msg.obj;
if (!TextUtils.isEmpty(obj)) {
switch (obj) {
case Constant.OPEN:
createPeerConnection();
break;
}
}
}
}
};
private List<PeerConnection.IceServer> iceServers;
private EditText tvFromName;
private EditText tvFrom;
private EditText tvToNname;
private EditText tvTo;
private Button btConnect;
private Button btCall;
private TextView tvIsCall;
private Button btReCall;
private Button btReFuse;
private DataChannel channel;
private MySdpObserver observer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webrtc);
localSurfaceView = findViewById(R.id.LocalSurfaceView);
remoteSurfaceView = findViewById(R.id.RemoteSurfaceView);
tvFromName = findViewById(R.id.tv_from_name);
tvFrom = findViewById(R.id.tv_from);
tvToNname = findViewById(R.id.tv_to_name);
tvTo = findViewById(R.id.tv_to);
btConnect = findViewById(R.id.bt_connect);
btCall = findViewById(R.id.bt_call);
tvIsCall = findViewById(R.id.tv_iscall);
btReFuse = findViewById(R.id.bt_refuse);
btReCall = findViewById(R.id.bt_recall);
btConnect.setOnClickListener(this);
btCall.setOnClickListener(this);
btReCall.setOnClickListener(this);
btReFuse.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v != null) {
switch (v.getId()) {
case R.id.bt_connect:
connectionWebsocket();
break;
case R.id.bt_call:
call();
break;
case R.id.bt_refuse:
reFuse();
break;
case R.id.bt_recall:
reCall();
break;
}
}
}
/**
* 连接Websocket
*/
private void connectionWebsocket() {
try {
webSocketClient = new WebSocketClient(URI.create(Constant.URL)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
setText("已连接");
Log.e(TAG, "onOpen == Status == " + handshakedata.getHttpStatus() + " StatusMessage == " + handshakedata.getHttpStatusMessage());
Model model = new Model(Constant.REGISTER, getFromName(), getFrom(), getToName(), getTo());
webSocketClient.send(new Gson().toJson(model));
}
@Override
public void onMessage(String message) {
Log.e(TAG, "onMessage == " + message);
if (!TextUtils.isEmpty(message)) {
Model model = new Gson().fromJson(message, Model.class);
if (model != null) {
String id = model.getId();
if (!TextUtils.isEmpty(id)) {
int isSucceed = model.getIsSucceed();
switch (id) {
case Constant.REGISTER_RESPONSE:
if (isSucceed == Constant.RESPONSE_SUCCEED) {
Message msg = new Message();
msg.obj = Constant.OPEN;
handler.sendMessage(msg);
Log.e(TAG, "连接成功");
} else if (isSucceed == Constant.RESPONSE_FAILURE) {
Log.e(TAG, "注册失败,已经注册");
}
break;
case Constant.CALL_RESPONSE:
if (isSucceed == Constant.RESPONSE_SUCCEED) {
Log.e(TAG, "对方在线,创建sdp offer");
createOffer();
} else if (isSucceed == Constant.RESPONSE_FAILURE) {
Log.e(TAG, "对方不在线,连接失败");
}
break;
case Constant.INCALL:
isIncall();
break;
case Constant.INCALL_RESPONSE:
if (isSucceed == Constant.RESPONSE_SUCCEED) {
createOffer();
Log.e(TAG, "对方同意接听");
} else if (isSucceed == Constant.RESPONSE_FAILURE) {
Log.e(TAG, "对方拒绝接听");
}
break;
case Constant.