5_2-手机开播场景-连麦推流
- 1 Feature Overview
- 2 Usage Demo
- 3 API Introduction
- 4 Implementation Overview
- 5 Subdirectory Overview
1 Feature Overview
The streaming and co-hosting module is a fundamental feature in the mobile live streaming scenario. It supports camera parameter settings, audio parameter settings, video parameter settings, etc. Streaming can be used for instructors to conduct classes, while co-hosting supports both audio and video co-hosting with free selection, and allows multiple participants to join, enriching the classroom content.
Streaming flow: Initialize the streaming engine -> Set streaming configuration parameters -> Start streaming.
Co-hosting flow: The instructor initiates a co-hosting request -> The student raises their hand to apply for co-hosting -> The instructor approves the student's co-hosting request -> The student officially joins the co-hosting session.
2 Usage Demo
The following example demonstrates how to create a streaming and co-hosting layout, and how to call the initialization method. The code is as follows:
// 推流和连麦布局
private IPLVLSStreamerLayout plvlsStreamerLy;
// findView
plvlsStreamerLy = findViewById(R.id.plvls_streamer_ly);
// 初始化推流和连麦布局
plvlsStreamerLy.init(liveRoomDataManager);
Specific use cases of the above methods can be found in the PLVLSLiveStreamerActivity of the polyv demo project.
3 API Introduction
3.1 IPLVLSStreamerLayout
This is an interface encapsulated for the streaming and co-hosting layout in the mobile live streaming scenario, defining methods for external direct calls.
public interface IPLVLSStreamerLayout {
/**
* 初始化
*
* @param liveRoomDataManager 直播间数据管理器
*/
void init(IPLVLiveRoomDataManager liveRoomDataManager);
/**
* 开始上课
*/
void startClass();
/**
* 停止上课
*/
void stopClass();
/**
* 设置推流码率
*
* @param bitrate 码率
*/
void setBitrate(@PLVSStreamerConfig.BitrateType int bitrate);
/**
* 获取推流的码率信息
*
* @return 码率信息<最大支持码率, 选择码率>
*/
Pair<Integer, Integer> getBitrateInfo();
/**
* 是否允许录制声音
*
* @param enable true:允许,false:不允许
*/
boolean enableRecordingAudioVolume(boolean enable);
/**
* 是否允许录制视频/打开摄像头
*
* @param enable true:允许,false:不允许
*/
boolean enableLocalVideo(boolean enable);
/**
* 设置相机方向
*
* @param front true:前置,false:后置
*/
boolean setCameraDirection(boolean front);
/**
* 控制成员列表中的用户加入或离开连麦
*
* @param position 列表中的位置
* @param isAllowJoin true:加入,false:离开
*/
void controlUserLinkMic(int position, boolean isAllowJoin);
/**
* 禁/启用用户媒体
*
* @param position 成员列表中的位置
* @param isVideoType true:视频,false:音频
* @param isMute true:禁用,false:启用
*/
void muteUserMedia(int position, boolean isVideoType, boolean isMute);
/**
* 下麦全体连麦用户
*/
void closeAllUserLinkMic();
/**
* 全体连麦用户禁用/开启声音
*
* @param isMute true:禁用,false:开启
*/
void muteAllUserAudio(boolean isMute);
/**
* 添加推流状态的监听器
*
* @param listener 监听器
*/
void addOnStreamerStatusListener(IPLVOnDataChangedListener<Boolean> listener);
/**
* 添加网络状态监听器
*
* @param listener 监听器
*/
void addOnNetworkQualityListener(IPLVOnDataChangedListener<Integer> listener);
/**
* 添加推流时间监听器
*
* @param listener 监听器
*/
void addOnStreamerTimeListener(IPLVOnDataChangedListener<Integer> listener);
/**
* 因断网延迟20s断流的状态
*
* @param listener 监听器
*/
void addOnShowNetBrokenListener(IPLVOnDataChangedListener<Boolean> listener);
/**
* 添加音频状态监听器
*
* @param listener 监听器
*/
void addOnEnableAudioListener(IPLVOnDataChangedListener<Boolean> listener);
/**
* 添加视频状态监听器
*
* @param listener 监听器
*/
void addOnEnableVideoListener(IPLVOnDataChangedListener<Boolean> listener);
/**
* 添加摄像头方向监听器
*
* @param listener 监听器
*/
void addOnIsFrontCameraListener(IPLVOnDataChangedListener<Boolean> listener);
/**
* 获取网络质量
*
* @return 网络质量常量
*/
int getNetworkQuality();
/**
* 是否推流开始成功
*
* @return true:成功,false:未成功
*/
boolean isStreamerStartSuccess();
/**
* 获取推流和连麦的presenter
*
* @return presenter
*/
IPLVStreamerContract.IStreamerPresenter getStreamerPresenter();
/**
* 销毁,释放资源
*/
void destroy();
}
4 Implementation Overview
4.1 PLVLSStreamerLayout
This class is the streaming and co-hosting layout in the mobile live streaming scenario, implementing the IPLVLSStreamerLayout interface.
The layout includes the instructor's streaming camera view, audio switch status, instructor title, nickname, and UI for other co-hosting audience members' related information.
Below are the main methods involved in this layout.
4.1.1 View Initialization Method
PLVLSStreamerLayout inherits from FrameLayout, and in the constructor, the initView method is used to initialize view.
public PLVLSStreamerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
//填充推流和连麦布局到该view中
LayoutInflater.from(getContext()).inflate(R.layout.plvls_streamer_layout, this);
//推流和连麦的列表视图
plvlsStreamerRv = findViewById(R.id.plvls_streamer_rv);
//init RecyclerView
plvlsStreamerRv.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
plvlsStreamerRv.addItemDecoration(new PLVMessageRecyclerView.SpacesItemDecoration(ConvertUtils.dp2px(8), 0));
//禁用RecyclerView默认动画
plvlsStreamerRv.getItemAnimator().setAddDuration(0);
plvlsStreamerRv.getItemAnimator().setChangeDuration(0);
plvlsStreamerRv.getItemAnimator().setMoveDuration(0);
plvlsStreamerRv.getItemAnimator().setRemoveDuration(0);
RecyclerView.ItemAnimator rvAnimator = plvlsStreamerRv.getItemAnimator();
if (rvAnimator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) rvAnimator).setSupportsChangeAnimations(false);
}
//init adapter
streamerAdapter = new PLVLSStreamerAdapter(plvlsStreamerRv, new PLVLSStreamerAdapter.OnStreamerAdapterCallback() {
@Override
public SurfaceView createLinkMicRenderView() {
return streamerPresenter.createRenderView(Utils.getApp());
}
@Override
public void setupRenderView(SurfaceView surfaceView, String linkMicId) {
streamerPresenter.setupRenderView(surfaceView, linkMicId);
}
});
//启动前台服务
PLVLSForegroundService.startService();
}
4.1.2 Data Initialization Method
The init method of PLVLSStreamerLayout is an external API that requires external input of IPLVLiveRoomDataManager for initialization.
//推流和连麦presenter
private IPLVStreamerContract.IStreamerPresenter streamerPresenter;
@Override
public void init(IPLVLiveRoomDataManager liveRoomDataManager) {
this.liveRoomDataManager = liveRoomDataManager;
//创建推流和连麦mvp-presenter
streamerPresenter = new PLVStreamerPresenter(liveRoomDataManager);
//注册推流和连麦mvp-view
streamerPresenter.registerView(streamerView);
//初始化推流和连麦mvp-presenter
streamerPresenter.init();
}
//创建推流和连麦的mvp-view
private PLVAbsStreamerView streamerView = new PLVAbsStreamerView() {
//...
}
Here, PLVStreamerPresenter is a streaming and co-hosting presenter encapsulated using the mvp pattern. It uses presenter to isolate view and mode. All business logic is operated through presenter, meaning presenter serves as the bridge between the view and data, with the view and data separated at both ends.
4.1.3 Start Class Method
The startClass method of PLVLSStreamerLayout is an external API. Internally, it calls the startLiveStream method of PLVStreamerPresenter to stream to the logged-in channel.
@Override
public void startClass() {
streamerPresenter.startLiveStream();
}
PLVLSStreamerLayout internally holds PLVStreamerPresenter, so in addition to freely calling streaming and co-hosting related methods internally, it can also be encapsulated into interfaces for external calls. The startClass method is one such example.
4.1.4 Stop Class Method
The stop class method is stopClass, which is an external API. Internally, it calls the stopLiveStream method of PLVStreamerPresenter to stop streaming.
@Override
public void stopClass() {
streamerPresenter.stopLiveStream();
}
5 Subdirectory Overview
The subdirectories here refer to the subdirectories under the streaming and co-hosting module in the mobile live streaming scenario, corresponding to the sub-packages under the com.easefun.polyv.livestreamer.modules.streamer module package named polyvLiveStreamerScene in the project.
5.1 adapter Directory
//推流和连麦列表适配器
PLVLSStreamerAdapter
This directory mainly contains adapters related to lists for streaming and co-hosting functionalities.
5.2 service Directory
//前台服务。防止华为机型上锁屏超过1分钟导致摄像头预览画面卡住和断流
PLVLSForegroundService
This directory mainly contains services used by the streaming and co-hosting functionalities.
