finish the trafficlive module

This commit is contained in:
zhongchao
2021-02-05 17:40:58 +08:00
parent 423e7fba13
commit b5b0eeeae4
6 changed files with 158 additions and 22 deletions

View File

@@ -18,7 +18,7 @@ public interface ILiveProgressListener {
}
/**
* 连接成功
* 正在连接
*/
void onConnecting();

View File

@@ -0,0 +1,8 @@
package com.mogo.cloud.live.listener;
public interface IRequestLiveListener {
void onSuccess();
void onError(Throwable e);
}

View File

@@ -5,6 +5,7 @@ import android.app.Application;
import com.google.gson.Gson;
import com.mogo.cloud.live.constant.LiveConstant;
import com.mogo.cloud.live.listener.ILiveProgressListener;
import com.mogo.cloud.live.listener.IRequestLiveListener;
import com.mogo.cloud.live.model.BaseData;
import com.mogo.cloud.live.model.LivePush;
import com.mogo.cloud.live.network.LiveApiServer;
@@ -44,7 +45,7 @@ public class RequestLiveManager {
return requestLiveManager;
}
public void requestVehicleHeadLive(Application application, String liveSn, ILiveProgressListener liveProgressListener) {
public void requestVehicleHeadLive(Application application, String liveSn, IRequestLiveListener requestLiveListener) {
Gson gson = new Gson();
LivePush livePush = new LivePush(liveSn, "1", "C_1");
String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
@@ -62,14 +63,17 @@ public class RequestLiveManager {
@Override
public void onError(Throwable e) {
if(requestLiveListener != null){
requestLiveListener.onError(e);
}
Logger.e(LiveConstant.TAG, "requestVehicleHeadLive exception : " + e);
}
@Override
public void onNext(BaseData baseData) {
MoGoLiveManager.getInstance().init(application, null);
MoGoLiveManager.getInstance().loginRoom(sn, liveSn);
MoGoLiveManager.getInstance().setLiveProgressListener(liveProgressListener);
if(requestLiveListener != null){
requestLiveListener.onSuccess();
}
}
});
}

View File

