加入了多房间登录操作来解决同一设备推流同时拉流

This commit is contained in:
董宏宇
2021-03-02 18:30:44 +08:00
parent 5e5980886d
commit c7b610efc8
13 changed files with 429 additions and 119 deletions

View File

@@ -43,12 +43,12 @@
android:label="配置信息"
android:launchMode="singleTask" />
<activity
android:name=".LivePushActivity"
android:label="直播播放"
android:name=".LivePlayAndPushActivity"
android:label="直播 推送 和 播放"
android:launchMode="singleTask" />
<activity
android:name=".LivePlayActivity"
android:label="直播推送"
android:label="直播播放"
android:launchMode="singleTask" />
<activity
android:name="com.mogo.cloud.network.NetworkActivity"

View File

@@ -5,10 +5,11 @@ import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.mogo.cloud.util.YuvToolUtils;
import com.zhidao.manager.camera.FrameBufferCallBack;
import com.zhidao.manager.camera.ZDCameraManager;
@@ -34,16 +35,25 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
// 当前直播的状态
protected ToggleButton tbLiveStatus;
// 相机数据预览
protected SurfaceView surfaceView;
protected SurfaceView surfacePreviewView;
// 查看指定车机的画面
protected SurfaceView surfacePlayView;
// 查看直播按钮
protected ToggleButton liveToggleBtn;
// 要查看的车机SN
protected EditText etLookRoomId;
// WIFI状态
protected TextView tvWifiState;
private ZDCameraManager zdCameraManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_push);
setContentView(R.layout.activity_live_play_and_push);
surfaceView = findViewById(R.id.surfaceView);
surfacePreviewView = findViewById(R.id.surfacePreviewView);
btnLive = findViewById(R.id.btnLive);
btnLive.setOnCheckedChangeListener((buttonView, isChecked) -> {
Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
@@ -67,6 +77,15 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
tbLiveStatus = findViewById(R.id.tbLiveStatus);
surfacePlayView = findViewById(R.id.surfacePlayView);
etLookRoomId = findViewById(R.id.etLookRoomId);
liveToggleBtn = findViewById(R.id.liveToggleBtn);
liveToggleBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
Toast.makeText(getApplicationContext(), buttonView.getText(), Toast.LENGTH_SHORT).show();
togglePlay(isChecked);
});
tvWifiState = findViewById(R.id.tvWifiState);
initCamer();
}
@@ -106,7 +125,7 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
});
// 这里是纯预览
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
surfacePreviewView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "addSurfaceCallBack id");
@@ -157,4 +176,11 @@ public abstract class BaseLiveActivity extends AppCompatActivity {
*/
public abstract void toggleCameraState(boolean isLive);
/**
* 开关查看直播
*
* @param isPlay true-查看直播false-不查看直播
*/
public abstract void togglePlay(boolean isPlay);
}

View File

@@ -0,0 +1,126 @@
package com.mogo.cloud;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.util.Log;
import com.mogo.cloud.live.listener.ILiveStatusListener;
import com.mogo.cloud.live.manager.ILiveStreamManager;
import com.mogo.cloud.live.manager.LiveStreamManagerImpl;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.mogo.cloud.trafficlive.api.ITrafficLiveCallBack;
import com.mogo.cloud.trafficlive.api.MoGoAiCloudTrafficLive;
import com.mogo.cloud.wifi.IWifiStateListener;
import com.mogo.cloud.wifi.WifiStateManager;
/**
* 推流和拉流页面
*/
public class LivePlayAndPushActivity extends BaseLiveActivity implements ITrafficLiveCallBack, IWifiStateListener {
public static final String TAG = "PushActivity";
private String liveSn;
ILiveStreamManager liveStreamManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WifiStateManager.getInstance().registerWifiStateListener(this);
// 初始化直播流管理
liveStreamManager = LiveStreamManagerImpl.getInstance(this.getApplication(),
MoGoAiCloudClientConfig.getInstance().getThirdPartyDeviceId());
// 设置状态回调
liveStreamManager.setLiveStatusChangeCallback(new ILiveStatusListener() {
@Override
public void onChange(String camId, int status) {
tbLiveStatus.post(new Runnable() {
@Override
public void run() {
if (status == 0) {
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStartLive));
tbLiveStatus.setChecked(true);
} else {
tbLiveStatus.setTextColor(getResources().getColor(R.color.colorStopLive));
tbLiveStatus.setChecked(false);
}
}
});
}
});
}
@Override
public void onVideoFrame(byte[] bytes, int bytesLength) {
//Log.i(TAG, "onVideoFrame byte length: " + bytesLength);
if (liveStreamManager != null) {
// 将摄像头采集的YUV数据推送到ZEGO
liveStreamManager.notifyYUVData(bytes, 1280, 720, 3);
}
}
@Override
public void toggleLive(boolean isLive) {
}
@Override
public void toggleCameraState(boolean isLive) {
Log.i(TAG, "toggleCameraState isLive: " + isLive);
if (isLive) {
// 上报摄像头状态1-可用2-不可用
liveStreamManager.uploadCamStatus(1, 1);
} else {
liveStreamManager.uploadCamStatus(2, 2);
}
}
@Override
public void togglePlay(boolean isPlay) {
if (isPlay) {
liveSn = etLookRoomId.getText().toString().trim();
MoGoAiCloudTrafficLive.viewVehicleHeadLive(this.getApplication(),
liveSn, surfacePlayView, this);
} else {
MoGoAiCloudTrafficLive.stopLive(liveSn);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (liveStreamManager != null) {
// 停止
liveStreamManager.stopLiveStream();
// 释放资源
liveStreamManager.release();
}
MoGoAiCloudTrafficLive.stopLive(liveSn);
MoGoAiCloudTrafficLive.destroyLive();
WifiStateManager.getInstance().unRegisterWifiStateListener(this);
}
@Override
public void onLive() {
Log.d(TAG, "开始直播 onLive");
}
@Override
public void onDisConnect() {
Log.d(TAG, "失去连接 onDisConnect");
}
@Override
public void onError(String errorMsg) {
Log.d(TAG, "发生错误 onError msg: " + errorMsg);
}
@Override
public void onWifiState(WifiInfo wifiInfo) {
tvWifiState.setText("wifiLevel: " + Math.abs(wifiInfo.getRssi()));
}
}

