Hi yangjianyi
Thanks for using Azure. Yes, both the caller and the receiver need to be registered with Azure Communication Services to establish a call. The receiver's ID is typically provided by the caller when initiating the call
first step: Create an agent from the user access token as shown in the documentation you shared:
You need a user token to create an authenticated call agent. Generally, this token is generated from a service with authentication specific to the application. For more information on user access tokens and how to create them please see User access tokens.
For the quickstart, replace <User_Access_Token>
with a user access token generated for your Azure Communication Services resource.
/**
* Create the call agent for placing calls
*/
private void createAgent() {
Context context = this.getApplicationContext();
String userToken = "<USER_ACCESS_TOKEN>";
try {
CommunicationTokenCredential credential = new CommunicationTokenCredential(userToken);
CallClient callClient = new CallClient();
deviceManager = callClient.getDeviceManager(context).get();
callAgent = callClient.createCallAgent(getApplicationContext(), credential).get();
} catch (Exception ex) {
Toast.makeText(context, "Failed to create call agent.", Toast.LENGTH_SHORT).show();
}
}
second step: Start a video call by using the call agent
You can place the call by using the call agent. All you need to do is provide a list of callee IDs and the call options.
To place a call with video, you have to enumerate local cameras by using the deviceManager
getCameras
API. After you select a desired camera, use it to construct a LocalVideoStream
instance. Then pass it into videoOptions
as an item in the localVideoStream
array to a call method. When the call connects, it automatically starts sending a video stream from the selected camera to the other participant.
private void startCall() {
Context context = this.getApplicationContext();
EditText callIdView = findViewById(R.id.call_id);
String callId = callIdView.getText().toString();
ArrayList<CommunicationIdentifier> participants = new ArrayList<CommunicationIdentifier>();
List<VideoDeviceInfo> cameras = deviceManager.getCameras();
StartCallOptions options = new StartCallOptions();
if(!cameras.isEmpty()) {
currentCamera = getNextAvailableCamera(null);
currentVideoStream = new LocalVideoStream(currentCamera, context);
LocalVideoStream[] videoStreams = new LocalVideoStream[1];
videoStreams[0] = currentVideoStream;
VideoOptions videoOptions = new VideoOptions(videoStreams);
options.setVideoOptions(videoOptions);
showPreview(currentVideoStream);
}
participants.add(new CommunicationUserIdentifier(callId));
call = callAgent.startCall(
context,
participants,
options);
//Subscribe to events on updates of call state and remote participants
remoteParticipantUpdatedListener = this::handleRemoteParticipantsUpdate;
onStateChangedListener = this::handleCallOnStateChanged;
call.addOnRemoteParticipantsUpdatedListener(remoteParticipantUpdatedListener);
call.addOnStateChangedListener(onStateChangedListener);
}
Hope that helps. Please let us know if you have further questions