With the .NET SDK and Virtual World plugin for Unity, you can easily integrate Dolby quality spatial communications capabilities into your virtual world applications.
The Virtual World plugin for Unity is built on top of the .NET SDK and includes support for Unity’s visual scripting. You can find the latest releases package here. Refer to this article for installing and using the Unity plugin.
The following guide presents an example of using the SDK to create a basic .NET audio-only conference application. The resulting project provides the foundation upon which you can add additional features as you build out your application.
You can find the complete code for the application in the Summary section. The created application is available in the SimpleApp folder.
Make sure that you have:
- A Dolby.io account. If you do not have an account, you can sign up for a free account.
- The client access token copied from the Dolby.io dashboard. To create the token, log into the Dolby.io dashboard, create an application, and navigate to the API keys section.
Use the following command to install the .NET SDK from NuGet into your .NET project:
dotnet add package DolbyIO.Comms.Sdk
Initialize the SDK using the secure authentication method that uses a token in the application. For the purpose of this application, use a client access token generated from the Dolby.io dashboard. Use the following code to initialize the SDK using the DolbyIOSDK.InitAsync method:
using DolbyIO.Comms;
DolbyIOSDK _sdk = new DolbyIOSDK();
try
{
await _sdk.InitAsync("Access Token", () =>
{
// Refresh Callback
return "Refreshed Access Token";
});
}
catch (DolbyIOException e)
{
// Error Handling
}
NOTE: The SDK is fully asynchronous, and all methods can throw the DolbyIOException.
After initializing the SDK, it is time to register your event handlers. Decide which events you want to add event handlers to and add the handlers as in the following example:
// Registering event handlers
_sdk.Conference.StatusUpdated = OnConferenceStatus;
// or inline
_sdk.Conference.ParticipantAdded = new ParticipantAddedEventHandler
(
(Participant participant) =>
{
}
);
A session is a connection between the client application and the Dolby.io backend. When opening a session, you should provide a name. Commonly, this is the name of the participant who established the session. The session can remain active for the whole lifecycle of your application.
To open a new session, use the Session.OpenAsync method as in the following example:
try
{
UserInfo user = new UserInfo();
user.Name = "My Name";
user = await _sdk.Session.OpenAsync(user);
}
catch (DolbyIOException e)
{
// Error Handling
}
A conference is a multi-person call where participants exchange audio with one another. To distinguish between multiple conferences, you should assign a conference alias or name. When multiple participants join a conference of the same name using a token of the same Dolby.io application, they will be able to meet in a call.
To create and join a conference, use the Conference.CreateAsync and Conference.JoinAsync methods as in the following example:
try
{
ConferenceOptions options = new ConferenceOptions();
options.Alias = "Conference alias";
JoinOptions joinOpts = new JoinOptions();
Conference conference = await _sdk.Conference.CreateAsync(options);
Conference joinedConference = await _sdk.Conference.JoinAsync(conference, joinOpts);
}
catch (DolbyIOException e)
{
// Error Handling
}
If the conference already exists, the SDK returns Conference.
To leave the conference, use the Conference.LeaveAsync method, as in the following example:
try
{
await _sdk.Session.LeaveAsync();
}
catch (DolbyIOException e)
{
// Error Handling
}
After leaving the conference, close the session and dispose the SDK using the Session.CloseAsync and DolbyIOSDK.Dispose methods.
try
{
await _sdk.Session.CloseAsync();
_sdk.Dispose();
}
catch (DolbyIOException e)
{
// Error Handling
}
This step releases the underneath unmanaged native resources.
All the steps combined create the following code snippet:
using System;
using DolbyIO.Comms;
public class Call
{
private DolbyIOSDK _sdk = new DolbyIOSDK();
public async Task OpenAndJoinAsync()
{
try
{
await _sdk.InitAsync("Access Token", () =>
{
return "Refreshed Access Token";
});
// Registering event handlers
_sdk.Conference.StatusUpdated = OnConferenceStatus;
// or inline
_sdk.Conference.ParticipantAdded = new ParticipantAddedEventHandler
(
(Participant participant) =>
{
}
);
UserInfo user = new UserInfo();
user.Name = "My Name";
user = await _sdk.Session.OpenAsync(user);
ConferenceOptions options = new ConferenceOptions();
options.Alias = "Conference alias";
JoinOptions joinOpts = new JoinOptions();
Conference conference = await _sdk.Conference.CreateAsync(options);
Conference joinedConference = await _sdk.Conference.JoinAsync(conference, joinOpts);
}
catch (DolbyIOException e)
{
Console.WriteLine(e.Message);
}
}
public async Task LeaveAndCloseAsync()
{
try
{
await _sdk.Conference.LeaveAsync();
await _sdk.Session.CloseAsync();
_sdk.Dispose();
}
catch (DolbyIOException e)
{
Console.WriteLine(e.Message);
}
}
private void OnConferenceStatus(ConferenceStatus status, string conferenceId)
{
}
}
public class Program
{
public static async Task Main()
{
Call call = new Call();
await call.OpenAndJoinAsync();
Console.ReadKey();
await call.LeaveAndCloseAsync();
}
}
If you want to build the SDK from source code, make sure that you have:
- The Dolby.io Communications C++ SDK 2.3.0-alpha.3 for your platform
- .NET SDK 6
- C++ compiler compatible with C++ 17
- CMake 3.23
- A macOS or Windows machine
In order to compile the Dolby.io Communications .NET SDK, you will need to use CMake for the build chain and generating projects. To build the application from sources, use the following commands:
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build .
The .NET SDK depends on the C++ SDK for core functions and supports the same functionalities for client applications as the C++ SDK.
cmake
allows you to generate various project files, for example, if you want to generate a Visual Studio solution on Windows, you can add the following option when generating (-G "Visual Studio 17 2022"
):
cmake .. -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 17 2022"
Note: Currently, the SDK is only compatible with the x86_64 architecture. If you want to compile your application on a Mac with an Apple Silicon chip, you need to use the x64 .NET SDK, not the ARM one, and specify the architecture on which CMake should build upon:
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_OSX_ARCHITECTURES=x86_64
Run the unit tests after compiling the SDK using the following command:
ctest -VV -C RelWithDebInfo