View File

@@ -21,8 +21,10 @@ public class LivePushActivity extends BaseLiveActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化直播流管理
liveStreamManager = LiveStreamManagerImpl.getInstance(this,
liveStreamManager = LiveStreamManagerImpl.getInstance(this.getApplication(),
MoGoAiCloudClientConfig.getInstance().getThirdPartyDeviceId());
// 设置状态回调
liveStreamManager.setLiveStatusChangeCallback(new ILiveStatusListener() {
@Override
public void onChange(String camId, int status) {
@@ -67,6 +69,11 @@ public class LivePushActivity extends BaseLiveActivity {
}
}
@Override
public void togglePlay(boolean isPlay) {
}
@Override
protected void onDestroy() {
super.onDestroy();

View File

@@ -7,7 +7,6 @@ import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;
import com.mogo.cloud.network.NetworkActivity;
import com.mogo.cloud.passport.IMoGoTokenCallback;
import com.mogo.cloud.passport.MoGoAiCloudClient;
@@ -19,6 +18,7 @@ public class MainActivity extends AppCompatActivity {
private Button btnJumpNetWorkPort;
private Button btnJumpRealTime;
private Button btnJumpRoadCondition;
private Button btnJumpLivePlayAndPush;
private Button btnJumpLivePush;
private Button btnJumpLivePlay;
@@ -66,6 +66,12 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
});
btnJumpLivePlayAndPush = findViewById(R.id.btnJumpLivePlayAndPush);
btnJumpLivePlayAndPush.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, LivePlayAndPushActivity.class);
startActivity(intent);
});
btnJumpLivePush = findViewById(R.id.btnJumpLivePush);
btnJumpLivePush.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, LivePushActivity.class);

View File

@@ -14,7 +14,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要查看的车机SN"
android:text="F803BB2037EZD00071"
android:text="F803EB2046PZD00149"
android:textColor="#FFFF"
app:layout_constraintBottom_toTopOf="@+id/liveToggleBtn"
app:layout_constraintEnd_toEndOf="parent"

View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surfacePreviewView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/surfacePlayView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SurfaceView
android:id="@+id/surfacePlayView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/surfacePreviewView"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/tbLiveStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFF"
android:padding="10dp"
android:textOff="直播已停止"
android:textOn="直播进行中"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/flTestPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ToggleButton
android:id="@+id/btnLive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textOff="@string/start"
android:textOn="@string/stop" />
<ToggleButton
android:id="@+id/btnChangeCameraState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textOff="设置不可直播"
android:textOn="设置可直播" />
<ToggleButton
android:id="@+id/btnSaveFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textOff="开始录制"
android:textOn="停止录制"
android:visibility="gone" />
</LinearLayout>
<EditText
android:id="@+id/etLookRoomId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入要查看的车机SN"
android:text="F803EB2046PZD00149"
android:textColor="#FFFF"
app:layout_constraintBottom_toTopOf="@+id/liveToggleBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/surfacePlayView" />
<ToggleButton
android:id="@+id/liveToggleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="开始拉流"
android:textOn="停止拉流"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/etLookRoomId" />
<TextView
android:id="@+id/tvWifiState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wifi State :0"
android:textColor="#17FBF4"
android:textSize="32px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

View File

@@ -72,16 +72,25 @@
android:layout_height="match_parent"
android:text="路况服务测试" />
<Button
android:id="@+id/btnJumpLivePlayAndPush"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="直播SDK 推送 和 播放" />
<Button
android:id="@+id/btnJumpLivePush"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="直播SDK推流测试" />
android:text="直播SDK推流测试"
android:visibility="gone" />
<Button
android:id="@+id/btnJumpLivePlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="直播SDK观看测试" />
android:text="直播SDK观看测试"
android:visibility="gone" />
</LinearLayout>
</ScrollView>