修复中间停止推送后,再次开启推送异常

This commit is contained in:
董宏宇
2021-02-03 18:19:06 +08:00
parent b1c8c0fa3c
commit 1c655fb208
3 changed files with 96 additions and 74 deletions

View File

@@ -55,6 +55,7 @@ public class ZeGoLiveManager {
* 存储即构的日志路径
*/
private static final String ZEGO_LOG_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZegoLog";
private Application mApplication;
/**
* 直播推送配置对象
*/
@@ -87,6 +88,10 @@ public class ZeGoLiveManager {
* 当前的房间ID
*/
private String currentRoomId = "";
/**
* 自定义采集状态true-初始化完成false-没有初始化
*/
private boolean isCaptureStatus;
/**
* 推送状态true-推流中false-没有推流
*/
@@ -126,6 +131,10 @@ public class ZeGoLiveManager {
return isPushing;
}
public boolean isCaptureStatus() {
return isCaptureStatus;
}
/**
* 初始化直播SDK
*
@@ -133,6 +142,7 @@ public class ZeGoLiveManager {
* @param livePushConfig 是否需要配置
*/
public void init(Application application, MGLivePushConfig livePushConfig) {
mApplication = application;
mLivePushConfig = livePushConfig;
// 通过 advancedConfig 设置 uuid 过滤字段,设置之后 SDK 只会抛出前 12 个字节为开发者所设置 uuid 的 SEI
ZegoEngineConfig zegoEngineConfig = new ZegoEngineConfig();
@@ -144,61 +154,6 @@ public class ZeGoLiveManager {
zegoEngineConfig.logConfig = zegoLogConfig;
// 设置配置到引擎中
ZegoExpressEngine.setEngineConfig(zegoEngineConfig);
// 创建 enging 对象, appID, appSign 为开发者在 ZEGO 管理控制台申请的凭证信息,
// 未上线的开发者 isTestEnvironment 为 true, application 为安卓应用的上下文
mExpressEngine =
ZegoExpressEngine.createEngine(
appId,
appKey,
true,
ZegoScenario.GENERAL,
application,
mEventHandler);
if (mLivePushConfig != null) {
// 创建自定义视频采集对象
customVideoCaptureConfig = new ZegoCustomVideoCaptureConfig();
// 设置自定义视频采集视频帧数据类型
customVideoCaptureConfig.bufferType = ZegoVideoBufferType.RAW_DATA;
// true 表示静音(关闭)
mExpressEngine.muteMicrophone(true);
// 开始或停止自定义视频采集,支持设置其他通道的推流
mExpressEngine.enableCustomVideoCapture(true, customVideoCaptureConfig, ZegoPublishChannel.MAIN);
// 设置自定义视频采集回调
mExpressEngine.setCustomVideoCaptureHandler(new IZegoCustomVideoCaptureHandler() {
@Override
public void onStart(ZegoPublishChannel channel) {
super.onStart(channel);
Log.i(TAG, "setCustomVideoCaptureHandler onStart");
if (listener != null) {
listener.onStart();
}
}
@Override
public void onStop(ZegoPublishChannel channel) {
super.onStop(channel);
Log.i(TAG, "setCustomVideoCaptureHandler onStop");
if (listener != null) {
listener.onStop();
}
}
});
// 视频配追
ZegoVideoConfig zegoVideoConfig = new ZegoVideoConfig();
// 采集分辨率控制摄像头图像采集的分辨率。SDK 要求将宽和高设置为偶数。
zegoVideoConfig.setCaptureResolution(mLivePushConfig.getWidth(), mLivePushConfig.getHeight());
// 编码分辨率控制编码器编码推流的图像分辨率。SDK 要求将宽和高设置为偶数。推流前后设置均可生效
zegoVideoConfig.setEncodeResolution(mLivePushConfig.getWidth(), mLivePushConfig.getHeight());
// 设置视频帧率
zegoVideoConfig.setVideoFPS(mLivePushConfig.getVideoFPS());
// 设置视频比特率
zegoVideoConfig.setVideoBitrate(mLivePushConfig.getVideoBitrate());
// 设置转码的ID使用H.264
zegoVideoConfig.setCodecID(ZegoVideoCodecID.DEFAULT);
// 将视频信息设置到推流引擎
mExpressEngine.setVideoConfig(zegoVideoConfig);
}
}
/**
@@ -209,6 +164,7 @@ public class ZeGoLiveManager {
@Override
public void onDebugError(int errorCode, String funcName, String info) {
super.onDebugError(errorCode, funcName, info);
Log.i(TAG, "onDebugError errorCode : " + errorCode);
if (listener != null) {
listener.onDebugError(errorCode, funcName, info);
}
@@ -235,6 +191,7 @@ public class ZeGoLiveManager {
@Override
public void onRoomStateUpdate(String roomID, ZegoRoomState state, int errorCode, JSONObject extendedData) {
super.onRoomStateUpdate(roomID, state, errorCode, extendedData);
Log.i(TAG, "onRoomStateUpdate roomID : " + roomID + " state:" + state + " errorCode:" + errorCode);
//房间状态更新
if (state == ZegoRoomState.CONNECTING) {
if (listener != null) {
@@ -255,14 +212,14 @@ public class ZeGoLiveManager {
public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
super.onRoomUserUpdate(roomID, updateType, userList);
//用户状态更新
Log.i(TAG, "onRoomUserUpdate roomId 房间内其他用户增加或减少的通知回调: " + roomID + " , updateType : " + updateType.name());
Log.i(TAG, "onRoomUserUpdate roomId : " + roomID + " , updateType : " + updateType.name());
}
@Override
public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList, JSONObject extendedData) {
super.onRoomStreamUpdate(roomID, updateType, streamList, extendedData);
//有用户新推送或者删除音视频时,更新流状态
Log.i(TAG, "onRoomStreamUpdate roomId 相同房间内其他用户推的流增加或减少的通知: " + roomID + " , ZegoUpdateType : " + updateType.name());
Log.i(TAG, "onRoomStreamUpdate roomId : " + roomID + " , ZegoUpdateType : " + updateType.name());
for (ZegoStream stream : streamList) {
String streamID = stream.streamID;
Log.i(TAG, "onRoomStreamUpdate streamId: " + streamID);
@@ -278,21 +235,21 @@ public class ZeGoLiveManager {
public void onPublisherStateUpdate(String streamID, ZegoPublisherState state,
int errorCode, JSONObject extendedData) {
super.onPublisherStateUpdate(streamID, state, errorCode, extendedData);
Log.i(TAG, "onPublisherStateUpdate streamID 推流状态回调: " + streamID + " , state : " + state.name() + " , errorCode : " + errorCode);
Log.i(TAG, "onPublisherStateUpdate streamID : " + streamID + " , state : " + state.name() + " , errorCode : " + errorCode);
isPushing = state == ZegoPublisherState.PUBLISHING;
}
@Override
public void onPlayerStateUpdate(String streamID, ZegoPlayerState state, int errorCode, JSONObject extendedData) {
super.onPlayerStateUpdate(streamID, state, errorCode, extendedData);
Log.i(TAG, "onPlayerStateUpdate streamId 拉流状态变更回调: " + streamID + " , state : " + state.name() + " , errorCode : " + errorCode + " , extendData : " + extendedData.toString());
Log.i(TAG, "onPlayerStateUpdate streamId : " + streamID + " , state : " + state.name() + " , errorCode : " + errorCode + " , extendData : " + extendedData.toString());
isPlaying = state == ZegoPlayerState.PLAYING;
}
@Override
public void onPlayerQualityUpdate(String streamID, ZegoPlayStreamQuality quality) {
super.onPlayerQualityUpdate(streamID, quality);
Log.i(TAG, "onPlayerQualityUpdate quality 拉流质量回调: " + quality.toString());
Log.i(TAG, "onPlayerQualityUpdate quality : " + quality.toString());
}
@Override
@@ -329,11 +286,74 @@ public class ZeGoLiveManager {
* @param roomId 要进入的房间ID
*/
public void loginRoom(String userId, String roomId) {
initCustomVideoCapture();
currentRoomId = ROOM_ID_PREFIX + roomId;
ZegoUser zegoUser = new ZegoUser(userId, NAME_PREFIX + userId);
mExpressEngine.loginRoom(currentRoomId, zegoUser);
}
/**
* 初始化自定义采集
*/
private void initCustomVideoCapture() {
// 创建 enging 对象, appID, appSign 为开发者在 ZEGO 管理控制台申请的凭证信息,
// 未上线的开发者 isTestEnvironment 为 true, application 为安卓应用的上下文
mExpressEngine = ZegoExpressEngine.createEngine(
appId,
appKey,
true,
ZegoScenario.GENERAL,
mApplication,
mEventHandler);
if (mLivePushConfig != null) {
// 创建自定义视频采集对象
customVideoCaptureConfig = new ZegoCustomVideoCaptureConfig();
// 设置自定义视频采集视频帧数据类型
customVideoCaptureConfig.bufferType = ZegoVideoBufferType.RAW_DATA;
// true 表示静音(关闭)
mExpressEngine.muteMicrophone(true);
// 开始或停止自定义视频采集,支持设置其他通道的推流
mExpressEngine.enableCustomVideoCapture(true, customVideoCaptureConfig, ZegoPublishChannel.MAIN);
// 设置自定义视频采集回调
mExpressEngine.setCustomVideoCaptureHandler(new IZegoCustomVideoCaptureHandler() {
@Override
public void onStart(ZegoPublishChannel channel) {
super.onStart(channel);
Log.i(TAG, "setCustomVideoCaptureHandler onStart");
isCaptureStatus = true;
if (listener != null) {
listener.onStart();
}
}
@Override
public void onStop(ZegoPublishChannel channel) {
super.onStop(channel);
Log.i(TAG, "setCustomVideoCaptureHandler onStop");
isCaptureStatus = false;
if (listener != null) {
listener.onStop();
}
}
});
// 视频配追
ZegoVideoConfig zegoVideoConfig = new ZegoVideoConfig();
// 采集分辨率控制摄像头图像采集的分辨率。SDK 要求将宽和高设置为偶数。
zegoVideoConfig.setCaptureResolution(mLivePushConfig.getWidth(), mLivePushConfig.getHeight());
// 编码分辨率控制编码器编码推流的图像分辨率。SDK 要求将宽和高设置为偶数。推流前后设置均可生效
zegoVideoConfig.setEncodeResolution(mLivePushConfig.getWidth(), mLivePushConfig.getHeight());
// 设置视频帧率
zegoVideoConfig.setVideoFPS(mLivePushConfig.getVideoFPS());
// 设置视频比特率
zegoVideoConfig.setVideoBitrate(mLivePushConfig.getVideoBitrate());
// 设置转码的ID使用H.264
zegoVideoConfig.setCodecID(ZegoVideoCodecID.DEFAULT);
// 将视频信息设置到推流引擎
mExpressEngine.setVideoConfig(zegoVideoConfig);
}
}
/**
* 退出房间
*/
@@ -381,13 +401,15 @@ public class ZeGoLiveManager {
* @param referenceTimeMillisecond 视频帧参考时间UNIX时间戳以毫秒为单位。音视频同步用的
*/
public void startPublishingStream(ByteBuffer byteBuffer, int dataLength, long referenceTimeMillisecond) {
ZegoVideoFrameParam zegoVideoFrameParam = new ZegoVideoFrameParam();
zegoVideoFrameParam.format = ZegoVideoFrameFormat.I420;
zegoVideoFrameParam.width = mLivePushConfig.getWidth();
zegoVideoFrameParam.height = mLivePushConfig.getHeight();
zegoVideoFrameParam.strides[0] = mLivePushConfig.getWidth();
zegoVideoFrameParam.strides[1] = mLivePushConfig.getWidth();
mExpressEngine.sendCustomVideoCaptureRawData(byteBuffer, dataLength, zegoVideoFrameParam, referenceTimeMillisecond);
if (mExpressEngine != null) {
ZegoVideoFrameParam zegoVideoFrameParam = new ZegoVideoFrameParam();
zegoVideoFrameParam.format = ZegoVideoFrameFormat.I420;
zegoVideoFrameParam.width = mLivePushConfig.getWidth();
zegoVideoFrameParam.height = mLivePushConfig.getHeight();
zegoVideoFrameParam.strides[0] = mLivePushConfig.getWidth();
zegoVideoFrameParam.strides[1] = mLivePushConfig.getWidth();
mExpressEngine.sendCustomVideoCaptureRawData(byteBuffer, dataLength, zegoVideoFrameParam, referenceTimeMillisecond);
}
}
/**

View File

@@ -97,16 +97,18 @@ public class PushService extends Service implements IYUVDataListener {
*/
private void startPush(String devicesId) {
if (mDevicesId != null && mDevicesId.equals(devicesId)
&& mLivePusher != null && mLivePusher.isPushing()) {
&& mLivePusher != null
&& mLivePusher.isCaptureStatus()
&& mLivePusher.isPushing()) {
return;
}
mDevicesId = devicesId;
// 注册视频YUV回调监听
CameraFrameManager.getInstance().addYuvDataCallback(this);
// 登录房间
mLivePusher.loginRoom(mDevicesId, mDevicesId);
// 开始发布
mLivePusher.startPush(mDevicesId);
// 注册视频YUV回调监听
CameraFrameManager.getInstance().addYuvDataCallback(this);
Log.d(TAG, "startPush :mRoomId=" + mDevicesId + " mDevicesId=" + mDevicesId);
}
@@ -130,7 +132,8 @@ public class PushService extends Service implements IYUVDataListener {
@Override
public void onFrame(byte[] data) {
if (mLivePusher != null && mLivePusher.isPushing() && data != null) {
if (mLivePusher != null && mLivePusher.isCaptureStatus()
&& mLivePusher.isPushing() && data != null) {
// 将YUV数据发布到即构
ZeGoLiveManager.getInstance().startPublishingStream(
ByteUtils.getBuffer(data, data.length), data.length,

View File

@@ -1,8 +1,5 @@
<<<<<<< HEAD
include ':modules:mogo-trafficlive'
=======
include ':foudations:mogo-common'
>>>>>>> f325f43171aed36ba51557ef5340f1758bdb1543
include ':foudations:mogo-live'
include ':foudations:mogo-socket'
include ':modules:mogo-realtime'