Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-khalid committed May 30, 2024
1 parent 2b5ecf7 commit b5194f0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 86 deletions.
2 changes: 2 additions & 0 deletions shared/video-sdk/get-started/get-started-sdk/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ To start a session, implement the following steps in your <Vpl k="CLIENT" />:
Please refer to [Agora account management](../get-started/manage-agora-account) for details.

## Set up your project

This section shows you how to set up a new <Vpl k="NAME"/> project and integrate the <Vg k="COMPANY" /> <Vpd k="SDK" />.

<ProjectSetup/>

## Implement <Vpd k="NAME" />

This section guides you through the implementation of basic real-time audio and video interaction in your <Vpl k="CLIENT"/>.

<ProjectImplement/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ This section shows you how to import the libraries required to implement <Vpd k=
3. Check whether permissions have been granted. Add the following code after `Update`:

```c#
private void CheckPermissions()
{
private void CheckPermissions() {
#if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
foreach (string permission in permissionList)
{
if (!Permission.HasUserAuthorizedPermission(permission))
{
foreach (string permission in permissionList) {
if (!Permission.HasUserAuthorizedPermission(permission)) {
Permission.RequestUserPermission(permission);
}
}
Expand Down Expand Up @@ -95,39 +92,44 @@ internal IRtcEngine RtcEngine;
</ProductWrapper>
<ProductWrapper product="voice-calling">
```csharp
// Insert your App ID.
// Insert your App ID
private string _appID = "";
// Insert your channel name.
// Insert your channel name
private string _channelName = "";
// Insert Token.
// Insert Token
private string _token = "";
internal IRtcEngine RtcEngine;
````
</ProductWrapper>

### Create and initialize an `IRtcEngine` instance

Before calling other Agora APIs, you create and initialize an `IRtcEngine` instance. Call `CreateAgoraRtcEngine` to create the instance. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `CheckPermissions`:
Create and initialize `IRtcEngine`. Before calling other Agora APIs, you need to create and initialize an `IRtcEngine` instance. Call `CreateAgoraRtcEngine` to create the `IRtcEngine` instance. In `Assets/AgoraEngine/Scripts/JoinChannelAudio.cs`, add the following code after `CheckPermissions`:

