Skip to content

Realtime Avatar Client

Test the IRealtimeAvatarClient adapter for D-ID realtime sessions.

This example assumes using DId; is in scope and apiKey contains your DId API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var apiKey =
    Environment.GetEnvironmentVariable("DID_API_KEY") is { Length: > 0 } key
        ? key
        : throw new AssertInconclusiveException("DID_API_KEY environment variable is not found.");

var agentId =
    Environment.GetEnvironmentVariable("DID_AGENT_ID") is { Length: > 0 } id
        ? id
        : throw new AssertInconclusiveException("DID_AGENT_ID environment variable is not found.");

var client = new DIdClient(apiKey);

// Create and connect via the unified interface
await using var avatar = await DIdRealtimeAvatarClient.ConnectAsync(
    client, agentId);

// Verify the adapter implements IRealtimeAvatarClient
tryAGI.RealtimeAvatar.IRealtimeAvatarClient realtimeClient = avatar;

// SendAudioAsync should throw NotSupportedException (D-ID is text-only)
Func<Task> sendAudio = () => avatar.SendAudioAsync(ReadOnlyMemory<byte>.Empty);

// Send text and receive at least one video frame
await avatar.SendTextAsync("Hello, this is a test.");

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var receivedVideo = false;
var receivedAudio = false;

var videoTask = Task.Run(async () =>
{
    await foreach (var frame in avatar.ReceiveVideoFramesAsync(cts.Token))
    {
        receivedVideo = true;
        break; // Just verify we get at least one frame
    }
}, cts.Token);

var audioTask = Task.Run(async () =>
{
    await foreach (var frame in avatar.ReceiveAudioFramesAsync(cts.Token))
    {
        receivedAudio = true;
        break;
    }
}, cts.Token);

try
{
    await Task.WhenAny(videoTask, audioTask, Task.Delay(TimeSpan.FromSeconds(30), cts.Token));
}
catch (OperationCanceledException) { }

// At least one type of frame should be received
    "Expected to receive at least one video or audio frame from D-ID avatar.");