Skip to content

Commit 5b47570

Browse files
authored
Update media-streaming-quickstart-csharp.md
Tidy up code snippet based on feedback.
1 parent 8ed86fd commit 5b47570

File tree

1 file changed

+42
-57
lines changed

1 file changed

+42
-57
lines changed

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
```

0 commit comments

Comments
 (0)