完善了通过指令下发的直播流程
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package com.mogo.cloud;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.manager.CameraFrameManager;
|
||||
import com.mogo.cloud.live.manager.ILiveStreamManager;
|
||||
@@ -21,7 +20,6 @@ public class LivePushActivity extends BaseLiveActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// 初始化直播流管理
|
||||
ILiveStreamManager liveStreamManager = LiveStreamManagerImpl.getInstance(this, Devices.getSn());
|
||||
liveStreamManager.uploadCamStatus(1, 1);
|
||||
@@ -29,22 +27,13 @@ public class LivePushActivity extends BaseLiveActivity {
|
||||
|
||||
@Override
|
||||
public void onVideoFrame(byte[] bytes, int bytesLength) {
|
||||
if (!isLive) {
|
||||
return;
|
||||
}
|
||||
//Log.i(TAG, "onVideoFrame byte length: " + bytesLength);
|
||||
CameraFrameManager.getInstance().notifyYUVData(bytes, 1280, 720, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -103,7 +103,7 @@ public class CameraFrameManager {
|
||||
}
|
||||
|
||||
private void dispatchData(byte[] data) {
|
||||
if (globalDataListener != null && globalDataListener.size() > 0) {
|
||||
if (globalDataListener.size() > 0) {
|
||||
for (IYUVDataListener callback : globalDataListener) {
|
||||
if (callback != null) {
|
||||
callback.onFrame(data);
|
||||
|
||||
@@ -472,7 +472,6 @@ public class MoGoLiveManager {
|
||||
* 停止观看直播
|
||||
*/
|
||||
public void onDestroyLive() {
|
||||
stopPreview();
|
||||
logOutRoom();
|
||||
destroyEngine();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.mogo.cloud.live.socket;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mogo.cloud.live.model.CommandModel;
|
||||
import com.mogo.cloud.socket.IMogoCloudSocketMsgAckListener;
|
||||
import com.mogo.cloud.socket.IMogoCloudSocketOnMessageListener;
|
||||
import com.mogo.cloud.socket.MsgBody;
|
||||
@@ -12,8 +11,7 @@ import com.mogo.cloud.socket.SocketManager;
|
||||
/**
|
||||
* Socket常链接工具类
|
||||
*/
|
||||
public class SocketMsgUtils implements IMogoCloudSocketMsgAckListener,
|
||||
IMogoCloudSocketOnMessageListener<CommandModel> {
|
||||
public class SocketMsgUtils implements IMogoCloudSocketMsgAckListener {
|
||||
private static final String TAG = "SocketMsgUtils";
|
||||
|
||||
private static final String appId = "liveStream";
|
||||
@@ -23,31 +21,33 @@ public class SocketMsgUtils implements IMogoCloudSocketMsgAckListener,
|
||||
|
||||
private SocketManager mSocketManager;
|
||||
private Context mContext;
|
||||
private IMogoCloudSocketOnMessageListener mMessageListener;
|
||||
|
||||
public static SocketMsgUtils getInstance(Context context) {
|
||||
public static SocketMsgUtils getInstance(Context context, IMogoCloudSocketOnMessageListener listener) {
|
||||
if (sInstance == null) {
|
||||
synchronized (SocketMsgUtils.class) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new SocketMsgUtils(context.getApplicationContext());
|
||||
sInstance = new SocketMsgUtils(context.getApplicationContext(), listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private SocketMsgUtils(Context context) {
|
||||
private SocketMsgUtils(Context context, IMogoCloudSocketOnMessageListener listener) {
|
||||
mContext = context;
|
||||
initSocket();
|
||||
initSocket(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化长连接
|
||||
*/
|
||||
private void initSocket() {
|
||||
private void initSocket(IMogoCloudSocketOnMessageListener listener) {
|
||||
Log.i(TAG, "初始化长连接……");
|
||||
mSocketManager = SocketManager.getInstance();
|
||||
mMessageListener = listener;
|
||||
mSocketManager.init(mContext);
|
||||
mSocketManager.registerOnMessageListener(401017, this);
|
||||
mSocketManager.registerOnMessageListener(401017, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,22 +64,11 @@ public class SocketMsgUtils implements IMogoCloudSocketMsgAckListener,
|
||||
*/
|
||||
public void release() {
|
||||
mSocketManager.release();
|
||||
mSocketManager.unregisterOnMessageListener(401017, this);
|
||||
mSocketManager.unregisterOnMessageListener(401017, mMessageListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAck(long msgId) {
|
||||
Log.i(TAG, "msgId=" + msgId);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<CommandModel> target() {
|
||||
return CommandModel.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsgReceived(CommandModel obj) {
|
||||
Log.i(TAG, "onMsgReceived: obj=" + obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,27 +4,25 @@ 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.model.CommandModel;
|
||||
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.IMogoCloudSocketOnMessageListener;
|
||||
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 {
|
||||
public class LiveStreamUtils implements IMogoCloudSocketOnMessageListener<CommandModel> {
|
||||
private static final String TAG = "LiveStreamUtils";
|
||||
private static final int POLL_TIME = 30 * 1000;
|
||||
// 循环上报摄像头可直播状态间隔时间
|
||||
private static final int PUSH_CAM_TIME = 10 * 60 * 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"; //后置摄像头
|
||||
@@ -37,6 +35,7 @@ public class LiveStreamUtils implements BizMessageListener {
|
||||
private ILiveStatusListener mLiveStatusCallback;
|
||||
private Context mContext;
|
||||
private Handler mHandler;
|
||||
private SocketMsgUtils mSocketMsgUtils;
|
||||
|
||||
public static LiveStreamUtils getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
@@ -56,13 +55,12 @@ public class LiveStreamUtils implements BizMessageListener {
|
||||
initPoll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
private void init() {
|
||||
// 初始化Socket长连接通道
|
||||
SocketMsgUtils.getInstance(mContext);
|
||||
mSocketMsgUtils = SocketMsgUtils.getInstance(mContext, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,28 +100,16 @@ public class LiveStreamUtils implements BizMessageListener {
|
||||
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);
|
||||
}
|
||||
mSocketMsgUtils.uploadCamInfo(msgBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收到长连接指令
|
||||
*
|
||||
* @param str 指令数据
|
||||
* @param videoChannel 指令数据
|
||||
*/
|
||||
private void rtmpPushHandler(String str) {
|
||||
private void rtmpPushHandler(int status, String videoChannel) {
|
||||
try {
|
||||
JSONObject content = new JSONObject(str);
|
||||
int status = content.optInt("status");
|
||||
String videoChannel = content.optString("videoChannel");
|
||||
|
||||
// 前置摄像头
|
||||
if (C1.equals(videoChannel)) {
|
||||
// 停止直播
|
||||
@@ -197,9 +183,20 @@ public class LiveStreamUtils implements BizMessageListener {
|
||||
try {
|
||||
sCam1LiveStatus = 0;
|
||||
sCam2LiveStatus = 0;
|
||||
SocketMsgUtils.getInstance(mContext).release();
|
||||
mSocketMsgUtils.release();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<CommandModel> target() {
|
||||
return CommandModel.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsgReceived(CommandModel obj) {
|
||||
Log.i(TAG, "onMsgReceived: obj=" + obj);
|
||||
rtmpPushHandler(obj.getType(), obj.getVideoChannel());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public class MoGoAiCloudClient {
|
||||
|
||||
// 变量赋值
|
||||
if (mAiCloudClientConfig != null) {
|
||||
mAiCloudClientConfig.setSn(result.sn);
|
||||
mAiCloudClientConfig.setSn("F803EB2046PZD00149");
|
||||
mAiCloudClientConfig.setToken(result.token);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user