<ProductWrapper notAllowed="voice-calling">
```c#
private void SetupVideoSDKEngine()
{
private void SetupVideoSDKEngine() {
// Create an IRtcEngine instance
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
RtcEngineContext context = new RtcEngineContext(_appID, 0,CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
// Initialize the instance
RtcEngineContext context = new RtcEngineContext();
context.appId = _appID;
context.channelProfile = CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING;
context.audioScenario = AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT;
context.areaCode = AREA_CODE.AREA_CODE_GLOB; // Initialize the instance
RtcEngine.Initialize(context);
}
```
</ProductWrapper>
<ProductWrapper product="voice-calling">
```c#
private void SetupAudioSDKEngine()
{
private void SetupAudioSDKEngine() {
// Create an IRtcEngine instance
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
RtcEngineContext context = new RtcEngineContext(_appID, 0,CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
RtcEngineContext context = new RtcEngineContext();
context.appId = _appID;
context.channelProfile = CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING;
context.audioScenario = AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT;
context.areaCode = AREA_CODE.AREA_CODE_GLOB; // Initialize the instance
// Initialize the instance
RtcEngine.Initialize(context);
}
Expand All @@ -139,70 +141,59 @@ private void SetupAudioSDKEngine()
Create an instance of the user event handler class and set it as the engine event handler. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `SetupVideoSDKEngine()`:
```c#
// Create a user event handler instance and set it as the engine event handler
private void InitEventHandler()
{
private void InitEventHandler() {
UserEventHandler handler = new UserEventHandler(this);
RtcEngine.InitEventHandler(handler);
}

// Implement your own EventHandler class by inheriting the IRtcEngineEventHandler interface class implementation
internal class UserEventHandler : IRtcEngineEventHandler
{
internal class UserEventHandler : IRtcEngineEventHandler {
private readonly JoinChannelVideo _videoSample;
internal UserEventHandler(JoinChannelVideo videoSample)
{
internal UserEventHandler(JoinChannelVideo videoSample) {
_videoSample = videoSample;
}
// error callback
public override void OnError(int err, string msg)
{
public override void OnError(int err, string msg) {
}
// Triggered when a local user successfully joins the channel
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
_videoSample.LocalView.SetForUser(0,"");
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed) {
_videoSample.LocalView.SetForUser(0,"");
}
}
```
</ProductWrapper>
<ProductWrapper product="voice-calling">
Create an instance of the user event handler class and set it as the engine event handler. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `SetupSDKEngine()`:
```csharp
// Create an instance of the user callback class and set the callback.
private void InitEventHandler()
{
// Create an instance of the user callback class and set the callback
private void InitEventHandler() {
UserEventHandler handler = new UserEventHandler(this);
RtcEngine.InitEventHandler(handler);
}

// Implement your own callback class, which can inherit from the IRtcEngineEventHandler interface class.
internal class UserEventHandler : IRtcEngineEventHandler
{
// Implement your own callback class, which can inherit from the IRtcEngineEventHandler interface class
internal class UserEventHandler : IRtcEngineEventHandler {
private readonly JoinChannelAudio _audioSample;

internal UserEventHandler(JoinChannelAudio audioSample)
{
internal UserEventHandler(JoinChannelAudio audioSample) {
_audioSample = audioSample;
}

// Error callback.
public override void OnError(int err, string msg)
{
}
// Triggered when the local user successfully joins the channel.
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
// Error callback
public override void OnError(int err, string msg) { }

// Triggered when the local user successfully joins the channel
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed) {
Debug.Log("OnJoinChannelSuccess _channelName");
}
// Triggered when a remote user successfully joins the channel.
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{

// Triggered when a remote user successfully joins the channel
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed) {
Debug.Log("Remote user joined");
}
// Triggered when a remote user leaves the current channel.
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
}

// Triggered when a remote user leaves the current channel
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason) { }
}
```
</ProductWrapper>
Expand All @@ -211,8 +202,7 @@ internal class UserEventHandler : IRtcEngineEventHandler
<ProductWrapper notAllowed="voice-calling">
UI elements are referenced in <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>. To set up the user interface, add the following code before `SetupVideoSDKEngine()`:
```c#
private void SetupUI()
{
private void SetupUI() {
GameObject go = GameObject.Find("LocalView");
LocalView = go.AddComponent<VideoSurface>();
go.transform.Rotate(0.0f, 0.0f, -180.0f);
Expand All @@ -229,8 +219,7 @@ private void SetupUI()
<ProductWrapper product="voice-calling">
UI elements are referenced in <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>. To set up the user interface, add the following code before `SetupSDKEngine()`:
```csharp
private void SetupUI()
{
private void SetupUI() {
GameObject go = GameObject.Find("Leave");
go.GetComponent<Button>().onClick.AddListener(Leave);
go = GameObject.Find("Join");
Expand All @@ -243,8 +232,7 @@ private void SetupUI()
In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `InitEventHandler`:

```c#
public void Join()
{
public void Join() {
// Enable the video module
RtcEngine.EnableVideo();
// Set channel media options
Expand Down Expand Up @@ -272,8 +260,7 @@ public void Join()
In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `InitEventHandler`:

```c#
public void Join()
{
public void Join() {
// Enable the video module
RtcEngine.EnableVideo();
// Set channel media options
Expand All @@ -300,8 +287,7 @@ public void Join()
In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `InitEventHandler`:

```c#
public void Join()
{
public void Join() {
// Enable the video module
RtcEngine.EnableVideo();
// Set channel media options
Expand All @@ -326,20 +312,19 @@ public void Join()
In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `InitEventHandler`:

```csharp
public void Join()
{
public void Join() {
Debug.Log("Joining _channelName");
// Enable the audio module.
// Enable the audio module
RtcEngine.EnableAudio();
// Set channel media options.
// Set channel media options
ChannelMediaOptions options = new ChannelMediaOptions();
// Automatically subscribe to all audio streams.
// Automatically subscribe to all audio streams
options.autoSubscribeAudio.SetValue(true);
// Set the channel profile to live broadcasting.
// Set the channel profile to live broadcasting
options.channelProfile.SetValue(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
// Set the user role to broadcaster.
// Set the user role to broadcaster
options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
// Join the channel.
// Join the channel
RtcEngine.JoinChannel(_token, _channelName, 0, options);
}
```
Expand All @@ -351,8 +336,7 @@ In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the fo

```c#
// When the SDK receives the first frame of a remote video stream and successfully decodes it, the OnUserJoined callback is triggered.
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed) {
// Set the remote video display
_videoSample.RemoteView.SetForUser(uid, connection.channelId, VIDEO_SOURCE_TYPE.VIDEO_SOURCE_REMOTE);
// Start video rendering
Expand All @@ -369,16 +353,14 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse

```c#
// This callback is triggered when a remote user leaves the current channel
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason) {
_videoSample.RemoteView.SetEnable(false);
}
```
1. When a user clicks **Leave**, call `LeaveChannel` to leave the current channel. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `Join`:

```c#
public void Leave()
{
public void Leave() {
Debug.Log("Leaving _channelName");
// Leave the channel
RtcEngine.LeaveChannel();
Expand All @@ -395,8 +377,7 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse
1. When a user clicks **Leave**, call `LeaveChannel` to leave the current channel. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, add the following code after `Join`:

```c#
public void Leave()
{
public void Leave() {
Debug.Log("Leaving _channelName");
// Leave the channel
RtcEngine.LeaveChannel();
Expand All @@ -411,8 +392,7 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse
1. When the <Vpl k="CLIENT" /> starts, check whether device permissions have been granted. In <code>Assets/Agora-Unity-RTC-SDK/Code/Rtc/{props.fileName}</code>, replace the `Update` method with the following:

```c#
void Update()
{
void Update() {
CheckPermissions();
}
```
Expand All @@ -421,8 +401,7 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse

<ProductWrapper notAllowed="voice-calling">
```c#
void Start()
{
void Start() {
SetupVideoSDKEngine();
InitEventHandler();
SetupUI();
Expand All @@ -432,8 +411,7 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse

<ProductWrapper product="voice-calling">
```c#
void Start()
{
void Start() {
SetupAudioSDKEngine();
InitEventHandler();
SetupUI();
Expand All @@ -444,10 +422,8 @@ public override void OnUserJoined(RtcConnection connection, uint uid, int elapse
1. To clean up all session-related resources when a user leaves the channel, call the `Dispose` method of `IRtcEngine`. Add the following code after `Update`:

```c#
void OnApplicationQuit()
{
if (RtcEngine != null)
{
void OnApplicationQuit() {
if (RtcEngine != null) {
Leave();
// Destroy IRtcEngine
RtcEngine.Dispose();
Expand Down

0 comments on commit b5194f0

Please sign in to comment.