update 视频
This commit is contained in:
@@ -1,26 +1,41 @@
|
||||
package com.shayu.onetoone.manager;
|
||||
|
||||
import android.Manifest;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import com.blankj.utilcode.util.PermissionUtils;
|
||||
import com.shayu.onetoone.listener.OnCallStatusListener;
|
||||
import com.yunbao.common.CommonAppContext;
|
||||
import com.yunbao.common.manager.IMLoginManager;
|
||||
import com.yunbao.common.utils.ToastUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import io.rong.callkit.util.CallKitUtils;
|
||||
import io.rong.calllib.CallUserProfile;
|
||||
import io.rong.calllib.IRongCallListener;
|
||||
import io.rong.calllib.IRongReceivedCallListener;
|
||||
import io.rong.calllib.RongCallClient;
|
||||
import io.rong.calllib.RongCallCommon;
|
||||
import io.rong.calllib.RongCallSession;
|
||||
import io.rong.calllib.StartCameraCallback;
|
||||
import io.rong.calllib.message.CallSTerminateMessage;
|
||||
import io.rong.common.RLog;
|
||||
import io.rong.imkit.IMCenter;
|
||||
import io.rong.imlib.RongIMClient;
|
||||
import io.rong.imlib.model.Conversation;
|
||||
|
||||
public class CallClientManager {
|
||||
public static CallClientManager manager;
|
||||
public static final String VIDEO_RECEIVED_CALL = "receivedCall";
|
||||
public static final String VIDEO_CALL = "call";
|
||||
private static CallClientManager manager;
|
||||
// private SurfaceView localVideo, remoteVideo;
|
||||
private List<OnCallStatusListener> listeners;
|
||||
|
||||
public static CallClientManager getManager() {
|
||||
if (manager == null) {
|
||||
@@ -30,31 +45,139 @@ public class CallClientManager {
|
||||
}
|
||||
|
||||
private CallClientManager() {
|
||||
listeners = new ArrayList<>();
|
||||
init();
|
||||
}
|
||||
|
||||
public SurfaceView getLocalVideo() {
|
||||
RongCallSession session = RongCallClient.getInstance().getCallSession();
|
||||
String userId= IMLoginManager.get(CommonAppContext.getTopActivity()).getUserInfo().getId()+"";
|
||||
for (CallUserProfile profile : session.getParticipantProfileList()) {
|
||||
if(profile.getUserId().equals(userId)){
|
||||
return profile.getVideoView();
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SurfaceView getRemoteVideo(String id) {
|
||||
RongCallSession session = RongCallClient.getInstance().getCallSession();
|
||||
for (CallUserProfile profile : session.getParticipantProfileList()) {
|
||||
if(profile.getUserId().equals(id)){
|
||||
return profile.getVideoView();
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
RongCallClient.setReceivedCallListener(new CallMeListener());
|
||||
}
|
||||
public void callVideo(String targetId, OnCallStatusListener statusListener) {
|
||||
List<String> userIds = new ArrayList<>();
|
||||
userIds.add(targetId);
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(statusListener));
|
||||
RongCallClient.getInstance().startCall(Conversation.ConversationType.PRIVATE,targetId,userIds,null, RongCallCommon.CallMediaType.VIDEO,null);
|
||||
|
||||
public void addOnVoIPCallListener(OnCallStatusListener statusListener) {
|
||||
listeners.add(statusListener);
|
||||
}
|
||||
|
||||
public void removeOnVoIPCallListener(OnCallStatusListener statusListener) {
|
||||
listeners.remove(statusListener);
|
||||
}
|
||||
|
||||
public void callVideo(String targetId) {
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(new OnCallStatusListener() {
|
||||
@Override
|
||||
public void onCallWait(SurfaceView localVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallWait(localVideo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallStart(String userId, SurfaceView remoteVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallStart(userId, remoteVideo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallEnd() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartFirstFrame() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onStartFirstFrame();
|
||||
}
|
||||
}
|
||||
}));
|
||||
List<String> userIds = new ArrayList<>();
|
||||
userIds.add(targetId);
|
||||
RongCallClient.getInstance().startCall(Conversation.ConversationType.PRIVATE, targetId, userIds, null, RongCallCommon.CallMediaType.VIDEO, null);
|
||||
}
|
||||
|
||||
public void acceptCall(String callId) {
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(new OnCallStatusListener() {
|
||||
@Override
|
||||
public void onCallWait(SurfaceView localVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallWait(localVideo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallStart(String userId, SurfaceView remoteVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallStart(userId, remoteVideo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallEnd() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartFirstFrame() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onStartFirstFrame();
|
||||
}
|
||||
}
|
||||
}));
|
||||
RongCallClient.getInstance().acceptCall(callId);
|
||||
}
|
||||
|
||||
public void endCall() {
|
||||
if (RongCallClient.getInstance() != null && RongCallClient.getInstance().getCallSession() != null) {
|
||||
RongCallClient.getInstance().hangUpCall(RongCallClient.getInstance().getCallSession().getCallId());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCalling() {
|
||||
return RongCallClient.getInstance() != null && RongCallClient.getInstance().getCallSession() != null;
|
||||
}
|
||||
|
||||
private static class CallMeListener implements IRongReceivedCallListener {
|
||||
|
||||
@Override
|
||||
public void onReceivedCall(RongCallSession callSession) {
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("model", VIDEO_RECEIVED_CALL);
|
||||
bundle.putString("targetId", callSession.getTargetId());
|
||||
bundle.putString("callId", callSession.getCallId());
|
||||
bundle.putString("sessionId", callSession.getSessionId());
|
||||
RouteManager.forwardActivity(RouteManager.ACTIVITY_CALL_VIDEO, bundle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckPermission(RongCallSession callSession) {
|
||||
PermissionUtils.permission(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO).callback(new PermissionUtils.SimpleCallback() {
|
||||
ToastUtil.show("权限申请");
|
||||
PermissionUtils.permission(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO).callback(new PermissionUtils.SimpleCallback() {
|
||||
@Override
|
||||
public void onGranted() {
|
||||
RongCallClient.getInstance().onPermissionGranted();
|
||||
@@ -64,21 +187,28 @@ public class CallClientManager {
|
||||
public void onDenied() {
|
||||
RongCallClient.getInstance().onPermissionDenied();
|
||||
}
|
||||
});
|
||||
}).request();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class CallStatusListener implements IRongCallListener {
|
||||
private class CallStatusListener implements IRongCallListener {
|
||||
OnCallStatusListener statusListener;
|
||||
private long time = 0;
|
||||
|
||||
public CallStatusListener(OnCallStatusListener statusListener) {
|
||||
this.statusListener = statusListener;
|
||||
}
|
||||
|
||||
public long getTime(long activeTime) {
|
||||
long tmpTime = activeTime == 0 ? 0 : (System.currentTimeMillis() - activeTime) / 1000;
|
||||
time = tmpTime == 0 ? time : tmpTime;
|
||||
return time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallIncoming(RongCallSession callSession, SurfaceView localVideo) {
|
||||
|
||||
System.out.println("CallStatusListener.onCallIncoming");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +220,10 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onCallOutgoing(RongCallSession callSession, SurfaceView localVideo) {
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
statusListener.onCallWait(localVideo);
|
||||
System.out.println("CallStatusListener.onCallOutgoing");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +235,9 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onCallConnected(RongCallSession callSession, SurfaceView localVideo) {
|
||||
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
statusListener.onCallWait(localVideo);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,17 +249,72 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onCallDisconnected(RongCallSession callSession, RongCallCommon.CallDisconnectedReason reason) {
|
||||
System.out.println("CallStatusListener.onCallDisconnected");
|
||||
|
||||
String senderId;
|
||||
String extra = "";
|
||||
|
||||
if (callSession == null) {
|
||||
RLog.e("CallStatusListener", "onCallDisconnected. callSession is null!");
|
||||
statusListener.onCallEnd();
|
||||
return;
|
||||
}
|
||||
senderId = callSession.getInviterUserId();
|
||||
long time = getTime(callSession.getActiveTime());
|
||||
if (time > 0) {
|
||||
if (time >= 3600) {
|
||||
extra =
|
||||
String.format(
|
||||
Locale.ROOT,
|
||||
"%d:%02d:%02d",
|
||||
time / 3600,
|
||||
(time % 3600) / 60,
|
||||
(time % 60));
|
||||
} else {
|
||||
extra = String.format(Locale.ROOT, "%02d:%02d", (time % 3600) / 60, (time % 60));
|
||||
}
|
||||
}
|
||||
if (!TextUtils.isEmpty(senderId)) {
|
||||
CallSTerminateMessage message = new CallSTerminateMessage();
|
||||
message.setReason(reason);
|
||||
message.setMediaType(callSession.getMediaType());
|
||||
message.setExtra(extra);
|
||||
long serverTime =
|
||||
System.currentTimeMillis() - RongIMClient.getInstance().getDeltaTime();
|
||||
if (senderId.equals(callSession.getSelfUserId())) {
|
||||
message.setDirection("MO");
|
||||
IMCenter.getInstance()
|
||||
.insertOutgoingMessage(
|
||||
Conversation.ConversationType.PRIVATE,
|
||||
callSession.getTargetId(),
|
||||
io.rong.imlib.model.Message.SentStatus.SENT,
|
||||
message,
|
||||
serverTime,
|
||||
null);
|
||||
} else {
|
||||
message.setDirection("MT");
|
||||
IMCenter.getInstance()
|
||||
.insertIncomingMessage(
|
||||
Conversation.ConversationType.PRIVATE,
|
||||
callSession.getTargetId(),
|
||||
senderId,
|
||||
CallKitUtils.getReceivedStatus(reason),
|
||||
message,
|
||||
serverTime,
|
||||
null);
|
||||
}
|
||||
}
|
||||
statusListener.onCallEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoteUserRinging(String userId) {
|
||||
|
||||
System.out.println("CallStatusListener.onRemoteUserRinging");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoteUserAccept(String userId, RongCallCommon.CallMediaType mediaType) {
|
||||
|
||||
System.out.println("CallStatusListener.onRemoteUserAccept");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,21 +328,24 @@ public class CallClientManager {
|
||||
* 如果对端调用{@link RongCallClient#startCall(int, boolean, Conversation.ConversationType, String, List, List, RongCallCommon.CallMediaType, String, StartCameraCallback)} 或
|
||||
* {@link RongCallClient#acceptCall(String, int, boolean, StartCameraCallback)}开始的音视频通话,则可以使用如下设置改变对端视频流的镜像显示:<br />
|
||||
* <pre class="prettyprint">
|
||||
* public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
* if (null != remoteVideo) {
|
||||
* ((RongRTCVideoView) remoteVideo).setMirror( boolean);//观看对方视频流是否镜像处理
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
* if (null != remoteVideo) {
|
||||
* ((RongRTCVideoView) remoteVideo).setMirror( boolean);//观看对方视频流是否镜像处理
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@Override
|
||||
public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
statusListener.onCallStart(userId,remoteVideo);
|
||||
remoteVideo.setZOrderOnTop(false);
|
||||
remoteVideo.setZOrderMediaOverlay(false);
|
||||
statusListener.onCallStart(userId, remoteVideo);
|
||||
System.out.println("CallStatusListener.onRemoteUserJoined");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoteUserInvited(String userId, RongCallCommon.CallMediaType mediaType) {
|
||||
|
||||
System.out.println("CallStatusListener.onRemoteUserInvited");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,17 +357,23 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onRemoteUserLeft(String userId, RongCallCommon.CallDisconnectedReason reason) {
|
||||
|
||||
System.out.println("CallStatusListener.onRemoteUserLeft");
|
||||
if (statusListener != null) {
|
||||
statusListener.onCallEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMediaTypeChanged(String userId, RongCallCommon.CallMediaType mediaType, SurfaceView video) {
|
||||
|
||||
System.out.println("CallStatusListener.onMediaTypeChanged");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(RongCallCommon.CallErrorCode errorCode) {
|
||||
|
||||
System.out.println("CallStatusListener.onError");
|
||||
if (statusListener != null) {
|
||||
statusListener.onCallEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -199,7 +398,9 @@ public class CallClientManager {
|
||||
|
||||
@Override
|
||||
public void onFirstRemoteVideoFrame(String userId, int height, int width) {
|
||||
|
||||
if (statusListener != null) {
|
||||
statusListener.onStartFirstFrame();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user