From db4841a462170ba5a1d28660ec59ded9a5e3b239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=AE=8F=E5=AE=87?= Date: Sun, 7 Feb 2021 19:16:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E4=BA=BA=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/gradle.xml | 1 - .../live/listener/ILiveProgressListener.java | 5 -- .../listener/ILiveRoomPersonListener.java | 28 +++++++++ .../live/manager/LiveStreamManagerImpl.java | 12 +++- .../cloud/live/manager/MoGoLiveManager.java | 58 +++++++++++++------ .../mogo/cloud/live/server/PushService.java | 57 +++++++++++++++--- .../trafficlive/core/TrafficLiveManager.java | 5 -- 7 files changed, 127 insertions(+), 39 deletions(-) create mode 100644 foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveRoomPersonListener.java diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 5674e0c..1f8e3ef 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -26,7 +26,6 @@ diff --git a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveProgressListener.java b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveProgressListener.java index a4c2e5b..034b70e 100644 --- a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveProgressListener.java +++ b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveProgressListener.java @@ -34,11 +34,6 @@ public interface ILiveProgressListener { */ void onDisConnect(); - /** - * 房间在线人数变化 - */ - void onRoomOnlineUserCountUpdate(int count); - /** * 调试错误信息回调 * diff --git a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveRoomPersonListener.java b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveRoomPersonListener.java new file mode 100644 index 0000000..b998540 --- /dev/null +++ b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/listener/ILiveRoomPersonListener.java @@ -0,0 +1,28 @@ +package com.mogo.cloud.live.listener; + +import java.util.ArrayList; + +import im.zego.zegoexpress.constants.ZegoUpdateType; +import im.zego.zegoexpress.entity.ZegoUser; + +/** + * 直播房间状态回调 + */ +public interface ILiveRoomPersonListener { + + /** + * 房间在线人数变化 + * + * @param count 剩余人数 + */ + void onRoomOnlineUserCountUpdate(int count); + + /** + * 房间内其他用户增加或减少的通知回调 + * + * @param updateType 类型 + * @param userList 当前房间内变更的用户列表 + */ + void onRoomUserUpdate(ZegoUpdateType updateType, ArrayList userList); + +} diff --git a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/LiveStreamManagerImpl.java b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/LiveStreamManagerImpl.java index ec8503e..87993f0 100644 --- a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/LiveStreamManagerImpl.java +++ b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/LiveStreamManagerImpl.java @@ -33,6 +33,7 @@ public class LiveStreamManagerImpl implements ILiveStreamManager { private static volatile int sCam1AvailableStatus = 0, sCam2AvailableStatus = 0; private static final int PUSH_START = 0; // 开始 private static final int PUSH_STOP = 1; // 结束 + private static final int PUSH_FORCED_STOP = 2; // 强制结束直播,不考虑是否有观众 private static final String C1 = "C_1"; //前置摄像头 private static final String C2 = "C_2"; //后置摄像头 private Handler mHandler; // 循环上报摄像头状态 @@ -153,13 +154,20 @@ public class LiveStreamManagerImpl implements ILiveStreamManager { if (C1.equals(videoChannel)) { // 停止直播 if (status == PUSH_STOP) { - PushService.startService(mContext, PushService.ACTION_STOP_RTMP_PUSH, LiveStreamManagerImpl.mDevicesId); + PushService.startService(mContext, PushService.ACTION_STOP_RTMP_PUSH, + LiveStreamManagerImpl.mDevicesId); // 释放资源 CameraFrameManager.getInstance().release(); } // 开始直播 else if (status == PUSH_START) { - PushService.startService(mContext, PushService.ACTION_START_RTMP_PUSH, LiveStreamManagerImpl.mDevicesId); + PushService.startService(mContext, PushService.ACTION_START_RTMP_PUSH, + LiveStreamManagerImpl.mDevicesId); + } + // 强制关闭直播 + else if (status == PUSH_FORCED_STOP) { + PushService.startService(mContext, PushService.ACTION_FORCED_STOP_RTMP_PUSH, + LiveStreamManagerImpl.mDevicesId); } sCam1LiveStatus = status; } else { diff --git a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/MoGoLiveManager.java b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/MoGoLiveManager.java index 35420b9..0b51fe1 100644 --- a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/MoGoLiveManager.java +++ b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/manager/MoGoLiveManager.java @@ -3,9 +3,11 @@ package com.mogo.cloud.live.manager; import android.app.Application; import android.os.Environment; import android.text.TextUtils; +import android.util.Log; import android.view.SurfaceView; import com.mogo.cloud.live.listener.ILiveProgressListener; +import com.mogo.cloud.live.listener.ILiveRoomPersonListener; import com.mogo.utils.logger.Logger; import org.json.JSONObject; @@ -82,7 +84,11 @@ public class MoGoLiveManager { /** * 直播进度回调用 */ - private ILiveProgressListener listener; + private ILiveProgressListener mProgressListener; + /** + * 直播房间状态 + */ + private ILiveRoomPersonListener mRoomStatusListener; /** * 当前的房间ID */ @@ -142,6 +148,10 @@ public class MoGoLiveManager { return isCaptureStatus; } + public int getOnlineNumber() { + return onlineNumber; + } + /** * 初始化直播SDK * @@ -173,8 +183,8 @@ public class MoGoLiveManager { public void onDebugError(int errorCode, String funcName, String info) { super.onDebugError(errorCode, funcName, info); Logger.i(TAG, "onDebugError errorCode : " + errorCode); - if (listener != null) { - listener.onDebugError(errorCode, funcName, info); + if (mProgressListener != null) { + mProgressListener.onDebugError(errorCode, funcName, info); } // 停止推送数据 stopPublishingStream(); @@ -185,11 +195,11 @@ public class MoGoLiveManager { public void onEngineStateUpdate(ZegoEngineState state) { super.onEngineStateUpdate(state); Logger.i(TAG, "onEngineStateUpdate state : " + state.name()); - if (listener != null) { + if (mProgressListener != null) { if (state == ZegoEngineState.START) { - listener.onEngineStart(); + mProgressListener.onEngineStart(); } else { - listener.onEngineStop(); + mProgressListener.onEngineStop(); } } } @@ -200,13 +210,13 @@ public class MoGoLiveManager { super.onRoomStateUpdate(roomID, state, errorCode, extendedData); Logger.i(TAG, "onRoomStateUpdate roomID : " + roomID + " state:" + state + " errorCode:" + errorCode); //房间状态更新 - if (listener != null) { + if (mProgressListener != null) { if (state == ZegoRoomState.CONNECTING) { - listener.onConnecting(); + mProgressListener.onConnecting(); } else if (state == ZegoRoomState.CONNECTED) { - listener.onConnected(roomID); + mProgressListener.onConnected(roomID); } else { - listener.onDisConnect(); + mProgressListener.onDisConnect(); } } } @@ -217,8 +227,8 @@ public class MoGoLiveManager { super.onRoomOnlineUserCountUpdate(roomID, count); Logger.i(TAG, "onRoomOnlineUserCountUpdate roomID : " + roomID + " , count : " + count); onlineNumber = count; - if (listener != null) { - listener.onRoomOnlineUserCountUpdate(count); + if (mRoomStatusListener != null) { + mRoomStatusListener.onRoomOnlineUserCountUpdate(count); } } @@ -226,8 +236,11 @@ public class MoGoLiveManager { @Override public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList userList) { super.onRoomUserUpdate(roomID, updateType, userList); - //用户状态更新 Logger.i(TAG, "onRoomUserUpdate roomId : " + roomID + " , updateType : " + updateType.name()); + onlineNumber = userList.size(); + if (mRoomStatusListener != null) { + mRoomStatusListener.onRoomUserUpdate(updateType, userList); + } } // 推流状态回调 @@ -256,7 +269,16 @@ public class MoGoLiveManager { * @param liveProgressListener 监听回调用 */ public void setLiveProgressListener(ILiveProgressListener liveProgressListener) { - this.listener = liveProgressListener; + this.mProgressListener = liveProgressListener; + } + + /** + * 设置直播间的人员监听 + * + * @param roomPersonListener 监听回调用 + */ + public void setLiveRoomPersonListener(ILiveRoomPersonListener roomPersonListener) { + this.mRoomStatusListener = roomPersonListener; } /** @@ -315,8 +337,8 @@ public class MoGoLiveManager { super.onStart(channel); Logger.i(TAG, "setCustomVideoCaptureHandler onStart"); isCaptureStatus = true; - if (listener != null) { - listener.onEngineStart(); + if (mProgressListener != null) { + mProgressListener.onEngineStart(); } } @@ -325,8 +347,8 @@ public class MoGoLiveManager { super.onStop(channel); Logger.i(TAG, "setCustomVideoCaptureHandler onStop"); isCaptureStatus = false; - if (listener != null) { - listener.onEngineStop(); + if (mProgressListener != null) { + mProgressListener.onEngineStop(); } } }); diff --git a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/server/PushService.java b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/server/PushService.java index af72147..612103f 100644 --- a/foudations/mogo-live/src/main/java/com/mogo/cloud/live/server/PushService.java +++ b/foudations/mogo-live/src/main/java/com/mogo/cloud/live/server/PushService.java @@ -8,17 +8,25 @@ import android.os.IBinder; import android.os.SystemClock; import android.util.Log; +import com.mogo.cloud.live.listener.ILiveRoomPersonListener; import com.mogo.cloud.live.listener.IYUVDataListener; import com.mogo.cloud.live.manager.CameraFrameManager; -import com.mogo.cloud.live.manager.MoGoLivePushConfig; import com.mogo.cloud.live.manager.MoGoLiveManager; +import com.mogo.cloud.live.manager.MoGoLivePushConfig; import com.mogo.cloud.live.utils.ByteUtils; +import com.mogo.utils.logger.Logger; + +import java.util.ArrayList; + +import im.zego.zegoexpress.constants.ZegoUpdateType; +import im.zego.zegoexpress.entity.ZegoUser; public class PushService extends Service implements IYUVDataListener { public static final String ACTION_START_RTMP_PUSH = "action_start_rtmp_push"; public static final String ACTION_STOP_RTMP_PUSH = "action_stop_rtmp_push"; + public static final String ACTION_FORCED_STOP_RTMP_PUSH = "action_forced_stop_rtmp_push"; private static final String TAG = "PushService"; @@ -65,13 +73,13 @@ public class PushService extends Service mLivePushConfig.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT); mLivePushConfig.setMute(true); - mLivePusher = MoGoLiveManager.getInstance(); - mLivePusher.init(getApplication(), mLivePushConfig); + initZeGoLivePusher(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { + // 开启直播 if (ACTION_START_RTMP_PUSH.equals(intent.getAction())) { try { String devicesId = intent.getStringExtra(DEVICES_ID); @@ -79,7 +87,17 @@ public class PushService extends Service } catch (Exception e) { e.printStackTrace(); } - } else if (ACTION_STOP_RTMP_PUSH.equals(intent.getAction())) { + } + // 关闭直播,如果还有人观看不关闭 + else if (ACTION_STOP_RTMP_PUSH.equals(intent.getAction())) { + if (mLivePusher.getOnlineNumber() <= 1) { + stopPush(); + } else { + Logger.i(TAG, "房间还有 " + mLivePusher.getOnlineNumber() + " 位观众,不可以关闭直播"); + } + } + // 强制关闭直播 + else if (ACTION_FORCED_STOP_RTMP_PUSH.equals(intent.getAction())) { stopPush(); } } @@ -104,10 +122,7 @@ public class PushService extends Service return; } mDevicesId = devicesId; - if (mLivePusher == null) { - mLivePusher = MoGoLiveManager.getInstance(); - mLivePusher.init(getApplication(), mLivePushConfig); - } + initZeGoLivePusher(); // 登录房间 mLivePusher.loginRoom(mDevicesId, mDevicesId); // 开始发布 @@ -117,6 +132,32 @@ public class PushService extends Service Log.d(TAG, "startPush :mRoomId=" + mDevicesId + " mDevicesId=" + mDevicesId); } + /** + * 初始化ZeGo直播推流 + */ + private void initZeGoLivePusher() { + if (mLivePusher == null) { + mLivePusher = MoGoLiveManager.getInstance(); + mLivePusher.init(getApplication(), mLivePushConfig); +// mLivePusher.setLiveRoomPersonListener(new ILiveRoomPersonListener() { +// @Override +// public void onRoomOnlineUserCountUpdate(int count) { +// Logger.i(TAG, "房间还有 " + count + " 位观众,不可以关闭直播"); +// } +// +// @Override +// public void onRoomUserUpdate(ZegoUpdateType updateType, ArrayList userList) { +// if (userList.size() <= 1) { +// Logger.i(TAG, "房间没有观众了,主动关闭直播"); +// stopPush(); +// } else { +// Logger.i(TAG, "房间还有 " + mLivePusher.getOnlineNumber() + " 位观众,不可以关闭直播"); +// } +// } +// }); + } + } + /** * 结束发布直播视频流 */ diff --git a/modules/mogo-trafficlive/src/main/java/com/mogo/cloud/trafficlive/core/TrafficLiveManager.java b/modules/mogo-trafficlive/src/main/java/com/mogo/cloud/trafficlive/core/TrafficLiveManager.java index 76f834d..fe6d851 100644 --- a/modules/mogo-trafficlive/src/main/java/com/mogo/cloud/trafficlive/core/TrafficLiveManager.java +++ b/modules/mogo-trafficlive/src/main/java/com/mogo/cloud/trafficlive/core/TrafficLiveManager.java @@ -134,11 +134,6 @@ public class TrafficLiveManager implements ILiveProgressListener { callBack.onDisConnect(); } - @Override - public void onRoomOnlineUserCountUpdate(int count) { - - } - @Override public void onDebugError(int errorCode, String funcName, String errorInfo) { Logger.e(TAG, "onDebugError errorCode : " + errorCode + " funcName : " + funcName + " errorInfo : " + errorInfo);