Skip to content

Commit 8cfffdd

Browse files
author
Vinothini Dharmaraj
committed
adding the string based parser
1 parent 2093ef4 commit 8cfffdd

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

sdk/communication/Azure.Communication.CallAutomation/api/Azure.Communication.CallAutomation.netstandard2.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,9 @@ protected StreamingData() { }
13791379
}
13801380
public static partial class StreamingDataParser
13811381
{
1382+
public static Azure.Communication.CallAutomation.StreamingData Parse(System.BinaryData binaryData) { throw null; }
13821383
public static Azure.Communication.CallAutomation.StreamingData Parse(byte[] receivedBytes) { throw null; }
1384+
public static Azure.Communication.CallAutomation.StreamingData Parse(string stringJson) { throw null; }
13831385
}
13841386
public enum TextFormat
13851387
{

sdk/communication/Azure.Communication.CallAutomation/src/Models/Streaming/StreamingDataParser.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ namespace Azure.Communication.CallAutomation
1313
/// </summary>
1414
public static class StreamingDataParser
1515
{
16+
/// <summary>
17+
/// Parsing a MediaStreaming package from BinaryData.
18+
/// </summary>
19+
/// <param name="binaryData"></param>
20+
/// <returns></returns>
21+
public static StreamingData Parse(BinaryData binaryData)
22+
{
23+
return Parse(binaryData.ToString());
24+
}
25+
1626
/// <summary>
1727
/// Parsing a MediaStreaming package from a byte array.
1828
/// </summary>
@@ -29,7 +39,7 @@ public static StreamingData Parse(byte[] receivedBytes)
2939
/// <param name="stringJson"></param>
3040
/// <returns></returns>
3141
/// <exception cref="NotImplementedException"></exception>
32-
private static StreamingData Parse(string stringJson)
42+
public static StreamingData Parse(string stringJson)
3343
{
3444
JsonElement package = JsonDocument.Parse(stringJson).RootElement;
3545

sdk/communication/Azure.Communication.CallAutomation/tests/Streaming/StreamingDataParserTests.cs

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,26 @@ namespace Azure.Communication.CallAutomation.Tests.MediaStreaming
1313
internal class StreamingDataParserTests
1414
{
1515
#region Audio
16-
1716
[Test]
1817
public void ParseAudioMetadata_Test()
18+
{
19+
string metadataJson = "{"
20+
+ "\"kind\": \"AudioMetadata\","
21+
+ "\"audioMetadata\": {"
22+
+ "\"subscriptionId\": \"subscriptionId\","
23+
+ "\"encoding\": \"encodingType\","
24+
+ "\"sampleRate\": 8,"
25+
+ "\"channels\": 2,"
26+
+ "\"length\": 640"
27+
+ "}"
28+
+ "}";
29+
30+
AudioMetadata streamingMetadata = (AudioMetadata)StreamingDataParser.Parse(metadataJson);
31+
ValidateAudioMetadata(streamingMetadata);
32+
}
33+
34+
[Test]
35+
public void ParseAudioMetadata_bytes_Test()
1936
{
2037
string metadataJson = "{"
2138
+ "\"kind\": \"AudioMetadata\","
@@ -34,6 +51,23 @@ public void ParseAudioMetadata_Test()
3451
ValidateAudioMetadata(streamingMetadata);
3552
}
3653

54+
[Test]
55+
public void ParseAudioData_Test()
56+
{
57+
string audioJson = "{"
58+
+ "\"kind\": \"AudioData\","
59+
+ "\"audioData\": {"
60+
+ "\"data\": \"AQIDBAU=\"," // [1, 2, 3, 4, 5]
61+
+ "\"timestamp\": \"2022-08-23T11:48:05Z\","
62+
+ "\"participantRawID\": \"participantId\","
63+
+ "\"silent\": false"
64+
+ "}"
65+
+ "}";
66+
67+
AudioData streamingAudio = (AudioData)StreamingDataParser.Parse(audioJson);
68+
ValidateAudioData(streamingAudio);
69+
}
70+
3771
[Test]
3872
public void ParseAudioData_NoParticipantIdSilent_Test()
3973
{
@@ -50,6 +84,23 @@ public void ParseAudioData_NoParticipantIdSilent_Test()
5084
ValidateAudioDataNoParticipant(streamingAudio);
5185
}
5286

87+
[Test]
88+
public void ParseBinaryAudioData()
89+
{
90+
JObject jsonData = new JObject();
91+
jsonData["kind"] = "AudioData";
92+
jsonData["audioData"] = new JObject();
93+
jsonData["audioData"]!["data"] = "AQIDBAU=";
94+
jsonData["audioData"]!["timestamp"] = "2022-08-23T11:48:05Z";
95+
jsonData["audioData"]!["participantRawID"] = "participantId";
96+
jsonData["audioData"]!["silent"] = false;
97+
98+
var binaryData = BinaryData.FromString(jsonData.ToString());
99+
100+
AudioData streamingAudio = (AudioData)StreamingDataParser.Parse(binaryData);
101+
ValidateAudioData(streamingAudio);
102+
}
103+
53104
[Test]
54105
public void ParseAudioDataEventsWithBinaryArray()
55106
{
@@ -99,6 +150,83 @@ private static void ValidateAudioDataNoParticipant(AudioData streamingAudio)
99150

100151
#region Transcription
101152

153+
[Test]
154+
public void ParseTranscriptionData_Test()
155+
{
156+
var transcriptionJson =
157+
"{" +
158+
"\"kind\":\"TranscriptionData\"," +
159+
"\"transcriptionData\":" +
160+
"{" +
161+
"\"text\":\"Hello World!\"," +
162+
"\"format\":\"display\"," +
163+
"\"confidence\":0.98," +
164+
"\"offset\":1," +
165+
"\"duration\":2," +
166+
"\"words\":" +
167+
"[" +
168+
"{" +
169+
"\"text\":\"Hello\"," +
170+
"\"offset\":1," +
171+
"\"duration\":1" +
172+
"}," +
173+
"{" +
174+
"\"text\":\"World\"," +
175+
"\"offset\":6," +
176+
"\"duration\":1" +
177+
"}" +
178+
"]," +
179+
"\"participantRawID\":\"abc12345\"," +
180+
"\"resultStatus\":\"final\"" +
181+
"}" +
182+
"}";
183+
184+
TranscriptionData transcription = (TranscriptionData)StreamingDataParser.Parse(transcriptionJson);
185+
ValidateTranscriptionData(transcription);
186+
}
187+
188+
[Test]
189+
public void ParseTranscriptionBinaryData()
190+
{
191+
JObject jsonData = new()
192+
{
193+
["kind"] = "TranscriptionData",
194+
["transcriptionData"] = new JObject()
195+
};
196+
jsonData["transcriptionData"]!["text"] = "Hello World!";
197+
jsonData["transcriptionData"]!["format"] = "display";
198+
jsonData["transcriptionData"]!["confidence"] = 0.98d;
199+
jsonData["transcriptionData"]!["offset"] = 1;
200+
jsonData["transcriptionData"]!["duration"] = 2;
201+
202+
JArray words = new();
203+
jsonData["transcriptionData"]!["words"] = words;
204+
205+
JObject word0 = new()
206+
{
207+
["text"] = "Hello",
208+
["offset"] = 1,
209+
["duration"] = 1
210+
};
211+
words.Add(word0);
212+
213+
JObject word1 = new()
214+
{
215+
["text"] = "World",
216+
["offset"] = 6,
217+
["duration"] = 1
218+
};
219+
words.Add(word1);
220+
221+
jsonData["transcriptionData"]!["participantRawID"] = "abc12345";
222+
jsonData["transcriptionData"]!["resultStatus"] = "final";
223+
224+
var binaryData = BinaryData.FromString(jsonData.ToString());
225+
226+
TranscriptionData transcription = (TranscriptionData)StreamingDataParser.Parse(binaryData);
227+
ValidateTranscriptionData(transcription);
228+
}
229+
102230
[Test]
103231
public void ParseTranscriptionMetadata_Test()
104232
{

0 commit comments

Comments
 (0)