This topic describes how to use Real-Time Communication (RTC) SDK to host an interactive class between a teacher and students.
Teacher side
- Before a teacher joins the channel, call SetChannelProfile to set the channel mode to interactive.
pEngine->setChannelProfile(AliRtcChannelProfileInteractiveLive); - Before the teacher joins the channel, call SetClientRole to set the role to interactive.
pEngine->setClientRole(AliRtcClientRoleInteractive);Note You can also set the role after joining a channel. If the role changes, the OnUpdateRoleNotify callback is triggered after the role is successfully set. - Before the teacher joins the channel, set the client to subscribe to audio and video streams by default.
// Configure default subscription to audio streams. Enable as needed. pEngine->SetDefaultSubscribeAllRemoteAudioStreams(true); // Configure default subscription to video streams. Enable as needed. pEngine->SetDefaultSubscribeAllRemoteVideoStreams(true); - Call JoinChannel to join the channel.
pEngine->joinChannel(aliRtcAuthInfo, userName); - Start publishing streams.
// Configure to publish the audio stream. Enable as needed. pEngine->PublishLocalAudioStream(true); // Configure to publish the camera stream. Enable as needed. pEngine->PublishLocalVideoStream(true); // Configure to publish the screen stream. Enable as needed. pEngine->StartScreenShareByDesktopId(desktopId, config);Name Type Description desktopId unsigned int The desktop ID. Obtain it by calling the GetScreenShareSourceInfo interface. config const AliEngineScreenShareConfig & Screen sharing configuration. - After you publish the stream, use the callback to verify that the audio stream was published successfully.
void OnAudioPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you publish the stream, use the callback to verify that the video stream was published successfully.
void OnVideoPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you publish the stream, use the callback to verify that the screen sharing stream was published successfully.
void OnScreenSharePublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - Receive a callback when a remote user starts or stops publishing a stream. (The student client receives the same callback as the teacher client).
/** * @brief This message is returned when a remote user's stream changes. * @param uid The user ID. A unique identifier assigned by the app server. * @param audioTrack The audio stream type. For more information, see AliRtcAudioTrack. * @param videoTrack The video stream type. For more information, see AliRtcVideoTrack. * @note This message is also sent when a remote user stops publishing a stream. */ virtual void onRemoteTrackAvailableNotify(const AliRtc::String & uid, AliRtcAudioTrack audioTrack, AliRtcVideoTrack videoTrack)Note- If `audioTrack` is `AliRtcAudioTrackNo` and `videoTrack` is `AliRtcVideoTrackNo`, the remote user has stopped publishing streams. Otherwise, the values indicate the publishing status of the remote user's stream.
- You can also set the role after joining the channel. You will receive a callback after the role is successfully set.
void onUpdateRoleNotify(const AliRtcClientRole old_role, const AliRtcClientRole new_role) - Note the current value of the role parameter. For example, if the current role is `AliRtcClientRoleInteractive`, calling the interface again with the same value does not trigger a callback.
pEngine->setClientRole(AliRtcClientRoleInteractive); - Separate the logic for switching roles from the logic for publishing and subscribing to streams.
Student side
- Before a student joins the channel, call SetChannelProfile to set the channel mode to interactive.
pEngine->setChannelProfile(AliRtcChannelProfileInteractiveLive);Note- Regular viewer: After you set the mode to interactive, the default role is viewer. A user with the viewer role can only subscribe to and watch the live stream, but cannot publish streams. This role is suitable for regular viewers and requires no other operations.
- Interactive viewer: A user with the interactive participant role can publish streams. For more information, see the Go on-mic section below.
- Call JoinChannel to join the channel.
pEngine->JoinChannel(aliRtcAuthInfo, userName); - Go on-mic. (A regular viewer must first switch their user role to go on-mic.)
- Switch the role to `AliRtcClientRoleInteractive`. (You must do this before you call the publish interface to publish a stream.)
pEngine->setClientRole(AliRtcClientRoleInteractive); - After the role is switched successfully, you will receive a callback.
void onUpdateRoleNotify(const AliRtcClientRole old_role, const AliRtcClientRole new_role) {}; - After you receive the callback, start publishing streams.
// Configure to publish the audio stream. Enable as needed. pEngine->PublishLocalAudioStream(true); // Configure to publish the camera stream. Enable as needed. pEngine->PublishLocalVideoStream(true); - After you publish the stream, use the callback to verify that the audio stream was published successfully.
void OnAudioPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you publish the stream, use the callback to verify that the video stream was published successfully.
void OnVideoPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you publish the stream, use the callback to verify that the screen sharing stream was published successfully.
void OnScreenSharePublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID.
- Switch the role to `AliRtcClientRoleInteractive`. (You must do this before you call the publish interface to publish a stream.)
- Go off-mic. This is the reverse of going on-mic. To become a regular viewer, an interactive viewer must first stop publishing streams and then switch their role.
- Stop publishing streams.
// Configure to unpublish the audio stream. Disable as needed. pEngine->PublishLocalAudioStream(false); // Configure to unpublish the camera stream. Disable as needed. pEngine->PublishLocalVideoStream(false); - After you stop publishing the stream, use the callback to verify that the audio stream was unpublished successfully.
void OnAudioPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you stop publishing the stream, use the callback to verify that the video stream was unpublished successfully.
void OnVideoPublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you stop publishing the stream, use the callback to verify that screen sharing was unpublished successfully.
void OnScreenSharePublishStateChanged(AliEnginePublishState oldState, AliEnginePublishState newState, int elapseSinceLastState, const char *channel);Name Type Description oldState AliEnginePublishState The previous publishing state. newState AliEnginePublishState The current publishing state. elapseSinceLastState int The time interval since the last state change. channel const char * The current channel ID. - After you stop publishing the stream, switch the role to a regular viewer.
pEngine->setClientRole(AliRtcClientRoleLive); - After the role is switched successfully, you will receive a callback. You can then continue to subscribe to and watch streams.
void onUpdateRoleNotify(const AliRtcClientRole old_role, const AliRtcClientRole new_role)
- Stop publishing streams.
该文章对您有帮助吗?