Skip to content

Commit 32b13ad

Browse files
authored
Merge pull request #217800 from valindrae/media-streaming-updates
Media streaming updates
2 parents e1cb5fd + 231da86 commit 32b13ad

File tree

3 files changed

+60
-86
lines changed

3 files changed

+60
-86
lines changed

articles/communication-services/concepts/voice-video-calling/media-streaming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Audio streams can be used in many ways, below are some examples of how developer
4040
## Supported formats
4141

4242
### Mixed format
43-
Contains mixed audio of all participants on the call.
43+
Contains mixed audio of all participants on the call. As this is mixed audio, the participantRawID will be null.
4444

4545
### Unmixed
4646
Contains audio per participant per channel, with support for up to four channels for four dominant speakers. You will also get a participantRawID that you can use to determine the speaker.

articles/communication-services/quickstarts/voice-video-calling/includes/call-automation-media/media-streaming-quickstart-csharp.md

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -66,68 +66,53 @@ The sample below demonstrates how to listen to media stream using your websocket
6666
HttpListener httpListener = new HttpListener();
6767
httpListener.Prefixes.Add("http://localhost:80/");
6868
httpListener.Start();
69-
while (true) {
70-
HttpListenerContext httpListenerContext = await httpListener.GetContextAsync();
71-
if (httpListenerContext.Request.IsWebSocketRequest) {
72-
WebSocketContext websocketContext;
73-
try {
69+
while (true)
70+
{
71+
HttpListenerContext httpListenerContext = await httpListener.GetContextAsync();
72+
if (httpListenerContext.Request.IsWebSocketRequest)
73+
{
74+
WebSocketContext websocketContext;
75+
try
76+
{
7477
websocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol: null);
75-
string ipAddress = httpListenerContext.Request.RemoteEndPoint.Address.ToString();
76-
} catch (Exception ex) {
77-
httpListenerContext.Response.StatusCode = 500;
78-
httpListenerContext.Response.Close();
79-
return;
80-
}
78+
}
79+
catch (Exception ex)
80+
{
81+
return;
82+
}
8183
WebSocket webSocket = websocketContext.WebSocket;
82-
try {
83-
while (webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.CloseSent) {
84+
try
85+
{
86+
while (webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.CloseSent)
87+
{
8488
byte[] receiveBuffer = new byte[2048];
8589
var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token;
86-
WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment < byte >. (receiveBuffer), cancellationToken);
87-
if (receiveResult.MessageType != WebSocketMessageType.Close) {
88-
var data = Encoding.UTF8.GetString(receiveBuffer).TrimEnd('\0');
89-
try {
90-
var json = JsonConvert.DeserializeObject < Audio > (data);
91-
if (json != null) {
92-
var byteArray = json.AudioData;
93-
//Processing mixed audio data
94-
if (string.IsNullOrEmpty(json?.ParticipantId)) {
95-
if (string.IsNullOrEmpty(WebSocketData.FirstReceivedMixedAudioBufferTimeStamp)) {
96-
WebSocketData.FirstReceivedMixedAudioBufferTimeStamp = json.Timestamp;
97-
}
98-
//Process byteArray ( audioData ) however you want
99-
}
100-
}
101-
102-
//Processing unmixed audio data
103-
else if (!string.IsNullOrEmpty(json?.ParticipantId) && !json.IsSilence) {
104-
if (json.ParticipantId != null) {
105-
switch (json.ParticipantId) {
106-
case {
107-
participantRawId1
108-
}:
109-
//Process audio data
110-
break;
111-
case {
112-
participantRawId2
113-
}::
114-
//Process audio data
115-
break;
116-
default:
117-
break;
118-
}
119-
}
120-
if (string.IsNullOrEmpty(WebSocketData.FirstReceivedUnmixedAudioBufferTimeStamp)) {
121-
WebSocketData.FirstReceivedUnmixedAudioBufferTimeStamp = json.Timestamp;
90+
WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), cancellationToken);
91+
if (receiveResult.MessageType != WebSocketMessageType.Close)
92+
{
93+
var data = Encoding.UTF8.GetString(receiveBuffer).TrimEnd('\0');
94+
try
95+
{
96+
var eventData = JsonConvert.DeserializeObject<AudioBaseClass>(data);
97+
if (eventData != null)
98+
{
99+
if(eventData.kind == "AudioMetadata")
100+
{
101+
//Process audio metadata
102+
}
103+
else if(eventData.kind == "AudioData")
104+
{
105+
//Process audio data
106+
var byteArray = eventData.audioData.data;
107+
//use audio byteArray as you want
108+
}
122109
}
123110
}
124-
} catch {}
125-
}
126-
}
127-
} catch (Exception ex) {}
128-
} else {
129-
httpListenerContext.Response.StatusCode = 400;
130-
httpListenerContext.Response.Close();
131-
}
111+
catch { }
112+
}
113+
}
114+
}
115+
catch (Exception ex) { }
116+
}
132117
}
133118
```

articles/communication-services/quickstarts/voice-video-calling/media-streaming.md

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,31 @@ Get started with using audio streams through Azure Communication Services Media
3333
When ACS has received the URL for your WebSocket server, it will create a connection to it. Once ACS has successfully connected to your WebSocket server, it will send through the first data packet which contains metadata regarding the incoming media packets.
3434

3535
``` code
36-
/**
37-
* The first message upon WebSocket connection will be the metadata packet
38-
* which contains the subscriptionId and audio format
39-
*/
40-
public class AudioMetadataSample {
41-
public string kind; // What kind of data this is, e.g. AudioMetadata, AudioData.
42-
public AudioMetadata audioMetadata;
43-
}
44-
45-
public class AudioMetadata {
46-
public string subscriptionId // unique identifier for a subscription request
47-
public string encoding; // PCM only supported
48-
public int sampleRate; // 16000 default
49-
public int channels; // 1 default
50-
public int length; // 640 default
36+
{
37+
"kind": <string> // What kind of data this is, e.g. AudioMetadata, AudioData.
38+
"audioMetadata": {
39+
"subscriptionId": <string>, // unique identifier for a subscription request
40+
"encoding":<string>, // PCM only supported
41+
"sampleRate": <int>, // 16000 default
42+
"channels": <int>, // 1 default
43+
"length": <int> // 640 default
44+
}
5145
}
5246
```
5347

5448
## Audio streaming schema
5549
After sending through the metadata packet, ACS will start streaming audio media to your WebSocket server. Below is an example of what the media object your server will receive looks like.
5650

5751
``` code
58-
/**
59-
* The audio buffer object which is then serialized to JSON format
60-
*/
61-
public class AudioDataSample {
62-
public string kind; // What kind of data this is, e.g. AudioMetadata, AudioData.
63-
public AudioData audioData;
52+
{
53+
"kind": <string>, // What kind of data this is, e.g. AudioMetadata, AudioData.
54+
"audioData":{
55+
"data": <string>, // Base64 Encoded audio buffer data
56+
"timestamp": <string>, // In ISO 8601 format (yyyy-mm-ddThh:mm:ssZ)
57+
"participantRawID": <string>,
58+
"silent": <boolean> // Indicates if the received audio buffer contains only silence.
59+
}
6460
}
65-
66-
public class AudioData {
67-
public string data; // Base64 Encoded audio buffer data
68-
public string timestamp; // In ISO 8601 format (yyyy-mm-ddThh:mm:ssZ)
69-
public string participantRawID;
70-
public boolean silent; // Indicates if the received audio buffer contains only silence.
71-
}
7261
```
7362

7463
Example of audio data being streamed

0 commit comments

Comments
 (0)