Co-Room Live Streaming
Audience Becoming Broadcaster
When an audience member becomes a broadcaster, they essentially switch roles to become the current room's host and continue live streaming operations.
Switching to Broadcaster Role
When an audience member needs to join the stream and interact with other broadcasters in the room, call the switchLiveRole
method with the parameter RCRTCRole.live_broadcaster
to switch to the broadcaster role.
// Callback for role switching
engine.onLiveRoleSwitched = (RCRTCRole role, int code, String? errMsg) {
if (code == 0) {
// Role switched successfully
} else {
// Role switch failed
}
};
// Call to switch role
engine.switchLiveRole(RCRTCRole.live_broadcaster);
Broadcaster Becoming Audience
When a broadcaster becomes an audience member, they essentially switch roles to become a viewer and continue watching the live stream.
Switching to Audience Role
When in the broadcaster role, call the switchLiveRole
method with the parameter RCRTCRole.live_audience
to switch to the audience role.
If the current broadcaster has joined other rooms via cross-room streaming, they can still successfully switch to the audience role. The SDK will automatically exit all secondary rooms but won't end the current streaming session. To end streaming, call leaveSubRoom
with the disband
parameter set to true
before switching roles.
// Callback for role switching
engine.onLiveRoleSwitched = (RCRTCRole role, int code, String? errMsg) {
if (code == 0) {
// Role switched successfully
} else {
// Role switch failed
}
};
// Call to switch role
engine.switchLiveRole(RCRTCRole.live_audience);
Subscribing to Resources
After switching roles, live streaming users will automatically unsubscribe from all resources. Resubscribe as needed.
- After successfully switching from audience to broadcaster, use the previously saved remote publishing resource information to subscribe to remote resources. See Broadcaster Subscribing to Resources.
- After successfully switching from broadcaster to audience, you can subscribe to the merged stream in the callback for remote merged stream publishing, which will be triggered after the role switch. Alternatively, use the saved remote publishing resource information to subscribe to the broadcaster's individual streams. For details, see:
In-Room Events
When users in the room switch roles using switchLiveRole
, other broadcasters in the same room can monitor the role switch via the onRemoteLiveRoleSwitched
method.
If a user subscribes to a broadcaster's audio/video stream and that broadcaster switches to the audience role, the SDK will automatically unsubscribe from the now-invalid stream. You can use this callback to update the current view or perform other operations.
// Callback for remote user role switching
Function(String roomId, String userId, RCRTCRole role)? onRemoteLiveRoleSwitched;