Skip to content

Commit a8e610a

Browse files
authored
Update audio-streaming-quickstart-csharp.md
update code snippets
1 parent 5878f0a commit a8e610a

File tree

1 file changed

+26
-68
lines changed

1 file changed

+26
-68
lines changed

articles/communication-services/how-tos/call-automation/includes/audio-streaming-quickstart-csharp.md

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ ms.author: alvinhan
1616
- An Azure Communication Services resource. See [Create an Azure Communication Services resource](../../../quickstarts/create-communication-resource.md?tabs=windows&pivots=platform-azp).
1717
- A new web service application created using the [Call Automation SDK](../../../quickstarts/call-automation/callflows-for-customer-interactions.md).
1818
- The latest [.NET library](https://dotnet.microsoft.com/download/dotnet-core) for your operating system.
19-
- A websocket server that can receive media streams.
19+
- A websocket server that can send and receive media streams.
2020

2121
## Set up a websocket server
2222
Azure Communication Services requires your server application to set up a WebSocket server to stream audio in real-time. WebSocket is a standardized protocol that provides a full-duplex communication channel over a single TCP connection.
23-
You can optionally use Azure services Azure WebApps that allows you to create an application to receive audio streams over a websocket connection. Follow this [quickstart](https://azure.microsoft.com/blog/introduction-to-websockets-on-windows-azure-web-sites/).
23+
24+
You can review documentation [here](https://azure.microsoft.com/blog/introduction-to-websockets-on-windows-azure-web-sites/) to learn more about websockets and how to use them.
2425

2526
## Receiving and Sending audio streaming data
2627
There are multiple ways to start receiving audio stream, which can be configured using the `startMediaStreaming` flag in the `mediaStreamingOptions` setup. You can also specify the desired sample rate used for recieving or sending audio data using the `audioFormat` parameter. Currently supported formats are PCM 24K mono and PCM 16K mono, with the default being PCM 16K mono.
2728

28-
To enable bidirectional audio streaming, where you're sending audio data into the call, you can enable the `EnableBidirectional` flag.
29+
To enable bidirectional audio streaming, where you're sending audio data into the call, you can enable the `EnableBidirectional` flag. For more details refer to the [API specifications](https://learn.microsoft.com/rest/api/communication/callautomation/answer-call/answer-call?view=rest-communication-callautomation-2024-06-15-preview&tabs=HTTP#mediastreamingoptions).
2930

3031
### Start streaming audio to your webserver at time of answering the call
3132
Enable automatic audio streaming when the call is established by setting the flag `startMediaStreaming: true`.
@@ -95,28 +96,20 @@ await callMedia.StopMediaStreamingAsync();
9596
The sample below demonstrates how to listen to audio streams using your websocket server.
9697

9798
``` C#
98-
public async Task StartReceivingFromAcsMediaWebSocket() {
99-
if (m_webSocket == null) {
100-
return;
101-
}
102-
try {
103-
while (m_webSocket.State == WebSocketState.Open || m_webSocket.State == WebSocketState.Closed) {
104-
byte[] receiveBuffer = new byte[2048];
105-
WebSocketReceiveResult receiveResult = await m_webSocket.ReceiveAsync(new ArraySegment < byte > (receiveBuffer), m_cts.Token);
106-
107-
if (receiveResult.MessageType != WebSocketMessageType.Close) {
108-
string data = Encoding.UTF8.GetString(receiveBuffer).TrimEnd('\0');
109-
var input = StreamingData.Parse(data, AudioData.class);
110-
if (input is AudioData audioData) {
111-
using(var ms = new MemoryStream(audioData.Data)) {
112-
// Forward audio data to external AI
113-
await m_aiServiceHandler.SendAudioToExternalAI(ms);
114-
}
115-
}
99+
private async Task StartReceivingFromAcsMediaWebSocket(Websocket websocket) {
100+
101+
while (webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.Closed) {
102+
byte[] receiveBuffer = new byte[2048];
103+
WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(
104+
new ArraySegment < byte > (receiveBuffer));
105+
106+
if (receiveResult.MessageType != WebSocketMessageType.Close) {
107+
string data = Encoding.UTF8.GetString(receiveBuffer).TrimEnd('\0');
108+
var input = StreamingData.Parse(data);
109+
if (input is AudioData audioData) {
110+
// Add your code here to process the received audio chunk
116111
}
117112
}
118-
} catch (Exception ex) {
119-
Console.WriteLine($"Exception -> {ex}");
120113
}
121114
}
122115
```
@@ -158,58 +151,23 @@ Once Azure Communication Services begins streaming audio to your WebSocket serve
158151
The example below demonstrates how to transmit the audio data back into the call after it has been processed by another service, for instance Azure OpenAI or other such voice based Large Language Models.
159152

160153
``` C#
161-
private void ConvertToAcsAudioPacketAndForward(byte[] audioData) {
162-
var audio = OutStreamingData.GetStreamingDataForOutbound(audioData)
163-
164-
// Serialize the JSON object to a string
165-
string jsonString = System.Text.Json.JsonSerializer.Serialize < OutStreamingData > (audio);
154+
var audioData = OutStreamingData.GetAudioDataForOutbound(audioData))
155+
byte[] jsonBytes = Encoding.UTF8.GetBytes(audioData);
166156

167-
// queue it to the buffer
168-
try {
169-
m_channel.Writer.TryWrite(async () => await m_mediaStreaming.SendMessageAsync(jsonString));
170-
} catch (Exception ex) {
171-
Console.WriteLine($"\"Exception received on ReceiveAudioForOutBound {ex}");
172-
}
173-
}
174-
175-
public async Task SendMessageAsync(string message) {
176-
if (m_webSocket?.State == WebSocketState.Open) {
177-
byte[] jsonBytes = Encoding.UTF8.GetBytes(message);
178-
179-
// Send the PCM audio chunk over WebSocket
180-
await m_webSocket.SendAsync(new ArraySegment < byte > (jsonBytes), WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None);
181-
}
182-
}
157+
// Write your logic to send the PCM audio chunk over the WebSocket
158+
// Example of how to send audio data over the WebSocket
159+
await m_webSocket.SendAsync(new ArraySegment < byte > (jsonBytes), WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None);
183160
```
184161

185162
You can also control the playback of audio in the call when streaming back to Azure Communication Services, based on your logic or business flow. For example, when voice activity is detected and you want to stop the queued up audio, you can send a stop message via the WebSocket to stop the audio from playing in the call.
186163

187164
``` C#
188-
private void StopAudio() {
189-
try {
190-
var jsonObject = OutStreamingData.GetStopAudioForOutbound();
165+
var stopData = OutStreamingData.GetStopAudioForOutbound();
166+
byte[] jsonBytes = Encoding.UTF8.GetBytes(stopData);
191167

192-
// Serialize the JSON object to a string
193-
string jsonString = System.Text.Json.JsonSerializer.Serialize < OutStreamingData > (jsonObject);
194-
195-
try {
196-
m_channel.Writer.TryWrite(async () => await m_mediaStreaming.SendMessageAsync(jsonString));
197-
} catch (Exception ex) {
198-
Console.WriteLine($"\"Exception received on ReceiveAudioForOutBound {ex}");
199-
}
200-
} catch (Exception ex) {
201-
Console.WriteLine($"Exception during streaming -> {ex}");
202-
}
203-
}
204-
205-
public async Task SendMessageAsync(string message) {
206-
if (m_webSocket?.State == WebSocketState.Open) {
207-
byte[] jsonBytes = Encoding.UTF8.GetBytes(message);
208-
209-
// Send the PCM audio chunk over WebSocket
210-
await m_webSocket.SendAsync(new ArraySegment < byte > (jsonBytes), WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None);
211-
}
212-
}
168+
// Write your logic to send stop data to ACS over the WebSocket
169+
// Example of how to send stop data over the WebSocket
170+
await m_webSocket.SendAsync(new ArraySegment < byte > (jsonBytes), WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None);
213171
```
214172

215173

0 commit comments

Comments
 (0)