@@ -2,8 +2,27 @@ package com.mogo.cloud.trafficlive.api;
public interface ITrafficLiveCallBack {
default String onLiveUrl(){
return null;
default void onStart(){
}
default void onStop(){
}
default void onConnecting(){
}
default void onConnected(){
}
void onLive();
void onDisConnect();
void onError(String errorMsg);
// void onError(int errorCode, String errorMsg);
}

View File

@@ -1,18 +1,53 @@
package com.mogo.cloud.trafficlive.api;
import android.app.Application;
import com.mogo.cloud.trafficlive.core.TrafficLiveManager;
import com.mogo.utils.logger.Logger;
import static com.mogo.cloud.trafficlive.constant.TrafficLiveConstant.TAG;
public class MoGoAiCloudTrafficLive {
/**
* 查看前方车辆直播
*/
public static void viewVehicleHeadLive(ITrafficLiveCallBack callBack) {
public static void viewVehicleHeadLive(Application application, String liveSn, ITrafficLiveCallBack callBack) {
try {
TrafficLiveManager.getInstance().viewVehicleHeadLive(application, liveSn, callBack);
} catch (Exception e) {
Logger.e(TAG, " viewVehicleHeadLive error : " + e);
e.printStackTrace();
}
}
/**
* 查看前方路口直播
*/
public static void viewIntersectionLive(ITrafficLiveCallBack callBack) {
TrafficLiveManager.getInstance().viewIntersectionLive(callBack);
}
/**
* 停止观看直播
*/
public static void stopLive() {
TrafficLiveManager.getInstance().stopLive();
}
/**
* 关闭直播组件
*/
public static void destroyLive(){
TrafficLiveManager.getInstance().destroyLive();
}
/**
* 是否在直播中
*
* @return 直播状态
*/
public static boolean isOnLive() {
return TrafficLiveManager.getInstance().isOnLive();
}
}

View File

@@ -1,16 +1,27 @@
package com.mogo.cloud.trafficlive.core;
import android.app.Application;
import android.text.TextUtils;
import android.view.SurfaceView;
import com.mogo.cloud.live.listener.ILiveProgressListener;
import com.mogo.cloud.live.listener.IRequestLiveListener;
import com.mogo.cloud.live.manager.MoGoLiveManager;
import com.mogo.cloud.live.manager.RequestLiveManager;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.trafficlive.api.ITrafficLiveCallBack;
import com.mogo.utils.logger.Logger;
import static com.mogo.cloud.trafficlive.constant.TrafficLiveConstant.TAG;
public class TrafficLiveManager implements ILiveProgressListener {
private static volatile TrafficLiveManager mInstance;
private RequestLiveManager requestLiveManager;
private ITrafficLiveCallBack callBack;
private SurfaceView surfaceView;
private String mStreamId;
private boolean isLoginSuccess = false;
private TrafficLiveManager() {
}
@@ -26,49 +37,108 @@ public class TrafficLiveManager implements ILiveProgressListener {
return mInstance;
}
public void getVehicleHeadLiveUrl(Application application, String liveSn, ITrafficLiveCallBack callBack) {
if (callBack == null) {
this.callBack = callBack;
}
requestLiveManager.requestVehicleHeadLive(application, liveSn, this);
public boolean isOnLive() {
return MoGoLiveManager.getInstance().isPlaying();
}
public void getIntersectionLiveUrl(ITrafficLiveCallBack callBack) {
public void viewVehicleHeadLive(Application application, String liveSn, SurfaceView surfaceView, ITrafficLiveCallBack trafficLiveCallBack) throws Exception {
if (trafficLiveCallBack == null) {
throw new Exception("ITrafficLiveCallBack can not be null");
}
if (surfaceView == null) {
throw new Exception("SurfaceView can not be null");
}
if (TextUtils.isEmpty(liveSn)) {
throw new Exception("liveSn can not be null");
}
if (!isLoginSuccess) {
callBack.onError("暂未进房,请勿重复请求");
return;
}
if (MoGoLiveManager.getInstance().isPlaying()) {
callBack.onError("正在直播中,请勿重复请求");
return;
}
this.surfaceView = surfaceView;
this.callBack = trafficLiveCallBack;
requestLiveManager.requestVehicleHeadLive(application, liveSn, new IRequestLiveListener() {
@Override
public void onSuccess() {
String sn = MoGoAiCloudClient.getInstance().getAiCloudClientConfig().getSn();
mStreamId = MoGoLiveManager.STREAM_ID_PREFIX + liveSn;
MoGoLiveManager.getInstance().init(application, null);
MoGoLiveManager.getInstance().loginRoom(sn, liveSn);
MoGoLiveManager.getInstance().setLiveProgressListener(TrafficLiveManager.this);
}
@Override
public void onError(Throwable e) {
callBack.onError(e.getMessage());
}
});
}
public void viewIntersectionLive(ITrafficLiveCallBack callBack) {
}
public void stopLive() {
MoGoLiveManager.getInstance().stopLive(mStreamId);
surfaceView = null;
callBack = null;
isLoginSuccess = false;
mStreamId = null;
}
public void destroyLive() {
MoGoLiveManager.getInstance().onDestroyLive();
}
@Override
public void onStart() {
callBack.onStart();
}
@Override
public void onStop() {
callBack.onStop();
}
@Override
public void onConnecting() {
callBack.onConnecting();
}
@Override
public void onConnected(String roomId) {
isLoginSuccess = true;
callBack.onConnected();
}
@Override
public void onDisConnect() {
isLoginSuccess = false;
callBack.onDisConnect();
}
@Override
public void onRoomStreamUpdate(String streamId, boolean isLive) {
if (streamId != null && isLive) {
Logger.i(TAG, "主播开始直播了");
mStreamId = streamId;
MoGoLiveManager.getInstance().startLive(mStreamId, surfaceView);
callBack.onLive();
} else {
Logger.i(TAG, "主播已离线");
callBack.onStop(); //todo 验证 onStop() 与 onRoomStreamUpdate() 回调时机 前后顺序
}
}
@Override
public void onDebugError(int errorCode, String funcName, String errorInfo) {
Logger.e(TAG, "onDebugError errorCode : " + errorCode + " funcName : " + funcName + " errorInfo : " + errorInfo);
callBack.onError(errorInfo);
}
}