# Realize live interactive video streaming
This guide introduces how to implement live interactive video streaming. The API call sequence of live interactive video streaming is shown in the figure below:
# Initialize
Call JCMediaDevice.create (opens new window) and JCCall.create (opens new window) to initialize modules needed to implement group calls:
/// Create a new class and implement it
class JCManager : JCClientCallback, JCMediaDeviceCallback,JCMediaChannelCallbac{
#region JCMediaDeviceCallback
public void onCameraUpdate(){...}
public void onAudioOutputTypeChange(string audioOutputType){...}
#endregion
#region JCMediaChannelCallback
...
/// Achieve methods in JCMediaChannelCallback
...
#endregion
/// Declare object
JCMediaDevice mMediaDevice;
JCMediaChannel mMediaChannel;
/// Initialization function
public bool initialize(Context context) {
/// 1. Media class
mMediaDevice = JCMediaDevice.create(mClient, this);
/// 2. Media channel lass
mMediaChannel = JCMediaChannel.create(client, mediaDevice, this);
}
}
# Role setting
Before joining the channel, you must set the role first. The role setting include the host and audiences. The role value can be customized according to the JCMediaChannel.CustomRole (opens new window) enumeration value, for example:
//Customize the role of the host according to the CustomState enumeration value
JCMediaChannelCustomRole ROLE_BROASCASTER = JCMediaChannelConstants.CUSTOM_ROLE_0;
//Customize the role of audiences according to the CustomState enumeration value
JCMediaChannelCustomRole ROLE_AUDIENCE = JCMediaChannelConstants.CUSTOM_ROLE_1;
Call setCustomRole (opens new window) to set your own role to enter the channel:
/// Set the role; the value of participant (the second parameter) is null, which means that you set your own role
mediaChannel.setCustomRole(ROLE_BROASCASTER, null);
# Join a channel
Call enableUploadAudioStream (opens new window) to enable audio streaming. Call enableUploadVideoStream (opens new window) to enable video streaming:
/// 1. Enable the audio stream mMediaDeviceChannel.enableUploadAudioStream(true); /// 2. Turn on the video stream mMediaDeviceChannel.enableUploadVIdeoStream(true);
To create and join a channel, you need to pass in
channelIdOrUri
and JCMediaChannel.JoinParam (opens new window):channelIdOrUri
refers to the channel ID or channel Uri.In
JCMediaChannelJoinParam
, theuriMode
parameter is set to true, which means the incoming channel Uri. When other parameters are set, it means the incoming channel ID. Users who pass in the same channel ID or the same channel Uri will enter the same Channel.
mMediaChannel.join("222", null);
The onJoin (opens new window) callback triggers after joining the channel:
public void onJoin(bool result, JCMediaChannelReason reason, String channelId) { if (result) { /// Join the channel successfully } else { /// Join the channel failed } }
# Create local video images
After joining a channel, call |getSelfParticipant| in |JCMediaChannel| to get the object of the user in the channel, Then call the startVideo (opens new window) method to open local video preview in JCMediaChannelParticipant (opens new window). The returned object is JCMediaDeviceVideoCanvas (opens new window). (Calling this method will turn on the camera)
/// Open local video preview
mMediaChannel.getSelfParticipant().startVideo(JCMediaChannel.JCMediaDeviceRenderMode.FULLCONTENT, JCMediaChannel.PICTURESIZE_NONE);
# Create remote video images
After entering a channel, call |getParticipants| in |JCMediaChannel| to get all the member objects in the channel.
Call startVideo (opens new window) in the JCMediaChannelParticipant (opens new window) class to get the remote video. The returned object is JCMediaDeviceVideoCanvas (opens new window) .
After the startVideo (opens new window) method is called, you also need to call the requestVideo (opens new window) method in |JCMediaChannel| to request the video streams of other users in the channel.
/// Access all member objects
List<JCMediaChannelParticipant> participants = mMediaChannel.getParticipants();
/// Call the method of creating a video image
participants.get(0).startVideo(JCMediaChannel.JCMediaDeviceRenderMode.FULLCONTENT, JCMediaChannel.PICTURESIZE_NONE);
/// Request remote video stream (call large video window)
mMediaChannel.requestVideo(participants.get(0), Large);
# Leave a channel
Call the leave (opens new window) method to leave the current channel:
mMediaChannel.leave();
In a group video call, you need to call stopVideo (opens new window) to remove the video image when leaving the channel:
mParticipant.stopVideo();
After leaving a channel, you will receive the onLeave (opens new window) callback, and other members will receive the onParticipantLeft (opens new window) callback at the same time:
/// The callback of leaving the ChannelReason
public void onLeave(JCMediaChannelReason reason, String channelId) {
...
/// Destroy the video
mParticipant.stopVideo();
}
# Destroy local and remote video images
After hanging up the video,Call stopVideo (opens new window) in the JCMediaChannelParticipant (opens new window) to destroy local and remote video images.
mParticipant.stopVideo();
# Destroy a channel
If you want to destroy a channel, you can call the following interface, and all members will be quit:
/// End a channel
mMediaChannel.stop();
In a group video call, you need to call stopVideo (opens new window) to remove the video image when leaving the channel:
mParticipant.stopVideo();
After the channel is stopped, the member that initiated the termination receives the onStop (opens new window) callback, and other members receive the onLeave (opens new window) callback at the same time. Please refer to MediaChannelReason (opens new window) for the enumeration value of the reason for failure:
public void onStop(bool result, JCMediaChannelReason reason) {
/// destroy the video, canvas is the instance of JCMediaDeviceVideoCanvas object
mParticipant.stopVideo();
canvas = null;
}