完成了数据上报socket
This commit is contained in:
@@ -2,7 +2,6 @@ package com.mogo.cloud;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
@@ -11,8 +10,7 @@ import android.widget.ToggleButton;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.mogo.cloud.live.listener.ILiveProgressListener;
|
||||
import com.mogo.cloud.live.manager.ZeGoLiveManager;
|
||||
import com.mogo.cloud.util.Devices;
|
||||
import com.mogo.cloud.live.manager.MoGoLiveManager;
|
||||
|
||||
|
||||
public class LivePlayActivity extends AppCompatActivity {
|
||||
@@ -36,12 +34,12 @@ public class LivePlayActivity extends AppCompatActivity {
|
||||
Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
|
||||
if (isChecked) {
|
||||
String roomId = etLookRoomId.getText().toString().trim();
|
||||
mStreamId = ZeGoLiveManager.STREAM_ID_PREFIX + roomId;
|
||||
ZeGoLiveManager.getInstance().init(this.getApplication(), null);
|
||||
ZeGoLiveManager.getInstance().loginRoom("F803EB2046PZD00140", roomId);
|
||||
ZeGoLiveManager.getInstance().setLiveProgressListener(listener);
|
||||
mStreamId = MoGoLiveManager.STREAM_ID_PREFIX + roomId;
|
||||
MoGoLiveManager.getInstance().init(this.getApplication(), null);
|
||||
MoGoLiveManager.getInstance().loginRoom("F803EB2046PZD00140", roomId);
|
||||
MoGoLiveManager.getInstance().setLiveProgressListener(listener);
|
||||
} else {
|
||||
ZeGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
MoGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -87,15 +85,15 @@ public class LivePlayActivity extends AppCompatActivity {
|
||||
private void toggleLive(boolean isChecked) {
|
||||
Log.i(TAG, "toggleLive status : " + isChecked + " , mStreamId : " + mStreamId);
|
||||
if (isChecked) {
|
||||
ZeGoLiveManager.getInstance().startLive(mStreamId, surfaceView);
|
||||
MoGoLiveManager.getInstance().startLive(mStreamId, surfaceView);
|
||||
} else {
|
||||
ZeGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
MoGoLiveManager.getInstance().stopLive(mStreamId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
ZeGoLiveManager.getInstance().onDestroyLive();
|
||||
MoGoLiveManager.getInstance().onDestroyLive();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.manager.CameraFrameManager;
|
||||
import com.mogo.cloud.live.manager.LiveStreamManager;
|
||||
import com.mogo.cloud.live.manager.LiveStreamManagerImpl;
|
||||
import com.mogo.cloud.live.server.PushService;
|
||||
import com.mogo.cloud.util.Devices;
|
||||
|
||||
@@ -20,6 +22,9 @@ public class LivePushActivity extends BaseLiveActivity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// 初始化直播流管理
|
||||
LiveStreamManager liveStreamManager = LiveStreamManagerImpl.getInstance(this, Devices.getSn());
|
||||
liveStreamManager.uploadCamStatus(1, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,11 +40,11 @@ public class LivePushActivity extends BaseLiveActivity {
|
||||
public void toggleLive(boolean isLive) {
|
||||
Log.i(TAG, "toggleLive : " + isLive);
|
||||
this.isLive = isLive;
|
||||
if (isLive) {
|
||||
PushService.startService(this, PushService.ACTION_START_RTMP_PUSH, Devices.getSn());
|
||||
} else {
|
||||
PushService.startService(this, PushService.ACTION_STOP_RTMP_PUSH, null);
|
||||
}
|
||||
// if (isLive) {
|
||||
// PushService.startService(this, PushService.ACTION_START_RTMP_PUSH, Devices.getSn());
|
||||
// } else {
|
||||
// PushService.startService(this, PushService.ACTION_STOP_RTMP_PUSH, null);
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.mogo.cloud.live.listener;
|
||||
|
||||
import com.mogo.cloud.live.socket.IotMessageType;
|
||||
|
||||
/**
|
||||
* 常连接消息回调用
|
||||
*/
|
||||
public interface BizMessageListener {
|
||||
void onMessageReceived(IotMessageType type, String content);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.mogo.cloud.live.manager;
|
||||
|
||||
|
||||
import com.mogo.cloud.live.listener.ILiveStatusListener;
|
||||
|
||||
public interface LiveStreamManager {
|
||||
/**
|
||||
* 开始直播
|
||||
*/
|
||||
void startLiveStream();
|
||||
|
||||
/**
|
||||
* 停止直播
|
||||
*/
|
||||
void stopLiveStream();
|
||||
|
||||
/**
|
||||
* 获取直播状态
|
||||
*/
|
||||
int getLiveStatus();
|
||||
|
||||
/**
|
||||
* 更新摄像头状态
|
||||
*
|
||||
* @param frontStatus front camera status 0:未知 1:可用 2:不可以
|
||||
* @param backStatus back camera status 0:未知 1:可用 2:不可以
|
||||
*/
|
||||
void uploadCamStatus(int frontStatus, int backStatus);
|
||||
|
||||
/**
|
||||
* 通知更新YUV数据
|
||||
*
|
||||
* @param data origin yuv data
|
||||
* @param width data width
|
||||
* @param height data height
|
||||
* @param type 1:YV12 2:NV21 3:I420 4 nv12
|
||||
*/
|
||||
void notifyYUVData(byte[] data, int width, int height, int type);
|
||||
|
||||
/**
|
||||
* 直播状态改变时回调
|
||||
*
|
||||
* @param changeCallback live status change callback
|
||||
*/
|
||||
void setLiveStatusChangeCallback(ILiveStatusListener changeCallback);
|
||||
|
||||
/**
|
||||
* 设置网络环境
|
||||
*/
|
||||
void setNetEnvironment(int environment);
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
void release();
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.mogo.cloud.live.manager;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.listener.ILiveStatusListener;
|
||||
import com.mogo.cloud.live.server.PushService;
|
||||
import com.mogo.cloud.live.utils.LiveStreamUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 直播流管理实现类
|
||||
*
|
||||
* @author donghongyu
|
||||
*/
|
||||
public class LiveStreamManagerImpl implements LiveStreamManager {
|
||||
private static final String TAG = "LiveStreamManagerImpl";
|
||||
|
||||
private static volatile LiveStreamManager sInstance;
|
||||
public static volatile String mDevicesId;
|
||||
|
||||
private Context mContext;
|
||||
private LiveStreamUtils mLiveStreamUtils;
|
||||
|
||||
public static LiveStreamManager getInstance(Context context, String devicesId) {
|
||||
if (sInstance == null) {
|
||||
synchronized (LiveStreamManagerImpl.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LiveStreamManagerImpl(context, devicesId);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private LiveStreamManagerImpl(Context context, String devicesId) {
|
||||
mContext = context;
|
||||
mDevicesId = devicesId;
|
||||
mLiveStreamUtils = LiveStreamUtils.getInstance(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLiveStatus() {
|
||||
return mLiveStreamUtils.getCam1LiveStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startLiveStream() {
|
||||
Log.d(TAG, "startLiveStream devices id:" + mDevicesId);
|
||||
PushService.startService(mContext, PushService.ACTION_START_RTMP_PUSH, mDevicesId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopLiveStream() {
|
||||
Log.d(TAG, "stopLiveStream devices id:" + mDevicesId);
|
||||
PushService.startService(mContext, PushService.ACTION_STOP_RTMP_PUSH, mDevicesId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadCamStatus(int frontStatus, int backStatus) {
|
||||
Log.d(TAG, "uploadCamStatus frontStatus is:" + frontStatus + " backStatus is:" + backStatus);
|
||||
mLiveStreamUtils.uploadCamStatus(frontStatus, backStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyYUVData(byte[] data, int width, int height, int type) {
|
||||
CameraFrameManager.getInstance().notifyYUVData(data, width, height, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLiveStatusChangeCallback(ILiveStatusListener callback) {
|
||||
mLiveStreamUtils.setLiveStatusChangeCallback(callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNetEnvironment(int environment) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
mLiveStreamUtils.release();
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ import im.zego.zegoexpress.entity.ZegoVideoFrameParam;
|
||||
/**
|
||||
* 即构直播管理
|
||||
*/
|
||||
public class ZeGoLiveManager {
|
||||
public class MoGoLiveManager {
|
||||
public static final String TAG = "ZeGoLiveManager";
|
||||
|
||||
/**
|
||||
@@ -59,7 +59,7 @@ public class ZeGoLiveManager {
|
||||
/**
|
||||
* 直播推送配置对象
|
||||
*/
|
||||
private MGLivePushConfig mLivePushConfig;
|
||||
private MoGoLivePushConfig mLivePushConfig;
|
||||
/**
|
||||
* 直播会议ID,使用推流端的车机信息建立房间ID,这里采用格式为:ROOM_ID_车机SN编号
|
||||
*/
|
||||
@@ -102,17 +102,17 @@ public class ZeGoLiveManager {
|
||||
private boolean isPlaying;
|
||||
|
||||
private static final class Holder {
|
||||
private static final ZeGoLiveManager manager = new ZeGoLiveManager();
|
||||
private static final MoGoLiveManager manager = new MoGoLiveManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取即构直播管理
|
||||
*/
|
||||
public static ZeGoLiveManager getInstance() {
|
||||
public static MoGoLiveManager getInstance() {
|
||||
return Holder.manager;
|
||||
}
|
||||
|
||||
private ZeGoLiveManager() {
|
||||
private MoGoLiveManager() {
|
||||
}
|
||||
|
||||
public void setAppId(long appId) {
|
||||
@@ -141,7 +141,7 @@ public class ZeGoLiveManager {
|
||||
* @param application 上下文
|
||||
* @param livePushConfig 是否需要配置
|
||||
*/
|
||||
public void init(Application application, MGLivePushConfig livePushConfig) {
|
||||
public void init(Application application, MoGoLivePushConfig livePushConfig) {
|
||||
mApplication = application;
|
||||
mLivePushConfig = livePushConfig;
|
||||
// 通过 advancedConfig 设置 uuid 过滤字段,设置之后 SDK 只会抛出前 12 个字节为开发者所设置 uuid 的 SEI
|
||||
@@ -3,7 +3,7 @@ package com.mogo.cloud.live.manager;
|
||||
/**
|
||||
* 直播配置对象
|
||||
*/
|
||||
public class MGLivePushConfig {
|
||||
public class MoGoLivePushConfig {
|
||||
private int mWidth = 1280;
|
||||
private int mHeight = 720;
|
||||
private int mVideoBitrate = 1200;
|
||||
@@ -12,8 +12,30 @@ public class MGLivePushConfig {
|
||||
private int mAudioChannels = 1;
|
||||
private int mAudioFormat = 2;
|
||||
private boolean isMute = true;
|
||||
private String devicesId;
|
||||
|
||||
public MGLivePushConfig() {
|
||||
private static MoGoLivePushConfig mMoGoLivePushConfig;
|
||||
|
||||
private MoGoLivePushConfig() {
|
||||
}
|
||||
|
||||
public static MoGoLivePushConfig getInstance() {
|
||||
if (mMoGoLivePushConfig == null) {
|
||||
synchronized (MoGoLivePushConfig.class) {
|
||||
if (mMoGoLivePushConfig == null) {
|
||||
mMoGoLivePushConfig = new MoGoLivePushConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mMoGoLivePushConfig;
|
||||
}
|
||||
|
||||
public String getDevicesId() {
|
||||
return devicesId;
|
||||
}
|
||||
|
||||
public void setDevicesId(String devicesId) {
|
||||
this.devicesId = devicesId;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
@@ -10,8 +10,8 @@ import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.listener.IYUVDataListener;
|
||||
import com.mogo.cloud.live.manager.CameraFrameManager;
|
||||
import com.mogo.cloud.live.manager.MGLivePushConfig;
|
||||
import com.mogo.cloud.live.manager.ZeGoLiveManager;
|
||||
import com.mogo.cloud.live.manager.MoGoLivePushConfig;
|
||||
import com.mogo.cloud.live.manager.MoGoLiveManager;
|
||||
import com.mogo.cloud.live.utils.ByteUtils;
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ public class PushService extends Service implements IYUVDataListener {
|
||||
private static final int HEIGHT = 720;
|
||||
|
||||
// 自研直播SDK
|
||||
private MGLivePushConfig mLivePushConfig;
|
||||
private ZeGoLiveManager mLivePusher;
|
||||
private MoGoLivePushConfig mLivePushConfig;
|
||||
private MoGoLiveManager mLivePusher;
|
||||
|
||||
/**
|
||||
* 当前设备ID,作为推流端,userID==roomId==streamID
|
||||
@@ -54,7 +54,7 @@ public class PushService extends Service implements IYUVDataListener {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Log.d(TAG, "初始化推流服务……");
|
||||
mLivePushConfig = new MGLivePushConfig();
|
||||
mLivePushConfig = MoGoLivePushConfig.getInstance();
|
||||
mLivePushConfig.setWidth(WIDTH);
|
||||
mLivePushConfig.setHeight(HEIGHT);
|
||||
mLivePushConfig.setVideoBitrate(1500);
|
||||
@@ -64,7 +64,7 @@ public class PushService extends Service implements IYUVDataListener {
|
||||
mLivePushConfig.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT);
|
||||
mLivePushConfig.setMute(true);
|
||||
|
||||
mLivePusher = ZeGoLiveManager.getInstance();
|
||||
mLivePusher = MoGoLiveManager.getInstance();
|
||||
mLivePusher.init(getApplication(), mLivePushConfig);
|
||||
}
|
||||
|
||||
@@ -103,6 +103,10 @@ public class PushService extends Service implements IYUVDataListener {
|
||||
return;
|
||||
}
|
||||
mDevicesId = devicesId;
|
||||
if (mLivePusher == null) {
|
||||
mLivePusher = MoGoLiveManager.getInstance();
|
||||
mLivePusher.init(getApplication(), mLivePushConfig);
|
||||
}
|
||||
// 登录房间
|
||||
mLivePusher.loginRoom(mDevicesId, mDevicesId);
|
||||
// 开始发布
|
||||
@@ -135,7 +139,7 @@ public class PushService extends Service implements IYUVDataListener {
|
||||
if (mLivePusher != null && mLivePusher.isCaptureStatus()
|
||||
&& mLivePusher.isPushing() && data != null) {
|
||||
// 将YUV数据发布到即构
|
||||
ZeGoLiveManager.getInstance().startPublishingStream(
|
||||
MoGoLiveManager.getInstance().startPublishingStream(
|
||||
ByteUtils.getBuffer(data, data.length), data.length,
|
||||
SystemClock.elapsedRealtime());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.mogo.cloud.live.socket;
|
||||
|
||||
public enum IotMessageType {
|
||||
|
||||
//Push 推流消息
|
||||
MSG_LIVE_PUSH_STATUS(0),
|
||||
|
||||
//Cancel car alarm 消息
|
||||
MSG_CANCEL_CAR_ALARM(1),
|
||||
|
||||
//单个文件上传
|
||||
MSG_UPLOAD_SINGLE_FILE(2),
|
||||
|
||||
//切换摄像头
|
||||
MSG_SWITCH_CAMERA(3),
|
||||
|
||||
//组文件上传
|
||||
MSG_UPLOAD_MULTI_FILE(4),
|
||||
|
||||
//抓拍图片上传
|
||||
MSG_UPLOAD_CAPTURE_PIC(5),
|
||||
|
||||
//未知消息
|
||||
UNKNOWN(-1),
|
||||
;
|
||||
|
||||
private int type;
|
||||
|
||||
private IotMessageType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public static IotMessageType getMsgType(int type){
|
||||
final IotMessageType[] messageTypes = IotMessageType.values();
|
||||
for(IotMessageType messageType : messageTypes){
|
||||
if (messageType.getType() == type){
|
||||
return messageType;
|
||||
}
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.mogo.cloud.live.socket;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.socket.IMogoCloudSocketMsgAckListener;
|
||||
import com.mogo.cloud.socket.MsgBody;
|
||||
import com.mogo.cloud.socket.SocketManager;
|
||||
|
||||
/**
|
||||
* Socket常链接工具类
|
||||
*/
|
||||
public class SocketMsgUtils implements IMogoCloudSocketMsgAckListener {
|
||||
private static final String TAG = "SocketMsgUtils";
|
||||
|
||||
private static final String appId = "liveStream";
|
||||
private static final int msgType = 1973;
|
||||
|
||||
private static volatile SocketMsgUtils sInstance;
|
||||
|
||||
private SocketManager mSocketManager;
|
||||
private Context mContext;
|
||||
|
||||
public static SocketMsgUtils getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
synchronized (SocketMsgUtils.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new SocketMsgUtils(context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private SocketMsgUtils(Context context) {
|
||||
mContext = context;
|
||||
initSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化长连接
|
||||
*/
|
||||
private void initSocket() {
|
||||
mSocketManager = SocketManager.getInstance();
|
||||
mSocketManager.init(mContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传相机信息
|
||||
*
|
||||
* @param body 数据包
|
||||
*/
|
||||
public void uploadCamInfo(MsgBody body) {
|
||||
mSocketManager.sendMsg(appId, msgType, body, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
public void release() {
|
||||
mSocketManager.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAck(long msgId) {
|
||||
Log.i(TAG, "msgId=" + msgId);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.mogo.cloud.live.socket;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.zhidao.ptech.connsvr.protocol.MogoConnsvr;
|
||||
import com.zhidao.ptech.shadow.server.protocol.DeviceInfo;
|
||||
import com.zhidao.utils.common.TelephoneUtil;
|
||||
|
||||
public class SocketRequestUtils {
|
||||
public static DeviceInfo.DeviceInfoData buildDeviceData(int c1, int c2) {
|
||||
DeviceInfo.CameraStatus c1Status = DeviceInfo.CameraStatus.forNumber(c1);
|
||||
DeviceInfo.CameraStatus c2Status = DeviceInfo.CameraStatus.forNumber(c2);
|
||||
DeviceInfo.CameraInfo cameraInfo = DeviceInfo.CameraInfo.newBuilder()
|
||||
.setC1(c1Status)
|
||||
.setC2(c2Status).build();
|
||||
|
||||
DeviceInfo.DeviceInfoData data = DeviceInfo.DeviceInfoData.newBuilder()
|
||||
.setCameraInfo(cameraInfo)
|
||||
.setSn(TelephoneUtil.getSerialNumber())
|
||||
.setTimestamp(System.currentTimeMillis()).build();
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] buildSocketPayload(byte[] payload, int payloadType) {
|
||||
MogoConnsvr.Payload payloadData = MogoConnsvr.Payload.newBuilder()
|
||||
.setMsgType(payloadType)
|
||||
.setPayload(ByteString.copyFrom(payload)).build();
|
||||
return payloadData.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package com.mogo.cloud.live.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.listener.BizMessageListener;
|
||||
import com.mogo.cloud.live.listener.ILiveStatusListener;
|
||||
import com.mogo.cloud.live.manager.CameraFrameManager;
|
||||
import com.mogo.cloud.live.manager.LiveStreamManagerImpl;
|
||||
import com.mogo.cloud.live.server.PushService;
|
||||
import com.mogo.cloud.live.socket.IotMessageType;
|
||||
import com.mogo.cloud.live.socket.SocketMsgUtils;
|
||||
import com.mogo.cloud.live.socket.SocketRequestUtils;
|
||||
import com.mogo.cloud.socket.MsgBody;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import static com.mogo.cloud.live.socket.IotMessageType.MSG_LIVE_PUSH_STATUS;
|
||||
|
||||
|
||||
public class LiveStreamUtils implements BizMessageListener {
|
||||
private static final String TAG = "LiveStreamUtils";
|
||||
private static final int POLL_TIME = 30 * 1000;
|
||||
// 循环上报摄像头可直播状态间隔时间
|
||||
private static final int PUSH_CAM_TIME = 10 * 1000;
|
||||
private static final int PUSH_START = 0;
|
||||
private static final int PUSH_STOP = 1;
|
||||
private static final String C1 = "C_1"; //前置摄像头
|
||||
private static final String C2 = "C_2"; //后置摄像头
|
||||
|
||||
private static volatile LiveStreamUtils sInstance;
|
||||
|
||||
private static volatile int sCam1LiveStatus = 0, sCam2LiveStatus = 0;
|
||||
private static volatile int sCam1AvailableStatus = 0, sCam2AvailableStatus = 0;
|
||||
|
||||
private ILiveStatusListener mLiveStatusCallback;
|
||||
private Context mContext;
|
||||
private Handler mHandler;
|
||||
|
||||
public static LiveStreamUtils getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
synchronized (LiveStreamUtils.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LiveStreamUtils(context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private LiveStreamUtils(Context context) {
|
||||
mContext = context;
|
||||
mHandler = new Handler();
|
||||
init();
|
||||
initPoll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
private void init() {
|
||||
// 初始化Socket长连接通道
|
||||
SocketMsgUtils.getInstance(mContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化查询摄像头状态
|
||||
*/
|
||||
private void initPoll() {
|
||||
restartCamStatusLoop();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前置摄像头直播状态
|
||||
*/
|
||||
public int getCam1LiveStatus() {
|
||||
return sCam1LiveStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后置摄像头直播状态
|
||||
*/
|
||||
public int getCam2LiveStatus() {
|
||||
return sCam2LiveStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传摄像头状态
|
||||
*
|
||||
* @param cam1Status 前置摄像头可直播状态
|
||||
* @param cam2Status 后置摄像头可直播状态
|
||||
*/
|
||||
public void uploadCamStatus(int cam1Status, int cam2Status) {
|
||||
Log.i(TAG, "上传摄像头状态");
|
||||
|
||||
sCam1AvailableStatus = cam1Status;
|
||||
sCam2AvailableStatus = cam2Status;
|
||||
|
||||
MsgBody msgBody = new MsgBody();
|
||||
msgBody.msgType(196614);
|
||||
msgBody.content(SocketRequestUtils.buildDeviceData(cam1Status, cam2Status));
|
||||
|
||||
SocketMsgUtils.getInstance(mContext).uploadCamInfo(msgBody);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(IotMessageType type, String content) {
|
||||
if (type == MSG_LIVE_PUSH_STATUS) {
|
||||
Log.d(TAG, content);
|
||||
rtmpPushHandler(content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收到长连接指令
|
||||
*
|
||||
* @param str 指令数据
|
||||
*/
|
||||
private void rtmpPushHandler(String str) {
|
||||
try {
|
||||
JSONObject content = new JSONObject(str);
|
||||
int status = content.optInt("status");
|
||||
String videoChannel = content.optString("videoChannel");
|
||||
|
||||
// 前置摄像头
|
||||
if (C1.equals(videoChannel)) {
|
||||
// 停止直播
|
||||
if (status == PUSH_STOP) {
|
||||
PushService.startService(mContext, PushService.ACTION_STOP_RTMP_PUSH, LiveStreamManagerImpl.mDevicesId);
|
||||
}
|
||||
// 开始直播
|
||||
else {
|
||||
PushService.startService(mContext, PushService.ACTION_START_RTMP_PUSH, LiveStreamManagerImpl.mDevicesId);
|
||||
}
|
||||
|
||||
// 同步状态
|
||||
if (sCam1LiveStatus != PUSH_STOP) {
|
||||
liveStatusCallbackOnChange(C1, PUSH_STOP);
|
||||
CameraFrameManager.getInstance().release();
|
||||
} else {
|
||||
liveStatusCallbackOnChange(C1, PUSH_START);
|
||||
}
|
||||
sCam1LiveStatus = status;
|
||||
} else {
|
||||
Log.d(TAG, "目前只支持前置行车记录仪");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直播状态回调
|
||||
*
|
||||
* @param camId 摄像头ID
|
||||
* @param status 状态
|
||||
*/
|
||||
private void liveStatusCallbackOnChange(String camId, int status) {
|
||||
if (mLiveStatusCallback != null) {
|
||||
mLiveStatusCallback.onChange(camId, status);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启摄像头状态循环
|
||||
*/
|
||||
private void restartCamStatusLoop() {
|
||||
Log.i(TAG, "开启摄像头状态循环");
|
||||
if (mHandler != null) {
|
||||
mHandler.removeCallbacks(mCamStatusRun);
|
||||
mHandler.postDelayed(mCamStatusRun, PUSH_CAM_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 循环同步摄像头状态
|
||||
*/
|
||||
private Runnable mCamStatusRun = () -> {
|
||||
Log.i(TAG, "循环同步摄像头状态");
|
||||
uploadCamStatus(sCam1AvailableStatus, sCam2AvailableStatus);
|
||||
restartCamStatusLoop();
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置直播状态回调用
|
||||
*
|
||||
* @param changeCallback 回调接口对象
|
||||
*/
|
||||
public void setLiveStatusChangeCallback(ILiveStatusListener changeCallback) {
|
||||
mLiveStatusCallback = changeCallback;
|
||||
}
|
||||
|
||||
public void release() {
|
||||
try {
|
||||
sCam1LiveStatus = 0;
|
||||
sCam2LiveStatus = 0;
|
||||
SocketMsgUtils.getInstance(mContext).release();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user